What am I missing in this code?

Brutusar

Board Regular
Joined
Nov 23, 2019
Messages
166
Office Version
  1. 365
Platform
  1. Windows
I am using the code pasted in below to transpose a range. An example range is shown in row 133 in the minisheet below. The code produces the result shown in row 7-15.
I am trying to "expand" the code so I can adjust the number of columns to be transposed, from minimum 2 cols to maximum 10. Any good ideas on how to do that best? ( I can't get it to work at all)

This is the code I am using:

VBA Code:
Sub Transpose()
    Dim Ary As Variant, Nary As Variant
    Dim r As Long, nr As Long, c As Long
   
    Ary = Sheets("Sheet1").Range("A1").CurrentRegion.Value2
    ReDim Nary(1 To UBound(Ary) * 5, 1 To 4)
    For r = 1 To UBound(Ary)
        For c = 5 To UBound(Ary, 2)
            nr = nr + 1
            Nary(nr, 1) = Ary(r, 1)
            Nary(nr, 2) = Ary(r, 2)
            Nary(nr, 3) = Ary(r, c)
        Next c
    Next r
    Sheets("Sheet1").Range("M1").Resize(nr, 3).Value = Nary
End Sub


Example.xlsx
G
7
Sheet1
 

Attachments

  • Example.PNG
    Example.PNG
    29.5 KB · Views: 14

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
That code will work for as many columns as exist, assuming there are no completely blank columns in the data.
 
Upvote 0
Hi,

No, if I am using for instance 5 columns of numbers, as in the example, it only make 2 new rows, and uses the numbers in col E, F and G, skipping the numbers in col C and D. The picture below shows the result after I ran the code on the last range in the example
 

Attachments

  • Example 2.PNG
    Example 2.PNG
    10.6 KB · Views: 11
Upvote 0
If you need it to start in col 3 of the array use
Excel Formula:
For c = 3 To UBound(Ary, 2)
 
Upvote 0
If you need it to start in col 3 of the array use
Excel Formula:
For c = 3 To UBound(Ary, 2)
Then the code run in to an error at: Nary(nr, 1) = Ary(r, 1) (Subscript out of range)
 
Upvote 0
In that case please post sample data using the XL2BB add-in.
 
Upvote 0
Maybe...

VBA Code:
Sub aTest()
    Dim DataArr As Variant, ArrRes As Variant
    Dim r As Long, nr As Long, c As Long
  
    DataArr = Sheets("Sheet1").Range("A1").CurrentRegion.Value2
    ReDim ArrRes(1 To UBound(DataArr, 1) * 3, 1 To 3)
    
    For r = 1 To UBound(DataArr, 1)
        For c = UBound(DataArr, 2) - 2 To UBound(DataArr, 2)
            nr = nr + 1
            ArrRes(nr, 1) = DataArr(r, 1)
            ArrRes(nr, 2) = DataArr(r, 2)
            ArrRes(nr, 3) = DataArr(r, c)
        Next c
    Next r
    Sheets("Sheet1").Range("M1").Resize(nr, 3).Value = ArrRes
End Sub

Hope this helps

M.
 
Upvote 0
Maybe...

VBA Code:
Sub aTest()
    Dim DataArr As Variant, ArrRes As Variant
    Dim r As Long, nr As Long, c As Long
 
    DataArr = Sheets("Sheet1").Range("A1").CurrentRegion.Value2
    ReDim ArrRes(1 To UBound(DataArr, 1) * 3, 1 To 3)
   
    For r = 1 To UBound(DataArr, 1)
        For c = UBound(DataArr, 2) - 2 To UBound(DataArr, 2)
            nr = nr + 1
            ArrRes(nr, 1) = DataArr(r, 1)
            ArrRes(nr, 2) = DataArr(r, 2)
            ArrRes(nr, 3) = DataArr(r, c)
        Next c
    Next r
    Sheets("Sheet1").Range("M1").Resize(nr, 3).Value = ArrRes
End Sub

Hope this helps

M.
Hi, still get a error message, no idea why. It is two columns with text and then 3-5 colums with numbers. When I tested it now it was text in col A and B, and numbers in col C, D and E.
 

Attachments

  • Error msg.PNG
    Error msg.PNG
    20.1 KB · Views: 9
Upvote 0
Hi, still get a error message, no idea why. It is two columns with text and then 3-5 colums with numbers. When I tested it now it was text in col A and B, and numbers in col C, D and E.

The code worked perfectly for me

Test 1
Data in A1:E3 (with 3 numbers)

Result
Pasta1
MNO
1New YorkLos Angeles450
2New YorkLos Angeles900
3New YorkLos Angeles900
4OrlandoHouston1610
5OrlandoHouston2220
6OrlandoHouston2220
7MiamiLas Vegas2300
8MiamiLas Vegas2700
9MiamiLas Vegas2700
Sheet1


Test 2 (with 5 numbers)
Data in A1:G3 (with 5 numbers)

Result
Pasta1
MNO
1New YorkLos Angeles900
2New YorkLos Angeles1100
3New YorkLos Angeles1700
4OrlandoHouston2220
5OrlandoHouston2300
6OrlandoHouston2500
7MiamiLas Vegas2700
8MiamiLas Vegas2900
9MiamiLas Vegas3000
Sheet1


M.
 
Upvote 0
I think I have an issue with Excel. When cells shows as empty, they are not really empty, and maybe that causes the problem. If I am automating data entry into Excel, and has set the code to write data into "first empty row", it may start entering data into for instance row 34, even if there are no visible data in the rows before. This causes the code to brake. If I pause the code at that point, manually delete the rows over the first entered row, and then continue to run the code, it works fine.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,195
Members
449,072
Latest member
DW Draft

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