Calling on a Range in VBA

前端 未结 1 1540
情书的邮戳
情书的邮戳 2021-01-24 12:18

What am I doing wrong with my code below? I am trying to name a range of data that is highlighted in excel and be able to call it in the VBA code and paste it, transpose it, etc

相关标签:
1条回答
  • 2021-01-24 12:56

    Edit: after reading the comments: Here is a simpler way to copy a range of cells, then pasting special (values) to somewhere else. I obtained this code my recording a macro entirely.

    Sub Macro1()
        Range("A1:C3").Select
        Selection.Copy
        Cells(10,"D").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
    End Sub
    

    If you meant to copy the D10 range to whatever activecell is, then Change

    Cells(10, "D").Select
    

    to

    Cells(10, "D").copy
    

    You also need to specify what do you want to SPECIALLY PASTE (values? format?) So your full code should be like

    Sub routine()
    
        Dim rng As Range
        Set rng = ActiveCell.CurrentRegion
    
        Cells(10, "D").Copy
        rng.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False   'This will only paste values
    
        Application.CutCopyMode = False
    End Sub
    
    0 讨论(0)
提交回复
热议问题