'Increment column reference in formula

I have data which I have to enter into a table that has the following layout:

enter image description here

I need to store it in another worksheet that has this layout:

enter image description here

I am trying to create a macro that will accomplish 3 things:

  1. Copy the contents in the first table to a column in another worksheet

  2. clear the original table data (but not the headers q, w, e, etc)

  3. Increment the column reference so that the next time the macro is ran, it will copy the data into column C in the second table, and the third time into column D, and so on.

This is what I have tried:

Line 1: Sheets("sheet2").Range("B1").Value = Sheets("sheet1").Range("A5").Value

Line 2: Sheets("sheet1").Range("A5:D6").ClearContents

In order to accomplish 3), I have to manually change "A5" to "B5" in the code, and to do so for each cell (of which there are about 60). Is there a way to automate this?



Solution 1:[1]

You can use excel's offset() function.

To change A5 to B5 you can do

For x = 1 to N
Sheets("sheet2").Range("B1").Value = Sheets("sheet1").Range("A5").offset(0,x).Value

If you want to do it only once then you will not need the for loop.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 abhikush