使用 VBA 从第 2 行开始从 CSV 导入
Starting import from CSV from row 2 using VBA
我是 VBA 的新手,因此需要一些帮助。我正在尝试使用一种方法从 CSV 文件导入数据,我可以使用以下代码选择列表的 csv:
Private Sub commandbuttonimport_click()
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Filters.Clear
.Title = "Select a CSV File"
.Filters.Add "CSV", "*.csv", 1
.AllowMultiSelect = False
Dim sfile As String
If .Show = True Then
sfile = .SelectedItems(1)
End If
End With
'import csv from filedialog
If sfile <> "" Then
Open sfile For Input As #1
row_number = 1
Do Until EOF(1)
Line Input #1, LineFromFile
LineItems = Split(LineFromFile, ";")
Application.Range("FC_Range_Final").Cells(row_number, 1).Value = LineItems(0)
Application.Range("FC_Range_Final").Cells(row_number, 2).Value = LineItems(1)
Application.Range("FC_Range_Final").Cells(row_number, 3).Value = LineItems(2)
row_number = row_number + 1
Loop
Close #1
End If
End Sub
但问题是,我只想从第 2 行开始从所选的 csv 文件导入数据并跳过第一行。如果我手动从 CSV 文件(包含 headers)中删除第一行,然后使用上面的代码一切正常,但如果 headers 仍然没有运气。
帮助将不胜感激,因为我现在有点卡住了。
尝试以下...
'import csv from filedialog
Dim LineItems() As String
Dim LineFromFile As String
Dim line_number As Long
Dim row_number As Long
line_number = 1
row_number = 1
Open sfile For Input As #1
Do Until EOF(1)
Line Input #1, LineFromFile
If line_number > 1 Then
LineItems = Split(LineFromFile, ";")
Application.Range("FC_Range_Final").Cells(row_number, 1).Value = LineItems(0)
Application.Range("FC_Range_Final").Cells(row_number, 2).Value = LineItems(1)
Application.Range("FC_Range_Final").Cells(row_number, 3).Value = LineItems(2)
row_number = row_number + 1
End If
line_number = line_number + 1
Loop
Close #1
编辑
根据您的评论,换行符 (vbLf) 似乎用作行尾标记。假设是这种情况,您需要将代码更改为以下内容...
'import csv from filedialog
Dim DataFromFile As String
Dim LinesFromData() As String
Dim LineItems() As String
Dim row_number As Long
Open sfile For Input As #1
DataFromFile = Input(LOF(1), 1) ' get contents from entire file
LinesFromData = Split(DataFromFile, vbLf) ' split data into separate lines assuming line feed as end of file marker
For row_number = LBound(LinesFromData) + 1 To UBound(LinesFromData) ' +1 to start with second line of data
LineItems = Split(LinesFromData(row_number), ";") ' split the line into separate items
If UBound(LineItems) <> -1 Then
Application.Range("FC_Range_Final").Cells(row_number, 1).Resize(, 3).Value = LineItems
End If
Next row_number
Close #1
我是 VBA 的新手,因此需要一些帮助。我正在尝试使用一种方法从 CSV 文件导入数据,我可以使用以下代码选择列表的 csv:
Private Sub commandbuttonimport_click()
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Filters.Clear
.Title = "Select a CSV File"
.Filters.Add "CSV", "*.csv", 1
.AllowMultiSelect = False
Dim sfile As String
If .Show = True Then
sfile = .SelectedItems(1)
End If
End With
'import csv from filedialog
If sfile <> "" Then
Open sfile For Input As #1
row_number = 1
Do Until EOF(1)
Line Input #1, LineFromFile
LineItems = Split(LineFromFile, ";")
Application.Range("FC_Range_Final").Cells(row_number, 1).Value = LineItems(0)
Application.Range("FC_Range_Final").Cells(row_number, 2).Value = LineItems(1)
Application.Range("FC_Range_Final").Cells(row_number, 3).Value = LineItems(2)
row_number = row_number + 1
Loop
Close #1
End If
End Sub
但问题是,我只想从第 2 行开始从所选的 csv 文件导入数据并跳过第一行。如果我手动从 CSV 文件(包含 headers)中删除第一行,然后使用上面的代码一切正常,但如果 headers 仍然没有运气。
帮助将不胜感激,因为我现在有点卡住了。
尝试以下...
'import csv from filedialog
Dim LineItems() As String
Dim LineFromFile As String
Dim line_number As Long
Dim row_number As Long
line_number = 1
row_number = 1
Open sfile For Input As #1
Do Until EOF(1)
Line Input #1, LineFromFile
If line_number > 1 Then
LineItems = Split(LineFromFile, ";")
Application.Range("FC_Range_Final").Cells(row_number, 1).Value = LineItems(0)
Application.Range("FC_Range_Final").Cells(row_number, 2).Value = LineItems(1)
Application.Range("FC_Range_Final").Cells(row_number, 3).Value = LineItems(2)
row_number = row_number + 1
End If
line_number = line_number + 1
Loop
Close #1
编辑
根据您的评论,换行符 (vbLf) 似乎用作行尾标记。假设是这种情况,您需要将代码更改为以下内容...
'import csv from filedialog
Dim DataFromFile As String
Dim LinesFromData() As String
Dim LineItems() As String
Dim row_number As Long
Open sfile For Input As #1
DataFromFile = Input(LOF(1), 1) ' get contents from entire file
LinesFromData = Split(DataFromFile, vbLf) ' split data into separate lines assuming line feed as end of file marker
For row_number = LBound(LinesFromData) + 1 To UBound(LinesFromData) ' +1 to start with second line of data
LineItems = Split(LinesFromData(row_number), ";") ' split the line into separate items
If UBound(LineItems) <> -1 Then
Application.Range("FC_Range_Final").Cells(row_number, 1).Resize(, 3).Value = LineItems
End If
Next row_number
Close #1