VBA Loop question

vanik16

New Member
Joined
Apr 27, 2021
Messages
4
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi All,

New to VBA, but I have some VBA code to call a sub which copies, tranposes and pastes the row as a column in a different sheet.

It loops through rows 1 to 10.

The bit I'm stuck on is the syntax for .Range("B:Z") to include the X, tried things like .Range("B:Z" & X) or .Range("B" & X : "Z" & X) which don't work.

.Range("A" & X) works fine. Could anyone help with the correct syntax for this?

VBA Code:
For X = 1 To 10

Call transposeAndPasteRow(Worksheets("Sheet 1").Range("A" & X), Worksheets("Sheet 2").Range("A999999").End(xlUp))

Call transposeAndPasteRow(Worksheets("Sheet 1").Range("B1:Z1"), Worksheets("Sheet 2").Range("C999999").End(xlUp))

Next X

Thanks for the help in advance!
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
.Range("B" & X : "Z" & X) should be valid but I don't really know what you are trying to do. No idea what transposeAndPasteRow does or what it expects as an argument.

What do you mean "don't work"? What are you expecting, and what happens instead?
 
Upvote 0
Thanks for the reply,

"transposeAndPasteRow" sub just copies a row and transposes to a column as below.

I'm trying to loop through all the rows in Sheet 1, and transpose them to Sheet 2.

For the line Call transposeAndPasteRow(Worksheets("Sheet 1").Range("A" & X), Worksheets("Sheet 2").Range("A999999").End(xlUp)) it copies the cell A1 to A10 and transposes it into the same number of rows. This works as intended.

For Call transposeAndPasteRow(Worksheets("Sheet 1").Range("B1:Z1"), Worksheets("Sheet 2").Range("C999999").End(xlUp)), I'm trying to copy the entire row B1 to Z1, looping through each row to B10 to Z10. If I enter the syntax .Range("B" & X : "Z" & X) , it comes up with a Compile error: Expected list seperator or )

VBA Code:
Sub transposeAndPasteRow(rowToCopy As Range, pasteTarget As Range)

    pasteTarget.Resize(rowToCopy.Columns.Count) = Application.WorksheetFunction.Transpose(rowToCopy.Value)

End Sub
 
Upvote 0
Ah nevermind, I solved it.

I was missing a , instead of the : for .Range("B" & X , "Z" & X)
 
Upvote 0
Solution
I was missing a , instead of the : for .Range("B" & X , "Z" & X)
Note that the difference is if you use the ":", it will include ALL columns from column B to column Z.
If you use the ",", it will ONLY be columns B and Z (and not any of the columns in between).
 
Upvote 0
Sorry, syntax error. Use this

.Range("B" & X & ":Z" & X)
 
Upvote 0

Forum statistics

Threads
1,215,514
Messages
6,125,273
Members
449,219
Latest member
daynle

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