[英]How to fix - Excel VBA copy and paste values - ThisWorkbook error
I'm setting up a copy/paste macro for my financial model. 我正在为我的财务模型设置一个复制/粘贴宏。
While it is currently running just fine, I am having trouble making if I save a new version of the model. 虽然它目前运行得很好,但如果我保存新版本的模型,我将无法正常运行。 The model has basic tabs I need to paste over as well as several tabs that operate on toggles and cycle through 10-15 sheets, so I have pasted an example of one sheet and one cycle in the code. 该模型具有我需要粘贴的基本选项卡以及几个在切换操作的选项卡,并循环显示10-15页,因此我在代码中粘贴了一个工作表和一个循环的示例。
Copy_PasteWorkbook.xlsm is a blank excel document utilized as the location of the pasting. Copy_PasteWorkbook.xlsm是一个空白的Excel文档,用作粘贴的位置。
Right now it will only work if I define the name of the file its running in and I cannot get Workbook(ThisWorkbook) to work in the code. 现在它只有在我定义运行的文件的名称时才能工作,我无法在代码中使用Workbook(ThisWorkbook) 。
Application.ScreenUpdating = False
Application.Calculation = xlCalculationAutomatic
Application.Calculate
Windows("Copy_PasteWorkbook.xlsx").Activate
ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = "Model"
Windows("XYZ_v1.xlsm").Activate
Sheets("Value_Summary_Sheet").Select
Range(Cells(1, 1), Cells.SpecialCells(xlCellTypeLastCell)).Select
Selection.Copy
Windows("Copy_PasteWorkbook.xlsx").Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=8
Sheets("Model").Select
Sheets("Model").Name = Cells(1, 3).Value
Range("A1").Select
Application.CutCopyMode = False
Windows("XYZ_v1.xlsm").Activate
Range("A1").Select
Sheets("Inputs").Range("Selected_Toggle_Number").Value = 1
Do Until Sheets("Inputs").Range("Selected_Toggle_Number").Value > Sheets("Inputs").Range("Total_Toggles").Value
Application.Calculate
Windows("Copy_PasteWorkbook.xlsx").Activate
ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = "Model"
Windows("XYZ_v1.xlsm").Activate
Sheets("Financial Models").Select
Range(Cells(1, 1), Cells.SpecialCells(xlCellTypeLastCell)).Select
Selection.Copy
Windows("Copy_PasteWorkbook.xlsx").Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=8
Sheets("Model").Select
Sheets("Model").Name = Cells(1, 3).Value
Range("A1").Select
Application.CutCopyMode = False
Windows("XYZ_v1.xlsm").Activate
Range("A1").Select
Sheets("Inputs").Range("Selected_Toggle_Number").Value = Sheets("Inputs").Range("Selected_Toggle_Number").Value + 1
DoEvents
Loop
Sheets("Inputs").Range("Selected_Toggle_Number").Value = 1
Sheets("Inputs").Select
Range("A1").Select
Application.CommandBars("Clipboard").Visible = True
On Error Resume Next 'incase clipboard IS empty
Application.CommandBars("Clipboard").FindControl(ID:=3634).Execute
Application.CommandBars("Clipboard").Visible = False
Application.ScreenUpdating = True
End Sub
Ideally I would like to sub out the Windows("XYZ_V1.xlsm").Activate code with ThisWorkbook so that it can function whenever changes are made to the model. 理想情况下,我想分出Windows(“XYZ_V1.xlsm”)。使用ThisWorkbook激活代码,以便只要对模型进行更改就可以运行。 Right now if I update and save a new version I then have to update the code throughout for the new name. 现在,如果我更新并保存新版本,那么我必须更新新名称的代码。
This answer took me a while, but since you are new I thought it was better to show you how and where your code was changed. 这个答案花了我一些时间,但由于你是新手,我认为最好向你展示代码的更改方式和位置。
Once you delete all the comments the code is way shorter: 删除所有注释后,代码会缩短:
Sub Test()
Dim wb As Workbook, wbPaste As Workbook, wsSumary As Worksheet, wsPaste As Worksheet, wsInputs As Worksheet, _
wsFinMod As Worksheet
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
End With
'First you need to avoid using select, in that matter you need worksheet and workbook variables
Set wb = ThisWorkbook 'the workbook containing the code
Set wbPaste = Workbooks("Copy_PasteWorkbook.xlsx") 'the workbook where you are going to paste
With wb
Set wsSumary = .Sheets("Value_Summary_Sheet")
Set wsInputs = .Sheets("Inputs")
Set wsFinMod = .Sheets("Financial Models")
End With
'The code above sets your worksheets and workbooks on the macro file
With wbPaste
Set wsPaste = .Sheets.Add(after:=.Sheets(.Sheets.Count))
End With
'the code above sets the paste workbook, adds a sheet and names it "Model", also equals to this:
' Windows("Copy_PasteWorkbook.xlsx").Activate
' ActiveWorkbook.Sheets.Add after:=Worksheets(Worksheets.Count)
' ActiveSheet.Name = "Model"
With wsPaste
wsSumary.UsedRange.Copy
.Range("A1").PasteSpecial xlPasteValues '?¿?¿ I assume from your code you want to paste it there
.Range("A1").PasteSpecial xlPasteFormats
.Name = .Cells(1, 3)
End With
'The code above equals to this:
' Windows("XYZ_v1.xlsm").Activate
' Sheets("Value_Summary_Sheet").Select
' Range(Cells(1, 1), Cells.SpecialCells(xlCellTypeLastCell)).Select
' Selection.Copy
' Windows("Copy_PasteWorkbook.xlsx").Activate
' Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
' :=False, Transpose:=False
' Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
' SkipBlanks:=False, Transpose:=False
' Selection.PasteSpecial Paste:=8
' Sheets("Model").Select
' Sheets("Model").Name = Cells(1, 3).Value
' Range("A1").Select
' Application.CutCopyMode = False
Dim i As Long, x As Long, wsTemp As Worksheet
x = wsInputs.Range("Total_Toggles").Value
For i = 1 To x
Application.Calculate
With wbPaste
Set wsTemp = .Sheets.Add(after:=.Sheets(.Sheets.Count))
End With
With wsTemp
wsFinMod.UsedRange.Copy
.Range("A1").PasteSpecial xlPasteValues
.Range("A1").PasteSpecial xlPasteFormats
.Name = .Cells(1, 3)
End With
Next i
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.CutCopyMode = False
End With
'The code above equals to this:
' Windows("XYZ_v1.xlsm").Activate
' Range("A1").Select
' Sheets("Inputs").Range("Selected_Toggle_Number").Value = 1
' Do Until Sheets("Inputs").Range("Selected_Toggle_Number").Value > Sheets("Inputs").Range("Total_Toggles").Value
' Application.Calculate
' Windows("Copy_PasteWorkbook.xlsx").Activate
' ActiveWorkbook.Sheets.Add after:=Worksheets(Worksheets.Count)
' ActiveSheet.Name = "Model"
' Windows("XYZ_v1.xlsm").Activate
' Sheets("Financial Models").Select
' Range(Cells(1, 1), Cells.SpecialCells(xlCellTypeLastCell)).Select
' Selection.Copy
' Windows("Copy_PasteWorkbook.xlsx").Activate
' Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
' :=False, Transpose:=False
' Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
' SkipBlanks:=False, Transpose:=False
' Selection.PasteSpecial Paste:=8
' Sheets("Model").Select
' Sheets("Model").Name = Cells(1, 3).Value
' Range("A1").Select
' Application.CutCopyMode = False
' Windows("XYZ_v1.xlsm").Activate
' Range("A1").Select
' Sheets("Inputs").Range("Selected_Toggle_Number").Value = Sheets("Inputs").Range("Selected_Toggle_Number").Value + 1
' DoEvents
' Loop
'
' Sheets("Inputs").Range("Selected_Toggle_Number").Value = 1
' Sheets("Inputs").Select
' Range("A1").Select
' Application.CommandBars("Clipboard").Visible = True
' On Error Resume Next 'incase clipboard IS empty
' Application.CommandBars("Clipboard").FindControl(ID:=3634).Execute
' Application.CommandBars("Clipboard").Visible = False
' Application.ScreenUpdating = True
End Sub
This is how your code would look like: 这就是你的代码的样子:
Sub Test()
Dim wb As Workbook, wbPaste As Workbook, wsSumary As Worksheet, wsPaste As Worksheet, wsInputs As Worksheet, _
wsFinMod As Worksheet
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
End With
'First you need to avoid using select, in that matter you need worksheet and workbook variables
Set wb = ThisWorkbook 'the workbook containing the code
Set wbPaste = Workbooks("Copy_PasteWorkbook.xlsx") 'the workbook where you are going to paste
With wb
Set wsSumary = .Sheets("Value_Summary_Sheet")
Set wsInputs = .Sheets("Inputs")
Set wsFinMod = .Sheets("Financial Models")
End With
With wbPaste
Set wsPaste = .Sheets.Add(after:=.Sheets(.Sheets.Count))
End With
With wsPaste
wsSumary.UsedRange.Copy
.Range("A1").PasteSpecial xlPasteValues '?¿?¿ I assume from your code you want to paste it there
.Range("A1").PasteSpecial xlPasteFormats
.Name = .Cells(1, 3)
End With
Dim i As Long, x As Long, wsTemp As Worksheet
x = wsInputs.Range("Total_Toggles").Value
For i = 1 To x
Application.Calculate
With wbPaste
Set wsTemp = .Sheets.Add(after:=.Sheets(.Sheets.Count))
End With
With wsTemp
wsFinMod.UsedRange.Copy
.Range("A1").PasteSpecial xlPasteValues
.Range("A1").PasteSpecial xlPasteFormats
.Name = .Cells(1, 3)
End With
Next i
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.CutCopyMode = False
End With
End Sub
This is an example of how to rewrite the first part of your code. 这是如何重写代码的第一部分的示例。 It is not a total answer, but can help you understand and rewrite your code. 它不是一个完整的答案,但可以帮助您理解和重写您的代码。 You don't need all the Activate
or Select
, you can do a Google search on how to avoid them 您不需要全部Activate
或Select
,您可以在Google上搜索如何避免它们
Dim Destwb As Workbook, Srcewb As Workbook
Set Destwb = Workbooks("Copy_PasteWorkbook")
Set Srcewb = Workbooks("XYZ_v1") 'or "ThisWorkbook" if the source wb is the wb with your macro
Destwb.Sheets.Add(after:=Sheets(Sheets.Count)).Name = "Model" 'add a new worksheet and rename it
'copy the range from the source workbook
Srcewb.Sheets("Value_Summary_Sheet").Range(Cells(1, 1), Cells.SpecialCells(xlCellTypeLastCell)).Copy
'paste the copied range to the new worksheet in the destination workbook
Destwb.Sheets("Model").Cells(1, 1).PasteSpecial Paste:=xlPasteValues 'all other paramaters are optional
'change the name of the new worksheet again(you could have set the name to the cell value the first time
Destwb.Sheets("Model").Name = Cells(1, 3).Value
'Goto a specific cell in the scource workbook
Application.Goto Srcewb.Range("A1"), Scroll = True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.