First you will need to copy each of your range one by one.
If your ranges B4:B48, C4:C48,D4:D48...GT4:GT48
follow a rule like always one column right, then you can use a loop going through all columns. B
is column no 2 and GT
is column no 202. So you can loop like
Dim iCol As Long
For iCol = 2 To 202
'your code here
Next iCol
The ranges eg B4:B48
can then be dynamically written as
ws.Range(ws.Cells(4, iCol), ws.Cells(48, iCol))
wich means if iCol = 2
then Cells(4, iCol)
is B4
and Cells(48, iCol)
is B48
so it represents the range between those 2 cells. In the next iteration Next iCol
will increase iCol
by one so the same line represents column C now.
Note that in the code above ws
represents your worksheet, you should specify that in the beginning so you can use that shortcut ws
:
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("YourSheetName")
You will also need to find the last used cell in column B to determine the place where you need to paste your range. A good way to do this is:
Dim LastUsedRow As Long
LastUsedRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
So now your next free row in column B is LastUsedRow + 1
and this is where you should paste.
Give it a try on your own, do some research if you get stuck. I gave you pretty much of what you need. The rest should be really basic stuff you can easily figure out by reading some basic tutorials or google.
Two last things that will prevent you from running into common issues:
I recommend always to activate Option Explicit
: In the VBA editor go to Tools › Options › Require Variable Declaration and declare all your variables properly.
You might benefit from reading How to avoid using Select in Excel VBA. Using .Select
or .Activate
should be avoided at any cost. It slows down everything a lot and your code will run into issues easily soon or later.