【问题标题】:copy cell values of named range in one sheet to another sheet starting at a cell defined by the user将一个工作表中命名范围的单元格值复制到另一个工作表,从用户定义的单元格开始
【发布时间】: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

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    我已经审查、更正并评论了您的代码。这是我的工作成果。

    Sub Copy_DOD_2()  'Copy specified named range
    
        Dim sWb As Workbook                         ' Source workbook
        ' if no data type is prescribed VBA assumes Variant
        ' VBA does NOT assume the data type specified for the
        ' last item in a line.
        Dim dWs As Worksheet, sWs As Worksheet      ' Destination and source worksheets
        Dim DrawingCode As String, sWsName As String
        Dim DockTopLeftCell As Range
    '    Dim dTopLeftRow As Long, dTopLeftColumn As Long
    
        Set sWb = Workbooks("Master.xlsm")
        Set dWs = Worksheets("Drawing")         ' this Ws is in the ActiveWorkbook
                                                ' maybe "Master", perhaps another
    
        Application.ScreenUpdating = False
    
        With dWs
    
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            ' Get the top left cell for the dock drawing and determine row and column values
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            ' Application Alerts provide useful help in this case.
            On Error Resume Next
            Set DockTopLeftCell = Application.InputBox( _
                                 "Enter the cell to be the top left corner " & _
                                 "of the dock drawing" & vbCr & _
                                 "(DO NOT GO LESS THAN CELL X6)", _
                                 "Dock drawing cell", "X6", Type:=8)
            If DockTopLeftCell Is Nothing Then Exit Sub
    
            On Error GoTo 0
    '            dTopLeftRow = DockTopLeftCell.Row            ' Set dock drawing row origin
    '            dTopLeftColumn = DockTopLeftCell.Column      ' Set dock drawing column origin
    
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            ' Get Drawing Code
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            DrawingCode = dWs.Range("DrawingCode").Value
    
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            ' 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 DockTopLeftCell
            'swb.Worksheets(swsName).Range(DrawingCode).Copy Range("X6")
    
        End With
    
        Application.ScreenUpdating = True
    End Sub
    

    错误似乎是DockTopLeftCell 已经是一个范围。因此Range(DockTopLeftCell) 必须失败。但是,我想提醒您注意指定范围的位置。 Type 8 InputBox 大概定义了当前 ActiveSheet 上的范围。您的代码中没有证据表明可能是哪张表。因此,您可能会对副本的最终位置感到惊讶。

    我可能会获取指定单元格的地址并在我想要的工作表上使用它,例如Set DockTopLeftCell = MySheet.Range(DockTopLeftCell.Address)。那么地址是在哪张纸上创建的就无关紧要了。

    【讨论】:

    • 许多 thaks - 认为这将是一些简单而明显的事情。就是看不出来。另外,感谢您提供有关粘贴的提示。宏是通过工作表中需要粘贴的按钮启动的,所以我不确定我需要担心,但为了确定,我需要添加什么?
    • 请注意我在回答中添加的段落。
    猜你喜欢
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    相关资源
    最近更新 更多