求助!VBA编程合并单元格
发布网友
发布时间:2022-05-07 10:22
我来回答
共1个回答
热心网友
时间:2023-10-25 21:35
代码如下:
Private Sub txt_read()
Dim tmp As String, str
Dim i As Long, arr()
i = 0
Open "D:\dzh.txt" For Input As #1 '
'打开文件作为数据输入用,D:\dzh.txt改成你自己的文件地址和名称即可
Do While Not EOF(1)
Line Input #1, tmp '从已打开的顺序文件中读出一行并将它分配给 String 变量
str = Split(tmp, Chr(9))
If Len(str(0)) > 0 Then
i = i + 1
ReDim Preserve arr(1 To i)
arr(i) = str(2) & "," & str(4) & "," & str(7)
End If
Loop
Close #1
'输出
For i = 1 To UBound(arr)
ActiveSheet.Cells(i, 1).Resize(1, 3) = Split(arr(i), ",")
Next
End Sub
也可以用录制宏的方式解决:
Sub 宏1()
'D:\dzh.txt改成你自己的文件路径和名字
With ActiveSheet.QueryTables.Add(Connection:="TEXT;D:\dzh.txt", Destination _
:=Range("$A$1"))
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 936
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(9, 9, 1, 9, 1, 9, 9, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
热心网友
时间:2023-10-25 21:35
代码如下:
Private Sub txt_read()
Dim tmp As String, str
Dim i As Long, arr()
i = 0
Open "D:\dzh.txt" For Input As #1 '
'打开文件作为数据输入用,D:\dzh.txt改成你自己的文件地址和名称即可
Do While Not EOF(1)
Line Input #1, tmp '从已打开的顺序文件中读出一行并将它分配给 String 变量
str = Split(tmp, Chr(9))
If Len(str(0)) > 0 Then
i = i + 1
ReDim Preserve arr(1 To i)
arr(i) = str(2) & "," & str(4) & "," & str(7)
End If
Loop
Close #1
'输出
For i = 1 To UBound(arr)
ActiveSheet.Cells(i, 1).Resize(1, 3) = Split(arr(i), ",")
Next
End Sub
也可以用录制宏的方式解决:
Sub 宏1()
'D:\dzh.txt改成你自己的文件路径和名字
With ActiveSheet.QueryTables.Add(Connection:="TEXT;D:\dzh.txt", Destination _
:=Range("$A$1"))
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 936
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(9, 9, 1, 9, 1, 9, 9, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub