gabriel
gabriel

Reputation: 399

Calling on a Range in VBA

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. somewhere else but it keeps giving me an error.

Sub routine()

Dim rng As Range
Set rng = ActiveCell.CurrentRegion

Cells(10, "D").Select
rng.PasteSpecial

End Sub

I also notice that when type "ActiveCell." and hit space i get a drop down of options. however the case isn't true when i type "Cells(1,1)." and space. Why is that? Thank you guys for your help!

Upvotes: 1

Views: 5102

Answers (1)

Ahmad
Ahmad

Reputation: 12737

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

Upvotes: 1

Related Questions