Rows to Column - Macro

wardex

New Member
Joined
Sep 21, 2023
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi guys, can you help me. I need a macro for this problem. Thanks in advance! :)
 

Attachments

  • capture-20230921-141631.png
    capture-20230921-141631.png
    4.1 KB · Views: 19
  • capture-20230921-141643.png
    capture-20230921-141643.png
    4.8 KB · Views: 20

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try this
VBA Code:
Sub Arrange()

Dim mA As Long, nA As Long, mB As Long, nB As Long, idx As Long
Dim eRow As Long, eCol As Long
Dim LastCell As Range
Dim wsA As Worksheet, wsB As Worksheet

Set wsA = ActiveWorkbook.Sheets("Sheet1")
Set wsB = ActiveWorkbook.Sheets("Sheet2")
Set LastCell = wsA.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)

eRow = LastCell.Row
eCol = LastCell.Column

For mA = 1 To eRow
    If Not wsA.Cells(mA, 1) = 0 Then
        idx = wsA.Cells(mA, 1)
        nB = 0
    End If
    For nA = 1 To eCol
        If Not mB = idx Then mB = mB + 1
        If Not Len(wsA.Cells(mA, nA)) = 0 Then
            If mB = idx Then nB = nB + 1
            wsB.Cells(mB, nB) = wsA.Cells(mA, nA)
        End If
    Next
Next

End Sub
 
Upvote 0
Try this
VBA Code:
Sub Arrange()

Dim mA As Long, nA As Long, mB As Long, nB As Long, idx As Long
Dim eRow As Long, eCol As Long
Dim LastCell As Range
Dim wsA As Worksheet, wsB As Worksheet

Set wsA = ActiveWorkbook.Sheets("Sheet1")
Set wsB = ActiveWorkbook.Sheets("Sheet2")
Set LastCell = wsA.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)

eRow = LastCell.Row
eCol = LastCell.Column

For mA = 1 To eRow
    If Not wsA.Cells(mA, 1) = 0 Then
        idx = wsA.Cells(mA, 1)
        nB = 0
    End If
    For nA = 1 To eCol
        If Not mB = idx Then mB = mB + 1
        If Not Len(wsA.Cells(mA, nA)) = 0 Then
            If mB = idx Then nB = nB + 1
            wsB.Cells(mB, nB) = wsA.Cells(mA, nA)
        End If
    Next
Next

End Sub
thanks for the help sir. An error prompt after running the given code.
 

Attachments

  • capture-20230921-161312.png
    capture-20230921-161312.png
    60 KB · Views: 9
Upvote 0
thanks for the help sir. An error prompt after running the given code.
:unsure:

It ran just fine on my PC. I'm still on Excel 2016 but it should not be a problem I suppose.

If it stuck there, does this mean you have yet to have anything written in Sheet2?
 
Upvote 0
I see that you run code before I modified
Line
Dim RowA As Long, RowB As Long, ColA As Long, ColB As Long
is not required because not used but that will not cause the problem.
 
Upvote 0
Sheet2 is empty sir. I created a new sheet and renamed it to Sheet2
 

Attachments

  • capture-20230921-162434.png
    capture-20230921-162434.png
    40.8 KB · Views: 7
  • capture-20230921-162418.png
    capture-20230921-162418.png
    45.2 KB · Views: 8
Upvote 0
My Sheet1
Book1
ABCDE
11abcd
2efgh
3ijkl
42aabbccdd
5eeffgghh
63aaabbbcccddd
7eeefffggghhh
8iiijjjkkklll
9mmmnnnoooppp
Sheet1


My Sheet2
Book1
ABCDEFGHIJKLMNOPQ
11abcdefghijkl
22aabbccddeeffgghh
33aaabbbcccdddeeefffggghhhiiijjjkkklllmmmnnnoooppp
4
Sheet2
 
Upvote 0
Try create a new workbook with Sheet1 and Sheet2 and run the code again
 
Upvote 0
Sir I got it :))

Just figured out that if you rename the new added sheet it will, but in the VBA it will not change at all.

Thanks again! God bless!
 

Attachments

  • capture-20230921-163342.png
    capture-20230921-163342.png
    59.9 KB · Views: 13
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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