VBA Excel - 如何将命名范围的值从工作簿 B 传输到工作簿 A 中的 same/similar 命名范围?
VBA Excel - How to transfer Values of Named Ranges from Workbook B into same/similar Named Ranges in Workbook A?
提前感谢您的帮助。我的知识是初级水平。我可以阅读代码,但很难写。
此外,我认为总是有更好(更有效)的代码编写方式。
解决方案是几个objective的组合:
1. 带有指定宏的命令按钮(完成)
2.错误处理(有一点代码)
3. 确定要从中传输的第二个工作簿(有代码)
4. 将 90 多个命名范围的值复制并粘贴到工作簿 A(使用宏记录器的穴居人代码)
5.将5个名称范围的对象(图片)复制并粘贴到工作簿A中(还没到这一步)
6. 用户反馈(传输成功或传输失败并显示错误消息)
代码:(跳过objective 1)
Sub Button_Transfer_FromOlderVersion()
' Start of Error Handling
On Error GoTo Errorcatch
' Declare string variable and use current open workbook filename as value
Dim WorkbookNameNew As String
WorkbookNameNew = ThisWorkbook.Name
' Declare string variable for 2nd workbook not yet identified
Dim WorkbookNameOld As String
' Find out the name of the 2nd workbook
' Declare string variable for finding and separating the filename from the path
Dim sFileName As String
' Show the open dialog and pass the selected file name to the string variable "sFileName"
sFileName = Application.GetOpenFilename
' If the user cancels finding the workbook file then exit subroutine
If sFileName = "False" Then Exit Sub
' Troubleshooting: Show me the filename with path of Workbook B
MsgBox sFileName
' Troubleshooting: Show me the filename of Workbook A
MsgBox WorkbookNameNew
' Open Workbook B which the user just selected
Workbooks.Open Filename:=sFileName
' Separate the filename from the path for Workbook B
WorkbookNameOld = Dir(sFileName)
' Troubleshooting: Show me the filename of Workbook B
MsgBox WorkbookNameOld
' Make sure Workbook B is the active workbook
Windows(WorkbookNameOld).Activate
' Make sure the correct worksheet is active
Worksheets("WorksheetName").Activate
' Select and copy the value of the first Named Range
Range("NamedRange01").Select
Selection.Copy
' Make Workbook A the active workbook
Windows(WorkbookNameNew).Activate
' Select the corresponding Named Range in Workbook A
Range("NamedRange01").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
' User Feedback of successful transfer and name of Workbook B
MsgBox ("TRANSFER COMPLETED FROM:" & " " & WorkbookNameOld)
Exit Sub
' Finish Error Handling
Errorcatch:
MsgBox Err.Description
End Sub
如果间距、缩进和注释未针对阅读进行优化,我深表歉意。我仍在学习最佳实践。
请注意:一些名称范围拼写不同,我需要映射它们,以便 copy/paste 准确。
此外,与其使用 copy/paste,不如将所有命名范围与相关变量一起列在数组中不是更好吗?将所有值和对象复制到数组中,然后切换到工作簿 A 并粘贴所有内容不是更好吗?
再次感谢您的帮助。
您无需在复制或粘贴前激活工作簿或工作表。那只会减慢速度。此外,您可以关闭屏幕更新和计算以进一步加快速度。
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Windows(WorkbookNameOld).Worksheets("WorksheetName").Range("NamedRange01").Copy
Windows(WorkbookNameNew).ActiveSheet.Range("NamedRange01").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
提前感谢您的帮助。我的知识是初级水平。我可以阅读代码,但很难写。
此外,我认为总是有更好(更有效)的代码编写方式。
解决方案是几个objective的组合:
1. 带有指定宏的命令按钮(完成)
2.错误处理(有一点代码)
3. 确定要从中传输的第二个工作簿(有代码)
4. 将 90 多个命名范围的值复制并粘贴到工作簿 A(使用宏记录器的穴居人代码)
5.将5个名称范围的对象(图片)复制并粘贴到工作簿A中(还没到这一步)
6. 用户反馈(传输成功或传输失败并显示错误消息)
代码:(跳过objective 1)
Sub Button_Transfer_FromOlderVersion()
' Start of Error Handling
On Error GoTo Errorcatch
' Declare string variable and use current open workbook filename as value
Dim WorkbookNameNew As String
WorkbookNameNew = ThisWorkbook.Name
' Declare string variable for 2nd workbook not yet identified
Dim WorkbookNameOld As String
' Find out the name of the 2nd workbook
' Declare string variable for finding and separating the filename from the path
Dim sFileName As String
' Show the open dialog and pass the selected file name to the string variable "sFileName"
sFileName = Application.GetOpenFilename
' If the user cancels finding the workbook file then exit subroutine
If sFileName = "False" Then Exit Sub
' Troubleshooting: Show me the filename with path of Workbook B
MsgBox sFileName
' Troubleshooting: Show me the filename of Workbook A
MsgBox WorkbookNameNew
' Open Workbook B which the user just selected
Workbooks.Open Filename:=sFileName
' Separate the filename from the path for Workbook B
WorkbookNameOld = Dir(sFileName)
' Troubleshooting: Show me the filename of Workbook B
MsgBox WorkbookNameOld
' Make sure Workbook B is the active workbook
Windows(WorkbookNameOld).Activate
' Make sure the correct worksheet is active
Worksheets("WorksheetName").Activate
' Select and copy the value of the first Named Range
Range("NamedRange01").Select
Selection.Copy
' Make Workbook A the active workbook
Windows(WorkbookNameNew).Activate
' Select the corresponding Named Range in Workbook A
Range("NamedRange01").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
' User Feedback of successful transfer and name of Workbook B
MsgBox ("TRANSFER COMPLETED FROM:" & " " & WorkbookNameOld)
Exit Sub
' Finish Error Handling
Errorcatch:
MsgBox Err.Description
End Sub
如果间距、缩进和注释未针对阅读进行优化,我深表歉意。我仍在学习最佳实践。
请注意:一些名称范围拼写不同,我需要映射它们,以便 copy/paste 准确。
此外,与其使用 copy/paste,不如将所有命名范围与相关变量一起列在数组中不是更好吗?将所有值和对象复制到数组中,然后切换到工作簿 A 并粘贴所有内容不是更好吗?
再次感谢您的帮助。
您无需在复制或粘贴前激活工作簿或工作表。那只会减慢速度。此外,您可以关闭屏幕更新和计算以进一步加快速度。
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Windows(WorkbookNameOld).Worksheets("WorksheetName").Range("NamedRange01").Copy
Windows(WorkbookNameNew).ActiveSheet.Range("NamedRange01").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic