Excel 2013 VBA:下标超出范围(错误9)
所以我有这个代码:Excel 2013 VBA:下标超出范围(错误9),excel,excel-2013,vba,Excel,Excel 2013,Vba,所以我有这个代码: Sub CopyItems() Dim Source As String Dim Target As String 'Dim SourceSheet As String 'Dim TargetSheet As String Source = "Source.xlsm" Target = "needChange.xlsm" 'SourceSheet = "Sprint backlog" 'TargetShee
Sub CopyItems()
Dim Source As String
Dim Target As String
'Dim SourceSheet As String
'Dim TargetSheet As String
Source = "Source.xlsm"
Target = "needChange.xlsm"
'SourceSheet = "Sprint backlog"
'TargetSheet = "Sheet1"
Workbooks(Source).Sheets("Sprint backlog").Range("B6:B15").Copy
Workbooks(Target).Sheets("Sheet1").Range("A14:A23").Paste '<-ERROR here
End Sub
并且用现在注释掉的内容修改了代码
我怀疑宏无法访问目标文件(needChange.xlsm),因为它找不到目标文件或无法访问目标文件,因此返回问题,但我不知道如何用代码修复它
如果有帮助的话,在运行宏时,我可以打开并访问代码中的两个工作簿
我向你求助
非常感谢。
致以最诚挚的问候。这比预期的要棘手。我从这个网页上借了很多东西 我必须在图纸上添加参考,以便复制和粘贴工作 发布的代码要求打开两个工作簿,但是如果您给wbTarget一个路径名,就可以打开它。在这种情况下,您可以注释掉-或-后面出现的两行 代码还可以保存和关闭目标工作簿
Sub CopyOpenItems()
'
' CopyOpenItems Macro
' Copy open items to sheet.
'
' Keyboard Shortcut: Ctrl+Shift+O
'
Dim wbTarget As Workbook 'workbook where the data is to be pasted
Dim wbThis As Workbook 'workbook from where the data is to copied
Dim strName As String 'name of the source sheet/ target workbook
'set to the current active workbook (the source book)
Set wbThis = ActiveWorkbook
'get the active sheetname of the book
strName = ActiveSheet.Name
'open a workbook that has same name as the sheet name
'Set wbTarget = Workbooks.Open("C:\YourPath\needChange.xlsm")
' - OR -
Workbooks("needChange.xlsm").Activate
Set wbTarget = ActiveWorkbook
'select cell A1 on the target book
'wbTarget.Range("A1").Select
'clear existing values form target book
'wbTarget.Range("A1:M51").ClearContents
'activate the source book
wbThis.Activate
'clear any thing on clipboard to maximize available memory
Application.CutCopyMode = False
'copy the range from source book
wbThis.Sheets("Sprint backlog").Range("B6:B15").Copy
'paste the data on the target book
wbTarget.Sheets("Sheet1").Range("A14").PasteSpecial
'clear any thing on clipboard to maximize available memory
Application.CutCopyMode = False
'save the target book
'wbTarget.Save
'close the workbook
'wbTarget.Close
'activate the source book again
wbThis.Activate
'clear memory
Set wbTarget = Nothing
Set wbThis = Nothing
End Sub
如果只复制值(而不复制公式、图片、格式),则
工作簿(目标).Sheet1.Range(“A14:A23”).value=工作簿(源).Sheets(“Sprint backlog”).Range(“B6:B15”).value
很好
(在同一代码行中,只有此处窗口的大小使其看起来像2)
对于超过个值:
workbook(Source).Sheets(“Sprint backlog”).Range(“B6:B15”).Copy_
工作簿(目标).Sheet1.Range(“A14:A23”)
(2行)
注意:\uu
表示以下行应位于同一行上,且仅用于更轻松地读取代码。(您在第二个代码中犯了这个错误)
注2:range().paste
不存在,只有sheets().paste
或range().pastespecial
注3:当然,所有工作簿和工作表必须存在,并且与使用的工作簿和工作表具有相同的确切名称
注4:仅当两个工作簿都已打开时,复制/粘贴才有效。对于关闭的文件,情况就不同了
简而言之,您犯了两个错误:
\uu
和范围().Range(“A14”)
将根据源的大小和形状进行调整;您不需要.Range(“A14:A23”)
。看看这个[为了指出错误,您可以使用中间对象和/或在调试时使用即时窗口。例如将wb设置为工作簿,将wb设置为工作簿(目标),将sh设置为工作表,将sh设置为工作表(“Sheet1”)
,等等。Rory-我检查了三遍,表名是相同的:(Jeeped-谢谢,我会记住这一点。dev1998-我看到了,问题是..当这个代码是最终的,并且将被使用时,路径可能不一样。嗨-我使用了你的代码并对其进行了一些修改-但是现在一切都很好。谢谢:)无需激活或选择工作簿,它们只需在复制时打开即可。
Sub CopyOpenItems()
'
' CopyOpenItems Macro
' Copy open items to sheet.
'
' Keyboard Shortcut: Ctrl+Shift+O
'
Dim wbTarget As Workbook 'workbook where the data is to be pasted
Dim wbThis As Workbook 'workbook from where the data is to copied
Dim strName As String 'name of the source sheet/ target workbook
'set to the current active workbook (the source book)
Set wbThis = ActiveWorkbook
'get the active sheetname of the book
strName = ActiveSheet.Name
'open a workbook that has same name as the sheet name
'Set wbTarget = Workbooks.Open("C:\YourPath\needChange.xlsm")
' - OR -
Workbooks("needChange.xlsm").Activate
Set wbTarget = ActiveWorkbook
'select cell A1 on the target book
'wbTarget.Range("A1").Select
'clear existing values form target book
'wbTarget.Range("A1:M51").ClearContents
'activate the source book
wbThis.Activate
'clear any thing on clipboard to maximize available memory
Application.CutCopyMode = False
'copy the range from source book
wbThis.Sheets("Sprint backlog").Range("B6:B15").Copy
'paste the data on the target book
wbTarget.Sheets("Sheet1").Range("A14").PasteSpecial
'clear any thing on clipboard to maximize available memory
Application.CutCopyMode = False
'save the target book
'wbTarget.Save
'close the workbook
'wbTarget.Close
'activate the source book again
wbThis.Activate
'clear memory
Set wbTarget = Nothing
Set wbThis = Nothing
End Sub