希望这应该是直截了当但我看不出我错过了什么。在清除“原始”工作表上的A列内容后,我在“粘贴特殊”行上收到运行时错误。有人可以帮助我吗?
Sub BACSConversion2()
Dim MyNewBook As String
Dim MySaveFile As String
Dim fileToOpen As Variant
Dim fileName As String
Dim sheetName As String
'Turn off display alerts
Application.DisplayAlerts = False
'Turn off screen updates
Application.ScreenUpdating = False
'This calls the routine to get the text file data
'Call CopyTxtFile
'Opens the folder to location to select txt file
fileToOpen = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If fileToOpen <> False Then
Workbooks.OpenText fileName:=fileToOpen, _
DataType:=xlDelimited, Tab:=True
End If
'Creates the file name based on txt file name
fileName = Mid(fileToOpen, InStrRev(fileToOpen, "\") + 1)
'Creates the sheet name based on the active txt file
sheetName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4)
'Save active file as...
ActiveWorkbook.SaveAs ("S:\Accounts (New)\Management Information
(Analysis)\Phil Hanmore - Analysis\Neil Test\Test Destination Folder\" &
fileName & ".CSV")
'Selects all data in column A and copies to clipboard
Range("A1", Range("A1").End(xlDown)).Select
Selection.Copy
'Closes the workbook
'ActiveWorkbook.Close
'Open the original document where the BACS file is located
Workbooks.Open "S:\Accounts (New)\Management Information (Analysis)\Phil
Hanmore - Analysis\Neil Test\copy of bacs conversation calc.xlsx"
'Selects the worksheet called "Original"
Sheets("Original").Select
Range("A:A").ClearContents
'Paste selected values from previous sheet
Selection.PasteSpecial Paste:=xlPasteValues
'Selects appropriate worksheet - Non-MyPayFINAL
Sheets("Non-MyPay FINAL").Select
'Selects all data in column A and copies to clipboard
Range("A1", Range("A1").End(xlDown)).Select
Selection.Copy
'Add a new workbook
Workbooks.Add
'Paste selected values from previous sheet
Selection.PasteSpecial Paste:=xlPasteValues
'Build SaveAs file name
MySaveFile = Format(Now(), "DDMMYYYY") & "NonMyPayFINAL" & ".CSV"
'Save template file as...
ActiveWorkbook.SaveAs ("S:\Accounts (New)\Management Information
(Analysis)\Phil Hanmore - Analysis\Neil Test\" & MySaveFile)
'Close the new saved file
ActiveWorkbook.Close
'Selects appropriate worksheet - MyPayFINAL
Sheets("MyPay FINAL").Select
'Selects all data in column A and copies to clipboard
Range("A1", Range("A1").End(xlDown)).Select
Selection.Copy
'Add a new workbook
Workbooks.Add
'Paste selected values from previous sheet
Selection.PasteSpecial Paste:=xlPasteValues
'Build SaveAs file name
MySaveFile = Format(Now(), "DDMMYYYY") & "MyPayFINAL" & ".CSV"
'Save template file as...
ActiveWorkbook.SaveAs ("S:\Accounts (New)\Management Information
(Analysis)\Phil Hanmore - Analysis\Neil Test\" & MySaveFile)
'Close the new saved file
ActiveWorkbook.Close
'Close original source workbook
Workbooks("bacs conversation calc").Close
'Turn on display alerts
Application.DisplayAlerts = True
'Turn on screen updates
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:0)
此代码实际上将复制范围分配给变量并使用它。因为您正在使用各种工作簿和工作表,所以最好为它们分配变量并直接引用它们,以便毫无疑问您要引用哪个文件。我不是100%肯定你在做什么,所以没有那样做。
Sub BACSConversion2()
Dim MyNewBook As String
Dim MySaveFile As String
Dim fileToOpen As Variant
Dim fileName As String
Dim sheetName As String
Dim rCopy As Range
'Turn off display alerts
Application.DisplayAlerts = False
'Turn off screen updates
Application.ScreenUpdating = False
'This calls the routine to get the text file data
'Call CopyTxtFile
'Opens the folder to location to select txt file
fileToOpen = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If fileToOpen <> False Then
Workbooks.OpenText fileName:=fileToOpen, _
DataType:=xlDelimited, Tab:=True
End If
'Creates the file name based on txt file name
fileName = Mid(fileToOpen, InStrRev(fileToOpen, "\") + 1)
'Creates the sheet name based on the active txt file
sheetName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4)
'Save active file as...
ActiveWorkbook.SaveAs ("S:\Accounts (New)\Management Information (Analysis)\Phil Hanmore - Analysis\Neil Test\Test Destination Folder\" & _
fileName & ".CSV")
'Selects all data in column A and copies to clipboard
Set rCopy = Range("A1", Range("A1").End(xlDown))
'Closes the workbook
'ActiveWorkbook.Close
'Open the original document where the BACS file is located
Workbooks.Open "S:\Accounts (New)\Management Information (Analysis)\Phil Hanmore - Analysis\Neil Test\copy of bacs conversation calc.xlsx"
'Selects the worksheet called "Original"
Sheets("Original").Range("A:A").ClearContents
'Paste selected values from previous sheet
rCopy.Copy
Sheets("Original").Range("A1").PasteSpecial Paste:=xlPasteValues
'Selects appropriate worksheet - Non-MyPayFINAL
Sheets("Non-MyPay FINAL").Select
'Selects all data in column A and copies to clipboard
Range("A1", Range("A1").End(xlDown)).Select
Selection.Copy
'Add a new workbook
Workbooks.Add
'Paste selected values from previous sheet
Selection.PasteSpecial Paste:=xlPasteValues
'Build SaveAs file name
MySaveFile = Format(Now(), "DDMMYYYY") & "NonMyPayFINAL" & ".CSV"
'Save template file as...
ActiveWorkbook.SaveAs ("S:\Accounts (New)\Management Information (Analysis)\Phil Hanmore - Analysis\Neil Test\" & MySaveFile)
'Close the new saved file
ActiveWorkbook.Close
'Selects appropriate worksheet - MyPayFINAL
Sheets("MyPay FINAL").Select
'Selects all data in column A and copies to clipboard
Range("A1", Range("A1").End(xlDown)).Select
Selection.Copy
'Add a new workbook
Workbooks.Add
'Paste selected values from previous sheet
Selection.PasteSpecial Paste:=xlPasteValues
'Build SaveAs file name
MySaveFile = Format(Now(), "DDMMYYYY") & "MyPayFINAL" & ".CSV"
'Save template file as...
ActiveWorkbook.SaveAs ("S:\Accounts (New)\Management Information (Analysis)\Phil Hanmore - Analysis\Neil Test\" & MySaveFile)
'Close the new saved file
ActiveWorkbook.Close
'Close original source workbook
Workbooks("bacs conversation calc").Close
'Turn on display alerts
Application.DisplayAlerts = True
'Turn on screen updates
Application.ScreenUpdating = True
End Sub