Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 使用VBA打开多个文本文件_Excel_Vba_File_Text - Fatal编程技术网

Excel 使用VBA打开多个文本文件

Excel 使用VBA打开多个文本文件,excel,vba,file,text,Excel,Vba,File,Text,大家好,我的VBA代码有一个小错误。我想打开多个文本文件并将其解析到我的excel。我可以选择多个文件,但只有一个文件得到解析,我不知道为什么有人可以帮我这个 Sub lithium() Dim MyData As String, strData() As String Dim PathInit As String Dim i As Integer Dim n As Long Dim z As Long, filecount As Long ' Opening the txt file

大家好,我的VBA代码有一个小错误。我想打开多个文本文件并将其解析到我的excel。我可以选择多个文件,但只有一个文件得到解析,我不知道为什么有人可以帮我这个

Sub lithium()
Dim MyData As String, strData() As String
Dim PathInit As String
Dim i As Integer
Dim n As Long
 Dim z As Long, filecount As Long
' Opening the txt file

  Dim myTxt
  myTxt = Application.GetOpenFilename("Text Files,*.txt", , , , True)

On Error Resume Next
filecount = UBound(myTxt)
On Error GoTo 0

If filecount = 0 Then MsgBox "No text file selected. Exiting now.", _
vbExclamation: Exit Sub

 For n = LBound(myTxt) To filecount
Open myTxt(n) 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

    '~~> Do stuffs here for text parsing...



 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
' Dim row_number As Integer
 'row_number = 1
 nCount = 1
 Dim nRowLenth As Integer
 nRowLenth = UBound(strData) - LBound(strData) ' Length of the total data array
 j = 3

For i = 5 To nRowLenth


    If nCount Mod 2 <> 0 Then

        strRow1() = Split(strData(i), ";")
        strRow2() = Split(strData(i + 1), ";")
        strRow3() = Split(strData(i + 2), ";")
        strRow4() = Split(strData(i + 3), ";")
        strRow5() = Split(strData(i + 4), ";")
        strRow6() = Split(strData(i + 5), ";")




          Cells(j, 1).Value = strRow1(0)
          Cells(j, 2).Value = Mid(strRow1(2), 3, Len(strRow1(2)))
          Cells(j, 3).Value = CLng("&H" & Mid(strRow2(2), 3, Len(strRow2(2))))
          Cells(j, 4).Value = CLng("&H" & Mid(strRow3(2), 3, Len(strRow3(2))))
          Cells(j, 5).Value = CLng("&H" & Mid(strRow4(2), 3, Len(strRow4(2))))
          Cells(j, 6).Value = CLng("&H" & Mid(strRow5(2), 3, Len(strRow5(2))))
          Cells(j, 7).Value = Mid(strRow6(2), 3, Len(strRow6(2)))

        If i + 5 = nRowLenth Then
            Exit For
        Else
            i = i + 5

        End If
    End If

    If nCount Mod 2 = 0 Then

        strRow1() = Split(strData(i), ";")
        strRow2() = Split(strData(i + 1), ";")
        strRow3() = Split(strData(i + 2), ";")
        Cells(j, 1).Value = strRow1(0)
        Cells(j, 2).Value = "#N/A"
        Cells(j, 3).Value = CLng("&H" & Mid(strRow1(2), 3, Len(strRow1(2))))
        Cells(j, 4).Value = CLng("&H" & Mid(strRow2(2), 3, Len(strRow2(2))))
        Cells(j, 5).Value = CLng("&H" & Mid(strRow3(2), 3, Len(strRow3(2))))
        Cells(j, 6).Value = "#N/A"
        Cells(j, 7).Value = "#N/A"

        If i + 2 = nRowLenth Then
            Exit For
        Else
            i = i + 2

        End If
    End If

 j = j + 1
  nCount = nCount + 1
Next i

'time transformation

Dim l As Long
Dim LR As Long
k = 3
LR = Range("A" & Rows.Count).End(xlUp).Row

 For l = 3 To LR

 Range("H" & k).Value = Val(Left(Right(Range("A" & l).Value, 10), 2)) + Val(Right(Left(Right(Range("A" & l).Value, 10), 5), 2)) / 60 + Val(Right(Right(Range("A" & l).Value, 10), 4)) / 3600

  k = k + 1

 Next l
 Next
  End Sub
亚锂()
将MyData设置为字符串,将strData()设置为字符串
Dim PathInit作为字符串
作为整数的Dim i
长
将z变长,将filecount变长
'打开txt文件
Dim myTxt
myTxt=Application.GetOpenFilename(“文本文件,*.txt”、True)
出错时继续下一步
filecount=UBound(myTxt)
错误转到0
如果filecount=0,则MsgBox“未选择文本文件。正在退出。”_
感叹号:退出子系统
对于n=LBound(myTxt)到filecount
打开二进制文件的myTxt(n)作为#1
MyData=空间$(LOF(1))
获取#1,MyData
关闭#1
strData()=拆分(MyData,vbCrLf)
'将文本文件的整个值保存到数组中。txt文件整行中数组中的每个元素
“~~~>在此处进行文本分析。。。
Dim strRow1()作为字符串
Dim strRow2()作为字符串
Dim strRow3()作为字符串
Dim strRow4()作为字符串
Dim strRow5()作为字符串
Dim strRow6()作为字符串
Dim nCount为整数
'将行数变为整数
'行数=1
n计数=1
Dim nRowLenth作为整数
nRowLenth=UBound(strData)-LBound(strData)'总数据数组的长度
j=3
对于i=5到nRowLenth
如果计算Mod 2 0,则
strow1()=拆分(strData(i),“;”)
strRow2()=拆分(strData(i+1),“;”)
strow3()=拆分(strData(i+2),“;”)
strRow4()=拆分(strData(i+3),“;”)
strRow5()=拆分(strData(i+4),“;”)
strRow6()=拆分(strData(i+5),“;”)
单元格(j,1)。值=strow1(0)
单元格(j,2)。值=中间(strRow1(2),3,Len(strRow1(2)))
单元格(j,3)。值=CLng(“&H”和Mid(strRow2(2),3,Len(strRow2(2)))
单元格(j,4)。值=CLng(“&H”和Mid(strow3(2),3,Len(strow3(2)))
单元格(j,5).Value=CLng(“&H”和Mid(strRow4(2),3,Len(strRow4(2)))
单元格(j,6)。值=CLng(“&H”和Mid(strRow5(2),3,Len(strRow5(2)))
单元格(j,7)。值=中间(strRow6(2),3,Len(strRow6(2)))
如果i+5=nRowLenth,则
退出
其他的
i=i+5
如果结束
如果结束
如果nCount Mod 2=0,则
strow1()=拆分(strData(i),“;”)
strRow2()=拆分(strData(i+1),“;”)
strow3()=拆分(strData(i+2),“;”)
单元格(j,1)。值=strow1(0)
单元格(j,2)。值=“#N/A”
单元格(j,3)。值=CLng(“&H”和Mid(strRow1(2),3,Len(strRow1(2)))
单元格(j,4)。值=CLng(“&H”和Mid(strRow2(2),3,Len(strRow2(2)))
单元格(j,5).Value=CLng(“&H”和Mid(strow3(2),3,Len(strow3(2)))
单元格(j,6)。Value=“#N/A”
单元格(j,7).Value=“#N/A”
如果i+2=nRowLenth,则
退出
其他的
i=i+2
如果结束
如果结束
j=j+1
nCount=nCount+1
接下来我
时间变换
我和你一样长
变暗LR为长
k=3
LR=范围(“A”和Rows.Count).End(xlUp).Row
对于l=3至LR
范围(“H”和k).Value=Val(左(右)(范围(“A”)和l.Value,10),2))+Val(右(左(右)(范围(“A”)和l.Value,10),5),2))/60+Val(右(范围(“A”)和l.Value,10),4))/3600
k=k+1
下一个l
下一个
端接头
试试这个:

从代码中删除以下行

j = 3
将此行添加到顶部(声明)


strData()=Split(MyData,vbCrLf)
将文件保存到数组中。在哪里将该数组写入excel文件?在我看来,似乎您只是用每个新文件覆盖数组。是的,我将数组写入excel文件请向我们展示将该数组写入excel文件的代码。我们如何不覆盖所有文本文件中的数据并将其解析到excel工作表中?我已将解析数组并将其写入excel工作表的代码张贴在此处擅长
Dim j As Integer
j = 3