vba - .txt 用于分隔工作表
问题描述
我正在尝试使用以下代码将多个 .txt 导入工作簿中单独的单独工作表中。在所有工作表中,它都无法用空格分隔最后一行,并且从工作表 2 开始,它也无法复制 .txt 文件的第一行。所有的.txt。文件格式完全相同。任何帮助表示赞赏。
Sub CombineTextFiles()
Dim FilesToOpen
Dim x As Integer
Dim wkbAll As Workbook
Dim wkbTemp As Workbook
Dim sDelimiter As String
On Error GoTo ErrHandler
Application.ScreenUpdating = False
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Text Files (*.txt), *.txt", _
MultiSelect:=True, Title:="Text Files to Open")
If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If
x = 1
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
wkbTemp.Sheets(1).Copy
Set wkbAll = ActiveWorkbook
wkbTemp.Close (False)
wkbAll.Worksheets(x).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False, _
Comma:=False, Space:=True, Other:=False, OtherChar:="|"
Dim lastrowA As Long
Dim lastrowB As Long
Dim sheetname As String
With ActiveSheet
lastrowA = .Cells(.Rows.Count, "A").End(xlUp).Row
lastrowB = .Cells(.Rows.Count, "B").End(xlUp).Row
sheetname = ActiveSheet.Name
Range("a1").EntireColumn.Insert
Range("a1").Value = sheetname
Range("a2" & ":a" & lastrowB).Value = Range("a1")
Range("a1").EntireRow.Insert
End With
x = x + 1
While x <= UBound(FilesToOpen)
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
With wkbAll
wkbTemp.Sheets(1).Move After:=.Sheets(.Sheets.Count)
.Worksheets(x).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False, _
Comma:=False, Space:=True, Other:=False
End With
With ActiveSheet
lastrowA = .Cells(.Rows.Count, "A").End(xlUp).Row
lastrowB = .Cells(.Rows.Count, "B").End(xlUp).Row
sheetname = ActiveSheet.Name
Range("a1").Value = sheetname
Range("a2" & ":a" & lastrowB).Value = Range("a1")
Range("a1").EntireRow.Insert
End With
x = x + 1
Wend
ExitHandler:
Application.ScreenUpdating = True
Set wkbAll = Nothing
Set wkbTemp = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub
解决方案
如果你做一个最小的、完整的、可验证的例子,你可能会自己发现错误。但是,根据您对第一行的描述,我想问题出在这里:
With ActiveSheet
lastrowA = .Cells(.Rows.Count, "A").End(xlUp).Row
lastrowB = .Cells(.Rows.Count, "B").End(xlUp).Row
sheetname = ActiveSheet.Name
Range("a1").EntireColumn.Insert
Range("a1").Value = sheetname
Range("a2" & ":a" & lastrowB).Value = Range("a1")
Range("a1").EntireRow.Insert
End With
您需要像这样声明范围:
With ActiveSheet
lastrowA = .Cells(.Rows.Count, "A").End(xlUp).Row
lastrowB = .Cells(.Rows.Count, "B").End(xlUp).Row
sheetname = ActiveSheet.Name
.Range("a1").EntireColumn.Insert
.Range("a1").Value = sheetname
.Range("a2" & ":a" & lastrowB).Value = .Range("a1")
.Range("a1").EntireRow.Insert
End With
看看这些点,它们会有所作为。如果代码位于工作表中,则范围会将它们所在的工作表作为父工作表。
推荐阅读
- python - Mix of line and scatter plots from pandas dataframe in a single plot using the tick frequency of the first plot only
- react-native - 反应导航,重置堆栈而不重定向
- azure-devops - 本地 Azure 代理忽略路径中的 chromedriver
- python - 重塑Conv2D的嵌入层输出?
- visual-studio-code - 如果我使用公钥进行身份验证,为什么要询问密码 VS Code Remote SSH 插件?
- ruby-on-rails - 如何在Ruby中动态获取Class,就像我们可以调用send来动态触发一个方法一样?
- sql - 如何知道 SQL Server 中数据库的增长
- javascript - 在Javascript中应用多个改变相同数组的函数的最优雅的方法是什么?
- python - 尽管创建了模型,但 Django 编程错误关系不存在
- mysql - 在mysql联合查询中插入序列号