Multiple (Specific) Columns to be stacked into another Column within the same WorkSheet (VBA Code)

Tyjyt

New Member
Joined
Oct 20, 2022
Messages
13
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
Platform
  1. Windows
Hello, I came across an issue whilst stacking multiple (specific) columns in excel. I have 5 columns that needs to be stacked up to last non empty cell. The data changes in every column occasionally, so range of each column might be different. There I do need to include Range.End(xlUp) as well... Looking at "Stack multiple columns VBA code", I was able to gain some clarity, however, I'm met with another obstacle.

Issue: While the code that I referenced was helpful, and successful in transferring across data (names), it isn't the same for cells that contain formulas... I'd like to seek for help to the data that I'm stacking which contains formulas, and it's presented in a % format. Is there any way to copy, and paste the values (e.g 10%, 14%) into another column while excluding the formulas? I think it might be something along the lines of PasteSpecial Paste:=xlPasteValuesAndNumberFormats, but I cant seem to get the code right :(

Sub Test()
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Dim Lastrowa As Long
For i = 1 To 5
Lastrow = Cells(Rows.Count, Choose(i, "C", "G", "K", "O", "R")).End(xlUp).Row + 1
If i = 1 Then
Lastrowa = 2
Else
Lastrowa = Cells(Rows.Count, "V").End(xlUp).Row 'mod
End If
Range(Cells(2, Choose(i, "C", "G", "K", "O", "R")), Cells(Lastrow, Choose(i, "C", "G", "K", "O", "R"))).Copy Cells(Lastrowa + 1, "V")
Next
Application.ScreenUpdating = True
End Sub
 
Kevin.

It already does that.
Before:
test.xlsm
ABCDEFGHIJKLMNOPQRSTUV
1NameInsertWeightage5%NameInsertWeightage8%NameInsertWeightage10%NameInsertWeightage5%NameInsertWeightage10%Stacked NamesStacked %
2
3John1%Jonathan1%Gabriel2%John1%Sky3%
4Peter1%Daniel1%Daniel2%Jonathan1%Luke2%
5Andrew1%Damien1%Andrew1%Luke1%Peter1%
6Luke1%John1%Luke1%Gabriel1%John3%
7Sky1%Luke1%Sky2%Gerard1%Johnny2%
8Mark0%Mark1%Edward2%
9Mike0%Mike1%
10Gabriel0%
11
Sheet1
Cell Formulas
RangeFormula
S3S3=ROW()/100


After:
test.xlsm
ABCDEFGHIJKLMNOPQRSTUV
1NameInsertWeightage5%NameInsertWeightage8%NameInsertWeightage10%NameInsertWeightage5%NameInsertWeightage10%Stacked NamesStacked %
2John6%
3John1%Jonathan1%Gabriel2%John1%Sky3%Peter2%
4Peter1%Daniel1%Daniel2%Jonathan1%Luke2%Andrew2%
5Andrew1%Damien1%Andrew1%Luke1%Peter1%Luke6%
6Luke1%John1%Luke1%Gabriel1%John3%Sky6%
7Sky1%Luke1%Sky2%Gerard1%Johnny2%Mark1%
8Mark0%Mark1%Edward2%Mike1%
9Mike0%Mike1%Gabriel3%
10Gabriel0%Jonathan2%
11Daniel3%
12Damien1%
13Edward2%
14Gerard1%
15Johnny2%
16
Sheet1
Cell Formulas
RangeFormula
S3S3=ROW()/100
Hi Kelvin, thanks for helping me out with this. Actually, could I trouble you to refer to this Sample (updated) attached, and edit the code accordingly? Because there were a couple of pieces of information that I've missed out which might have affected the code...

M2 & Q2 are titles which I'd like to exclude as well (Not to be included as part of the names when stacked)
 

Attachments

  • Sample.PNG
    Sample.PNG
    60 KB · Views: 6
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Kevin

You've provided 3 different data structures in posts #4, #6 and #11. I won't spend another minute on this until you provide what your data actually looks like using the XL2BB add in.
 
Upvote 0
Kevin

You've provided 3 different data structures in posts #4, #6 and #11. I won't spend another minute on this until you provide what your data actually looks like using the XL2BB add in.

Kevin

You've provided 3 different data structures in posts #4, #6 and #11. I won't spend another minute on this until you provide what your data actually looks like using the XL2BB add in.
Hi Kelvin, my deepest apologies. I am new to this, so I'd like to seek your understanding and patience. Please give me a while as I explore using XL2BB add in.
 
Upvote 0
Kevin

You've provided 3 different data structures in posts #4, #6 and #11. I won't spend another minute on this until you provide what your data actually looks like using the XL2BB add in.
Hi Kelvin, I've installed the add-in, as requested but I am unable to select any functions located within 'Capture the range'... I'd like to seek your understanding on this ... I will try my best to provide as much information here, along with the screenshot provided. There would be no changes after this.

As shared, I'd like to have my data stacked in Column U, Stacked Names, and Column V, Stacked Percentages respectively. However, Columns M2, and P2 have "title" in them, which are not part of the names. Hence, the starting point where data can be extracted out to the stacked columns would be "A2 & C2", "E2 & G2", "I2 & K2", "M3 & O3", "P3 & R3"

The code that you provided previously worked, but due to my lack of information, some information is being overlapped with each other, and I am deeply apologetic for, causing any inconvenience.

After extracting the data, and stacking them successfully, I'd like to be able to UNIQUE, and, if possible, to SUM the varying percentages tagged to the same name. It would look like this:
Column U (Stacked Names) | Column V (Stacked Percentages)
John | 4%
Peter | 1%
Andrew | 2%
.
.
.
Gabriel | 4%

*As there are formulas located in Columns "C", "G", "K", "O", "R", I'd only like to extract the percentage (e.g. 4%), excluding the formula
*Columns S and T are intentionally left blank
*Please ignore the highlights, as the cells are highlighted such that it's easier to see the different sets of data stacked on top of each other.

Thank you once again, Kelvin. I'm sorry for any inconvenience caused as it's not in my intention to waste anyone's time.
 

Attachments

  • Sample.PNG
    Sample.PNG
    67.9 KB · Views: 6
Upvote 0
1. My name is Kevin. Watch how it's spelt: K - E - V - I - N. It is not Kelvin.
2. The screenshot you provided in post #14 is now the 4th different data layout you have indicated in this thread. The code below will work on the layout you provided in post #14. If you change it again, or decide it wasn't correct, then someone else will need to be willing to assist you further because I will not.

With this layout (as you provided in post # 14)

test.xlsm
ABCDEFGHIJKLMNOPQRSTUVWX
1NameInsertWeightage5%NameInsertWeightage8%NameInsertWeightage10%NameInsertWeightage5%Stacked NamesStacked Percentages
2John1.561%Jonathan1.11%Gabriel1.82%Title 1Title
3Peter1.771%Daniel1.31%Daniel1.92%John1.11%Sky1.61%
4Andrew1.471%Damien1.41%Andrew1.21%Jonathan1.221%Luke0.91%
5Luke2.81%John1.111%Luke1.31%Luke1.451%Peter0.50%
6Sky2.11%Luke1.151%Sky1.62%Gabriel1.71%John1.61%
7Mark1.20%Mark1.21%Edward1.752%Gerard1.21%Johnny1.21%
8Mike1.10%Mike1.31%
9Gabriel10%
10
Sheet1


Running this code:

VBA Code:
Option Explicit
Sub Tyiyt_4()
    Application.ScreenUpdating = 0
    Dim Ws As Worksheet
    Set Ws = Worksheets("Sheet1")   '<<< change to actual sheet name
    Dim LRowSrc As Long, LRowDest As Long, Col
    
    Ws.Range("U1").CurrentRegion.Offset(1).ClearContents
    
    For Each Col In Array("C", "G", "K", "O", "R")
        LRowSrc = Ws.Cells(Rows.Count, Col).End(3).Row
        LRowDest = Ws.Cells(Rows.Count, "V").End(3).Row + 1
        Ws.Range(Cells(2, Col), Ws.Cells(LRowSrc, Col)).Copy
        Ws.Cells(LRowDest, "V").PasteSpecial xlPasteValuesAndNumberFormats
        Application.CutCopyMode = 0
        Ws.Range(Cells(2, Col), Ws.Cells(LRowSrc, Col)).Offset(, -2).Copy Ws.Cells(LRowDest, "U")
    Next Col
    
    Dim c As Range
    LRowDest = Ws.Cells(Rows.Count, "U").End(3).Row
    For Each c In Ws.Range("U2:U" & LRowDest)
        If c = "Title" Then c.Resize(, 2).Delete xlShiftUp
    Next c
    
    Dim Ar, i As Long, n As Long
    Ar = Ws.Range("U1").CurrentRegion
    
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(Ar, 1)
            .Item(Ar(i, 1)) = .Item(Ar(i, 1)) + Ar(i, 2)
        Next i
        Ar = Array(.keys, .items)
        n = .Count
    End With
    Ws.Range("U1").CurrentRegion.ClearContents
    Ws.Range("U1").Resize(n, 2).Value = Application.Transpose(Ar)
    
    Application.ScreenUpdating = 1
End Sub

Produces this result:

test.xlsm
ABCDEFGHIJKLMNOPQRSTUVWX
1NameInsertWeightage5%NameInsertWeightage8%NameInsertWeightage10%NameInsertWeightage5%Stacked NamesStacked Percentages
2John1.561%Jonathan1.11%Gabriel1.82%Title 1TitleJohn4%
3Peter1.771%Daniel1.31%Daniel1.92%John1.11%Sky1.61%Peter1%
4Andrew1.471%Damien1.41%Andrew1.21%Jonathan1.221%Luke0.91%Andrew2%
5Luke2.81%John1.111%Luke1.31%Luke1.451%Peter0.50%Luke5%
6Sky2.11%Luke1.151%Sky1.62%Gabriel1.71%John1.61%Sky4%
7Mark1.20%Mark1.21%Edward1.752%Gerard1.21%Johnny1.21%Mark1%
8Mike1.10%Mike1.31%Mike1%
9Gabriel10%Gabriel3%
10Jonathan2%
11Daniel3%
12Damien1%
13Edward2%
14Title 10%
15Gerard1%
16Johnny1%
17
Sheet1
 
Upvote 0
Solution
1. My name is Kevin. Watch how it's spelt: K - E - V - I - N. It is not Kelvin.
2. The screenshot you provided in post #14 is now the 4th different data layout you have indicated in this thread. The code below will work on the layout you provided in post #14. If you change it again, or decide it wasn't correct, then someone else will need to be willing to assist you further because I will not.

With this layout (as you provided in post # 14)

test.xlsm
ABCDEFGHIJKLMNOPQRSTUVWX
1NameInsertWeightage5%NameInsertWeightage8%NameInsertWeightage10%NameInsertWeightage5%Stacked NamesStacked Percentages
2John1.561%Jonathan1.11%Gabriel1.82%Title 1Title
3Peter1.771%Daniel1.31%Daniel1.92%John1.11%Sky1.61%
4Andrew1.471%Damien1.41%Andrew1.21%Jonathan1.221%Luke0.91%
5Luke2.81%John1.111%Luke1.31%Luke1.451%Peter0.50%
6Sky2.11%Luke1.151%Sky1.62%Gabriel1.71%John1.61%
7Mark1.20%Mark1.21%Edward1.752%Gerard1.21%Johnny1.21%
8Mike1.10%Mike1.31%
9Gabriel10%
10
Sheet1


Running this code:

VBA Code:
Option Explicit
Sub Tyiyt_4()
    Application.ScreenUpdating = 0
    Dim Ws As Worksheet
    Set Ws = Worksheets("Sheet1")   '<<< change to actual sheet name
    Dim LRowSrc As Long, LRowDest As Long, Col
   
    Ws.Range("U1").CurrentRegion.Offset(1).ClearContents
   
    For Each Col In Array("C", "G", "K", "O", "R")
        LRowSrc = Ws.Cells(Rows.Count, Col).End(3).Row
        LRowDest = Ws.Cells(Rows.Count, "V").End(3).Row + 1
        Ws.Range(Cells(2, Col), Ws.Cells(LRowSrc, Col)).Copy
        Ws.Cells(LRowDest, "V").PasteSpecial xlPasteValuesAndNumberFormats
        Application.CutCopyMode = 0
        Ws.Range(Cells(2, Col), Ws.Cells(LRowSrc, Col)).Offset(, -2).Copy Ws.Cells(LRowDest, "U")
    Next Col
   
    Dim c As Range
    LRowDest = Ws.Cells(Rows.Count, "U").End(3).Row
    For Each c In Ws.Range("U2:U" & LRowDest)
        If c = "Title" Then c.Resize(, 2).Delete xlShiftUp
    Next c
   
    Dim Ar, i As Long, n As Long
    Ar = Ws.Range("U1").CurrentRegion
   
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(Ar, 1)
            .Item(Ar(i, 1)) = .Item(Ar(i, 1)) + Ar(i, 2)
        Next i
        Ar = Array(.keys, .items)
        n = .Count
    End With
    Ws.Range("U1").CurrentRegion.ClearContents
    Ws.Range("U1").Resize(n, 2).Value = Application.Transpose(Ar)
   
    Application.ScreenUpdating = 1
End Sub

Produces this result:

test.xlsm
ABCDEFGHIJKLMNOPQRSTUVWX
1NameInsertWeightage5%NameInsertWeightage8%NameInsertWeightage10%NameInsertWeightage5%Stacked NamesStacked Percentages
2John1.561%Jonathan1.11%Gabriel1.82%Title 1TitleJohn4%
3Peter1.771%Daniel1.31%Daniel1.92%John1.11%Sky1.61%Peter1%
4Andrew1.471%Damien1.41%Andrew1.21%Jonathan1.221%Luke0.91%Andrew2%
5Luke2.81%John1.111%Luke1.31%Luke1.451%Peter0.50%Luke5%
6Sky2.11%Luke1.151%Sky1.62%Gabriel1.71%John1.61%Sky4%
7Mark1.20%Mark1.21%Edward1.752%Gerard1.21%Johnny1.21%Mark1%
8Mike1.10%Mike1.31%Mike1%
9Gabriel10%Gabriel3%
10Jonathan2%
11Daniel3%
12Damien1%
13Edward2%
14Title 10%
15Gerard1%
16Johnny1%
17
Sheet1
Yup. This works. Thank you for offering your help, Kevin. I'm sorry that I've been misspelling your name, and have also caused you inconvenience. I am thankful that you are still willing to help me, this means a lot. Have a great week ahead.
 
Upvote 0
Yup. This works. Thank you for offering your help, Kevin. I'm sorry that I've been misspelling your name, and have also caused you inconvenience. I am thankful that you are still willing to help me, this means a lot. Have a great week ahead.
No problems, glad we got there in the end 🙂👍
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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