Vba 粘贴范围类的特殊方法失败-错误104
希望这应该是直截了当的,但我看不出我错过了什么。在清除“原始”工作表上A列的内容之后,我在粘贴专行上得到了运行时错误。有人能帮我吗Vba 粘贴范围类的特殊方法失败-错误104,vba,excel,Vba,Excel,希望这应该是直截了当的,但我看不出我错过了什么。在清除“原始”工作表上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 Appli
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
Sub-BACSConversion2()
将MyNewBook设置为字符串
将MySaveFile设置为字符串
Dim fileToOpen作为变体
将文件名设置为字符串
将sheetName设置为字符串
'关闭显示警报
Application.DisplayAlerts=False
'关闭屏幕更新
Application.ScreenUpdating=False
'这将调用例程以获取文本文件数据
'调用CopyTxtFile
'将文件夹打开到要选择txt文件的位置
fileToOpen=Application.GetOpenFilename(“文本文件(*.txt),*.txt”)
如果fileToOpen为False,则
Workbooks.OpenText文件名:=fileToOpen_
数据类型:=xlDelimited,制表符:=True
如果结束
'根据txt文件名创建文件名
fileName=Mid(fileToOpen,InStrRev(fileToOpen,“\”)+1)
'根据激活的txt文件创建图纸名称
sheetName=Left(ActiveWorkbook.Name,Len(ActiveWorkbook.Name)-4)
'将活动文件另存为。。。
ActiveWorkbook.SaveAs(“S:\Accounts(New)\n管理信息
(分析)\Phil Hanmore-分析\Neil测试\测试目标文件夹\“&
文件名“.CSV”)
'选择列A中的所有数据并复制到剪贴板
范围(“A1”,范围(“A1”)。结束(xlDown))。选择
选择,复制
'关闭工作簿
'ActiveWorkbook.Close
'打开BACS文件所在的原始文档
工作簿。打开“S:\Accounts(新建)\Management Information(分析)\Phil”
Hanmore-分析\Neil测试\bacs对话calc.xlsx的副本”
'选择名为“原始”的工作表
图纸(“原件”)。选择
范围(“A:A”)。ClearContents
'粘贴上一工作表中的选定值
Selection.Paste特殊粘贴:=xlPasteValues
'选择适当的工作表-非MyPayFINAL
工作表(“非MyPay最终版”)。选择
'选择列A中的所有数据并复制到剪贴板
范围(“A1”,范围(“A1”)。结束(xlDown))。选择
选择,复制
'添加新工作簿
工作手册。添加
'粘贴上一工作表中的选定值
Selection.Paste特殊粘贴:=xlPasteValues
'生成另存为文件名
MySaveFile=Format(Now(),“DDMMYYYY”)和“NonMyPayFINAL”和“.CSV”
'将模板文件另存为。。。
ActiveWorkbook.SaveAs(“S:\Accounts(New)\n管理信息
(分析)\Phil Hanmore-Analysis\Neil Test\“&MySaveFile)
'关闭新保存的文件
活动工作簿。关闭
'选择适当的工作表-MyPayFINAL
工作表(“MyPay FINAL”)。选择
'选择列A中的所有数据并复制到剪贴板
范围(“A1”,范围(“A1”)。结束(xlDown))。选择
选择,复制
'添加新工作簿
工作手册。添加
'粘贴上一工作表中的选定值
Selection.Paste特殊粘贴:=xlPasteValues
'生成另存为文件名
MySaveFile=Format(Now(),“DDMMYYYY”)&“MyPayFINAL”和“.CSV”
'将模板文件另存为。。。
ActiveWorkbook.SaveAs(“S:\Accounts(New)\n管理信息
(分析)\Phil Hanmore-Analysis\Neil Test\“&MySaveFile)
'关闭新保存的文件
活动工作簿。关闭
'关闭原始源工作簿
工作手册(“bacs对话计算”)。关闭
'打开显示警报
Application.DisplayAlerts=True
'打开屏幕更新
Application.ScreenUpdating=True
端接头
此代码实际上将复制范围分配给变量并使用该变量。由于您使用的是各种工作簿和工作表,因此最好将变量分配给它们并直接引用它们,这样您就可以毫无疑问地引用哪个文件。我不是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
Sub-BACSConversion2()
将MyNewBook设置为字符串
将MySaveFile设置为字符串
Dim fileToOpen作为变体
将文件名设置为字符串
将sheetName设置为字符串
变暗复制为范围
'关闭显示警报
Application.DisplayAlerts=False
'关闭屏幕更新
Application.ScreenUpdating=False
'这将调用例程以获取文本文件数据
'调用CopyTxtFile
'将文件夹打开到要选择txt文件的位置
fileToOpen=Application.GetOpenFilename(“文本文件(*.txt),*.txt”)
如果fileToOpen为False,则
Workbooks.OpenText文件名:=fileToOpen_
数据类型:=xlDelimited,制表符:=True
如果结束
'根据txt文件名创建文件名
fileName=Mid(fileToOpen,InStrRev(fileToOpen,“\”)+1)
'根据激活的txt文件创建图纸名称
sheetName=Left(ActiveWorkbook.Name,Len(ActiveWorkbook.Name)-4)
'将活动文件另存为。。。
ActiveWorkbook.SaveAs(“S:\Accounts(New)\Management Information(Analysis)\Phil Hanmore-Analysis\Neil Test\Test Destination Folder\”和_
文件名“.CSV”)
'选择列A中的所有数据并复制到剪贴板
设置rCopy=范围(“A1”,范围(“A1”)。结束(xlDown))
'关闭工作簿
'ActiveWorkbook.Close
'打开BACS文件所在的原始文档
工作簿。打开“S:\Accounts(新建)\Management Information(分析)\Phil Hanmore-Analysis\Neil Test\bacs conversation calc.xlsx的副本”
'选择名为“原始”的工作表
纸张(“原件”)。范围(“A:A”)。清晰内容
'粘贴上一工作表中的选定值
复印件,复印件
纸张(“原始”)。范围(“A1”)。粘贴特殊粘贴:=XLPasteValue
'选择适当的工作表-非MyPayFINAL
工作表(“非MyPay最终版”)。选择
'选择列A中的所有数据并复制到剪贴板
范围(“A1”,范围(“A1”)。结束(xlDown))。选择
选择,复制
'添加新工作簿
工作手册。添加
'粘贴上一工作表中的选定值
Selection.Paste特殊粘贴:=xlPasteValues
'生成另存为文件名
MySaveFile=Format(Now(),“DDMMYYYY”)和“NonMyPayFINAL”和“.CSV”
'将模板文件另存为。。。
ActiveWorkbook.SaveAs(“S:\Accounts(New)\Management Information(Analysis)\Phil Hanmore-Analysis\Neil Test\”&MySaveFile)
'关闭新保存的文件
活动工作簿。关闭
'选择适当的工作表-MyPayFINAL
工作表(“MyPay FINAL”)。选择
'选择列A中的所有数据并复制到剪贴板
范围(“A1”,范围(“A1”)。结束(xlDown))。选择
选择,复制