[英]loading data from file to excel
I have this job I need to do in VBA, and if I open the IDE and run the code from there all behaves as expected, loads the data from txt file to a new temp workbook, copy and paste to my worksheet, close the temp workbook. 我有这项工作需要在VBA中完成,如果我打开IDE并从那里运行代码,则所有行为均符合预期,将数据从txt文件加载到新的临时工作簿,复制并粘贴到我的工作表中,关闭临时文件工作簿。
But when running using a shortcut, ie Ctr+Sft+L, it loads the data to the temp workbook, activates it and stops execution there... Doesn't paste the data back to the worksheet. 但是,当使用快捷方式(例如Ctr + Sft + L)运行时,它将数据加载到临时工作簿中,然后将其激活并停止执行...不会将数据粘贴回工作表中。 How can I fix this?
我怎样才能解决这个问题?
Here's the code: 这是代码:
Sub Load_Text()
Dim wsSheet As Worksheet
InsertWorkSheet ("Data")
Set wsSheet = ThisWorkbook.Sheets("Data")
Workbooks.OpenText Filename:="C:\Documents and Settings\w059\Desktop\20121107_Report.txt", _
Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, Comma:=False, _
Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), _
Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1)), TrailingMinusNumbers:=True
ActiveWorkbook.Worksheets(1).Cells.Copy wsSheet.Range("A1")
ActiveWorkbook.Close True
End Sub
Public Sub InsertWorkSheet(name As String)
Dim wsSheet As Worksheet
On Error Resume Next
Set wsSheet = ThisWorkbook.Sheets(name)
If wsSheet Is Nothing Then
ThisWorkbook.Sheets.Add After:=Sheets(Sheets.Count)
ThisWorkbook.Sheets(Sheets.Count).name = name
End If
End Sub
I was getting a wrong reference to the ActiveWorkbook. 我对ActiveWorkbook的引用有误。 This corrected it:
这纠正了它:
Replaced 已更换
ActiveWorkbook.Worksheets(1).Cells.Copy wsSheet.Range("A1")
ActiveWorkbook.Close True
With 用
Dim wb As Workbook
Set wb = ActiveWorkbook
wb.Worksheets(1).Cells.Copy wsSheet.Range("A1")
wb.Close True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.