LobbySi

Untitled

Feb 6th, 2025
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub ImportCSV()
  2.     Dim ws As Worksheet
  3.     Dim qt As QueryTable
  4.     Dim csvFilePath As String
  5.  
  6.     ' Укажите путь к CSV-файлу
  7.    csvFilePath = "C:\path\to\your\file.csv"
  8.    
  9.     ' Укажите лист, куда будет загружен файл
  10.    Set ws = ThisWorkbook.Sheets("Sheet1")
  11.    
  12.     ' Очистка предыдущих данных
  13.    ws.Cells.Clear
  14.    
  15.     ' Создание QueryTable для импорта CSV
  16.    Set qt = ws.QueryTables.Add(Connection:="TEXT;" & csvFilePath, Destination:=ws.Range("A1"))
  17.    
  18.     ' Настройки импорта
  19.    With qt
  20.         .TextFileConsecutiveDelimiter = False
  21.         .TextFileCommaDelimiter = True ' Разделитель - запятая (для других используйте TextFileOtherDelimiter)
  22.        .TextFileOtherDelimiter = ";"  ' Для CSV с другим разделителем (например, точка с запятой)
  23.        .TextFilePlatform = xlWindows
  24.         .TextFileStartRow = 1
  25.         .TextFileParseType = xlDelimited
  26.         .TextFileConsecutiveDelimiter = False
  27.         .Refresh BackgroundQuery:=False
  28.     End With
  29.  
  30.     ' Освобождение памяти
  31.    Set qt = Nothing
  32.     Set ws = Nothing
  33.  
  34.     MsgBox "Импорт завершен!", vbInformation
  35. End Sub
  36.  
Advertisement
Add Comment
Please, Sign In to add comment