Transpose multiple rows and columns into one column with ignoring blanks

parankush

New Member
Joined
Jun 11, 2020
Messages
36
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
  6. 2011
  7. 2010
  8. 2007
  9. 2003 or older
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
i want to Transpose multiple rows and columns into one column with ignoring blanks. I have a large amount of data. But i can provide with a sample.Is there any VBA code i can use for it.
1591894818027.png
 
A detail emerged, use this code:

VBA Code:
Sub Transpose_2()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long
  a = Sheets("Sheet1").Range("A1", Sheets("Sheet1").Range("A1").SpecialCells(xlLastCell)).Value2
  ReDim b(1 To UBound(a, 1) * UBound(a, 2), 1 To 1)
  For i = 1 To UBound(a, 1)
    For j = 1 To UBound(a, 2)
      If a(i, j) <> "" Then
        k = k + 1
        b(k, 1) = a(i, j)
      End If
    Next
  Next
  Sheets("Sheet2").Range("A1").Resize(k).Value = b
End Sub


How can add this VBA code into function kindly help me with that
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
into function kindly
What do you mean by function?

HOW TO INSTALL MACROs
------------------------------------
If you are new to macros, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. To use the macro, go back to the worksheet with your data on it and press ALT+F8, select the macro name (Transpose_2) from the list that appears and click the Run button. The macro will execute and perform the action(s) you asked for. If you will need to do this again in this same workbook, and if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "Yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0
T
A detail emerged, use this code:

VBA Code:
Sub Transpose_2()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long
  a = Sheets("Sheet1").Range("A1", Sheets("Sheet1").Range("A1").SpecialCells(xlLastCell)).Value2
  ReDim b(1 To UBound(a, 1) * UBound(a, 2), 1 To 1)
  For i = 1 To UBound(a, 1)
    For j = 1 To UBound(a, 2)
      If a(i, j) <> "" Then
        k = k + 1
        b(k, 1) = a(i, j)
      End If
    Next
  Next
  Sheets("Sheet2").Range("A1").Resize(k).Value = b
End Sub

The code you gave worked. I have copied it in a module and saved It as Macro-Enabled Worksheet. I Paste the Data in sheet 1 and Get the output in sheet 2. But after using it for some 15-20 times. It gives error "Out of Memory".
 
Upvote 0
It gives error "Out of Memory".

You could comment the following:
  1. In which line of the macro does the error send you?
  2. Are the data the same or are they increasing?
  3. How many rows with data do you have?
  4. How many columns with data do you have?

After answering the above, you could try the following:

VBA Code:
Sub Transpose_2()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long
  a = Sheets("Sheet1").Range("A1", Sheets("Sheet1").Range("A1").SpecialCells(xlLastCell)).Value2
  MsgBox "Rows : " & UBound(a, 1) & " Cols: " & UBound(a, 2) & vbCr & "Press Enter to continue"
  ReDim b(1 To UBound(a, 1) * UBound(a, 2), 1 To 1)
  For i = 1 To UBound(a, 1)
    For j = 1 To UBound(a, 2)
      If a(i, j) <> "" Then
        k = k + 1
        b(k, 1) = a(i, j)
      End If
    Next
  Next
  Sheets("Sheet2").Range("A1").Resize(k).Value = b
  Erase a, b
End Sub
 
Upvote 0
You could comment the following:
  1. In which line of the macro does the error send you?
  2. Are the data the same or are they increasing?
  3. How many rows with data do you have?
  4. How many columns with data do you have?

After answering the above, you could try the following:

VBA Code:
Sub Transpose_2()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long
  a = Sheets("Sheet1").Range("A1", Sheets("Sheet1").Range("A1").SpecialCells(xlLastCell)).Value2
  MsgBox "Rows : " & UBound(a, 1) & " Cols: " & UBound(a, 2) & vbCr & "Press Enter to continue"
  ReDim b(1 To UBound(a, 1) * UBound(a, 2), 1 To 1)
  For i = 1 To UBound(a, 1)
    For j = 1 To UBound(a, 2)
      If a(i, j) <> "" Then
        k = k + 1
        b(k, 1) = a(i, j)
      End If
    Next
  Next
  Sheets("Sheet2").Range("A1").Resize(k).Value = b
  Erase a, b
End Sub


1.)In 4th line"(a = Sheets("Sheet1").Range("A1", Sheets("Sheet1").Range("A1").SpecialCells(xlLastCell)).Value2)
2.)They are both Increasing or decreasing.
3.) Not more than Row 1,00,000.
4.)Not more than column DZ.
 
Upvote 0
Also try the following:

VBA Code:
Sub Transpose_2()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long
  Dim lr As Long, lc As Long
  '
  With Sheets("Sheet1")
    lr = .Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
    lc = .Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
    a = .Range("A1", .Cells(lr, lc)).Value2
  End With
  MsgBox "Rows : " & UBound(a, 1) & " Cols: " & UBound(a, 2) & vbCr & "Press Enter to continue"
  ReDim b(1 To UBound(a, 1) * UBound(a, 2), 1 To 1)
  '
  For i = 1 To UBound(a, 1)
    For j = 1 To UBound(a, 2)
      If a(i, j) <> "" Then
        k = k + 1
        b(k, 1) = a(i, j)
      End If
    Next
  Next
  Sheets("Sheet2").Range("A1").Resize(k).Value = b
  Erase a, b
End Sub
 
Upvote 0
Tell me what appears in the msgbox



100,000 or 1,000,000(one million)?
1 Lakh.

I will try all the codes. And if I find any error I will update the same.
 
Upvote 0
I have no problems with 100,000 and DZ column.
Run the macro in post #17 again and tell me it appears in the msgbox.
 
Upvote 0

Forum statistics

Threads
1,216,488
Messages
6,130,952
Members
449,608
Latest member
jacobmudombe

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