0
CSV Files我有一个奇怪的问题,打开一个CSV文件。我已经编写了一个代码来打开CSV文件并使用vba在Excel中处理它。我只能打开一个特定的CSV文件,但任何其他CSV文件,我无法打开它或在Excel中处理它我不知道为什么。为什么只有一个特定的文件正在被Excel VBA读取。下面的代码使用vba打开csv文件
Sub lithium()
Dim ws As Worksheet
Dim MyData As String, strData() As String
Dim PathInit As String
Dim i As Integer
Dim z As Long, filecount As Long
Dim l As Long
Dim LR As Long
k = 2
LR = Range("A" & Rows.Count).End(xlUp).Row
' Opening the txt file
Dim myTxt
myTxt = Application.GetOpenFilename(filefilter:="CSV Files (*.csv), *.csv", MultiSelect:=False)
Open myTxt For Binary As #1
MyData = Space$(LOF(1))
Get #1, , MyData
Close #1
strData() = Split(MyData, vbCrLf) ' Saving the entire value of the text file into the array. every element in the array in the entire row of the txt file
Dim strRow1() As String
Dim strRow2() As String
Dim strRow3() As String
Dim strRow4() As String
Dim strRow5() As String
Dim strRow6() As String
Dim nCount As Integer
nCount = 1
Dim nRowLenth As Integer
nRowLenth = UBound(strData) - LBound(strData) ' Length of the total data array
j = 3
For i = 18 To nRowLenth
strRow1() = Split(strData(i), ";")
Cells(j, 15).value = strRow1(0)
Cells(j, 16).value = strRow1(1)
If Cells(j, 16).value = "c0" Then Cells(j, 17).value = strRow1(2)
If Cells(j, 16).value = "c1" Then Cells(j, 18).value = strRow1(2)
If Cells(j, 16).value = "c4" Then Cells(j, 19).value = strRow1(2)
Cells(j, 21).value = Left(Cells(j, 17).value, 2)
Cells(j, 22).value = Left(Cells(j, 18).value, 2)
Cells(j, 23).value = Right(Left(Cells(j, 18).value, 6), 2) & Right(Left(Cells(j, 18).value, 4), 2)
Cells(j, 23).NumberFormat = "0000"
Cells(j, 24).value = Left(Cells(j, 19).value, 2)
Cells(j, 26).value = Right(Left(Cells(j, 19).value, 12), 2) & Right(Left(Cells(j, 19).value, 10), 2)
Cells(j, 27).value = Right(Left(Cells(j, 19).value, 16), 2) & Right(Left(Cells(j, 19).value, 14), 2)
If Cells(j, 16).value = "c0" Then Cells(j, 1).value = Cells(j, 15).value Else _
If Cells(j, 16).value = "c1" Then Cells(j, 1).value = Cells(j, 15).value Else _
If Cells(j, 16).value = "c4" Then Cells(j, 1).value = Cells(j, 15).value Else _
If Cells(j, 21).value = "" Then Cells(j, 2).value = "#N/A" Else Cells(j, 2).value = CLng("&H" & Cells(j, 21).value)
If Cells(j, 22).value = "" Then Cells(j, 3).value = "#N/A" Else Cells(j, 3).value = CLng("&H" & Cells(j, 22).value)
If Cells(j, 24).value = "" Then Cells(j, 4).value = "#N/A" Else Cells(j, 4).value = CLng("&H" & Cells(j, 24).value) - 40
Cells(j, 5).value = CLng("&H" & Cells(j, 25).value) - 40
If Cells(j, 23).value = "" Then Cells(j, 5).value = "#N/A" Else Cells(j, 5).value = CLng("&H" & Cells(j, 23).value) - 32768
If Cells(j, 26).value = "" Then Cells(j, 6).value = "#N/A" Else Cells(j, 6).value = CLng("&H" & Cells(j, 26).value)
If Cells(j, 27).value = "" Then Cells(j, 7).value = "#N/A" Else Cells(j, 7).value = CLng("&H" & Cells(j, 27).value)
If Cells(j, 27).value = "" Then Cells(j, 8).value = "#N/A" Else Cells(j, 8).value = Cells(j, 6).value - Cells(j, 7).value
j = j +1
Next
End Sub
,也为一个特定的文件能够正常工作,但我得到一个错误索引出的,我不知道为什么范围。如果有人可以帮助
我们也不知道。 ....请发布工作的csv文件的结构和不起作用的csv文件的详细信息。 ....我们应该猜测哪条线给你的错误? ....你的问题类似于'我有这种画笔,它可以从一罐油漆中确定颜色,但是不能从另一罐油漆中很好地绘制。你能告诉我为什么吗?' – jsotola
对不起,对于CSV文件都是类似的结构...我会上传文件... – Santhosh
只需粘贴几行到你的问题 – jsotola