【发布时间】:2020-03-18 08:41:38
【问题描述】:
我有许多不同的单元格(每个单元格都分配了一个唯一的名称),它们位于名为“Master”的工作簿中包含的各种工作表中。要复制的源单元格是通过将它们的工作表和范围名称与目标工作簿中包含绘图代码的单元格的内容相匹配来选择的。以下宏专门将单元格“X6”定义为要复制到调用宏的目标工作表(“绘图”)中的单元格的起始单元格:
Option Explicit
Sub Copy_DOD() 'Copy specified named range
Dim dws, sws As Worksheet ' Destination and source worksheets
Dim swb As Workbook ' Source workbook
Dim DrawingCode, swsName As String
Set dws = Worksheets("Drawing")
Set swb = Workbooks("Master.xlsm")
With dws
Application.ScreenUpdating = False
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Get Drawing Code
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
DrawingCode = dws.Range("DrawingCode")
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Determine Source Worksheet - DrawingCode up to character "x"
' e.g code of 1234x56 produces worksheet name "1234"
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
swsName = Left(DrawingCode, (InStr(DrawingCode, "x")) - 1)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copy Cells to Destination sheet
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
swb.Worksheets(swsName).Range(DrawingCode).Copy Range("X6")
End With
End Sub
我不想使用预定义的单元格(“X6”)作为要复制到的目标起始单元格,而是让用户指定起始单元格,而不是使用 InputBox。以下成功从用户那里获取指定的目标单元格,但在粘贴范围时失败。我知道我必须错误地定义粘贴,但无法弄清楚它需要什么。欢迎任何指导!
Option Explicit
Sub Copy_DOD() 'Copy specified named range
Dim dws, sws As Worksheet ' Destination and source worksheets
Dim swb As Workbook ' Source workbook
Dim DrawingCode, swsName As String
Dim DockTopLeftCell As Range
Dim dTopLeftRow, dTopLeftColumn As Integer
Set dws = Worksheets("Drawing")
Set swb = Workbooks("Master.xlsm")
With dws
Application.ScreenUpdating = False
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Get the top left cell for the dock drawing and determine row and column values
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
On Error Resume Next
Application.DisplayAlerts = False
Set DockTopLeftCell = (Application.InputBox("Enter the cell to be the top left corner of the dock drawing (DO NOT GO LESS THAN CELL X6)", Type:=8))
Application.DisplayAlerts = True
On Error GoTo 0
If DockTopLeftCell Is Nothing Then Exit Sub
dTopLeftRow = DockTopLeftCell.Row ' Set dock drawing row origin
dTopLeftColumn = DockTopLeftCell.Column ' Set dock drawing column origin
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Get Drawing Code
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
DrawingCode = dws.Range("DrawingCode")
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Determine Source Worksheet - DrawingCode up to character "x"
' e.g code of 1234x56 produces worksheet name "1234"
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
swsName = Left(DrawingCode, (InStr(DrawingCode, "x")) - 1)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copy Cells to Destination sheet
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
swb.Worksheets(swsName).Range(DrawingCode).Copy Range(DockTopLeftCell)
'swb.Worksheets(swsName).Range(DrawingCode).Copy Range("X6")
End With
End Sub
【问题讨论】: