Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping and indexing in VBA

Tags:

excel

vba

I have experience with C/C++ but I am new to VBA and Excel.
What I have is:

Range("A7:L7").Select
Selection.Copy
Range("R18").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=True
    'One column copied

The problem is that I want to go through an entire range of cells (everything from A6:L6 all the way up to A41:41).

I tried looking into For loops but I don't understand exactly how indexing works when selecting ranges. Here's what I have written so far:

pasteLocation = 6
For i = 6 To 41
   Range("A" & i:"L" & i).Select

    Selection.Copy
    Range("R" & pasteLocation).Select '+12 every time to this counter

    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True

    pasteLocation = pasteLocation + 12 'want to move down by 12 every time

Next i

Clearly I'm doing something wrong because I get "Compile Error: Expected: list separator or )"

Can anyone explain how indexing with VBA works and what I'm doing wrong?

like image 574
user2850099 Avatar asked Feb 02 '26 21:02

user2850099


1 Answers

You have a typo:

Range("A" & i:"L" & i).Select

should be

Range("A" & i & ":L" & i).Select
like image 150
Heinzi Avatar answered Feb 04 '26 09:02

Heinzi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!