VBA Copy 2 cells every n-th column and transpose ?

vadius

Board Regular
Joined
Jul 5, 2011
Messages
70
Hi there

I am trying to develop a code that select two cells (one name + one number) in one sheet (SMI_Ranking) and transpose them in a new sheet (Final_ranking). The cells I want to copy are every 4th column

From
name1/nb1...[4columns]...name2/nb2 ....[4columns].... name3/nb3

to
name1/nb1
name2/nb2
name3/nb3

Below is the code I tried to do but it is not working.

Has anyone an idea ?

Thanks!

Code:
Sub Finalranking()
Dim a, j As Integer
Dim table As Variant
'select the 2 cells every two 4 columns
For j = 5 To 121 Step 4
table(1, j) = Worksheets("SMI_Ranking").Range(Cells(1, j - 1), Cells(1, j))
'put the result in the "ranking" sheet
Worksheets.Add().Name = "Final_ranking"
Worksheets("Final_ranking").Activate
a = 1
Cells(1, a).Value = table(1, j)
a = a + 1

Next j
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Is the name and number to be transposed from two cells into one or into two cells?
 
Upvote 0
Hi

it is transpose to two separate cells

cell(A1) & Cell (B1) ...[4 column]....cell(G1) & Cell (H1)


to
cell(A1) & Cell (B1)cell(G1) & Cell (H1)

thanks
 
Upvote 0
Try this:

Code:
Sub Finalranking()
Dim a, j As Integer
Dim table(1 To 29, 1 To 2) As Variant
a = 5
For j = 1 To 29
table(j, 1) = Worksheets("Sheet1").Cells(1, a).Value
table(j, 2) = Worksheets("Sheet1").Cells(1, a + 1).Value
a = a + 4
Next j
'put the result in the "ranking" sheet
Worksheets.Add().Name = "Final_ranking"
Worksheets("Final_ranking").Activate
For j = LBound(table) To UBound(table)
Cells(j, 1).Value = table(j, 1)
Cells(j, 2).Value = table(j, 2)
Next j
End Sub

To calculate the table size I took the numbers from your original post:

(121 - 5)/4 = 29

This might be wrong but you can change it :)
 
Upvote 0
it's nearly great

below exactly what I was looking for

Thanks for your help !

Code:
Sub Finalranking()
Dim a, j As Integer
Dim table(1 To 29, 1 To 2) As Variant
a = 4
For j = 1 To 29
table(j, 1) = Worksheets("SMI_Ranking").Cells(1, a).Value
table(j, 2) = Worksheets("SMI_Ranking").Cells(1, a + 1).Value
a = a + 4
Next j
'put the result in the "ranking" sheet
Worksheets.Add().Name = "Final_ranking"
Worksheets("Final_ranking").Activate
For j = LBound(table) To UBound(table)
Cells(j, 2).Value = table(j, 2)
Cells(j, 1).Value = table(j, 1)
Next j
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,792
Members
452,942
Latest member
VijayNewtoExcel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top