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
Do you have formulae in those cells?
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
No, they should be empty. Every time the code has run and entered in data for processing it is running a "Clear all" macro, deleting everything in these cell
 
Upvote 0
If it's deleting everything in those cells, then what you said in post#10 makes no sense.
 
Upvote 0
Not isolated, but it is a multi-step process. Data is being written into a excel sheet using Python, and then multiple VBA codes are being run in an order, processing that code. This is a loop, so when the processing is done, all data (apart from the result that is copied into a new sheet, is cleared. Then new data is being written in by Python.

The problem is that when data is written in to Excel the second time (or any time following the first), it is supposed to be written in to a range starting at A1, but it does not. It start writing in data in the first row that was empty in the first loop. This causes the next macro to fail as it is difficult to forsee where the first row is.
 
Upvote 0
Either the cells are not empty, or there is a problem with your python code.
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,094
Latest member
teemeren

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