How to integrate two columns in VBA

aryaden

Board Regular
Joined
Jun 9, 2021
Messages
101
Office Version
  1. 2019
Platform
  1. Windows
I have two columns of data I need to integrate and I was running into some trouble

This is my example sheet:

Example Sheet.xlsm
AB
1Category Description
2
3BasketballAAAA
4BasketballBBBB
5SoccerCCCC
6SoccerDDDD
7SoccerEEEE
8TennisFFFF
9TennisGGGG
Sheet1


and I need the output to be:

Example Sheet.xlsm
A
1Category_Description
2
3Basketball
4AAAA
5BBBB
6Soccer
7CCCC
8DDDD
9EEEE
10Tennis
11FFFF
12GGGG
Sheet4
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Why not pivot the data without using VBA?

Other.xlsm
ABCD
1CategoryDescription
2BasketballAAAARow Labels
3BasketballBBBBBasketball
4SoccerCCCCAAAA
5SoccerDDDDBBBB
6SoccerEEEESoccer
7TennisFFFFCCCC
8TennisGGGGDDDD
9EEEE
10Tennis
11FFFF
12GGGG
13
14
15
16
17
Sheet5
 
Upvote 0
How about
VBA Code:
Sub aryaden()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, nr As Long
   
   Ary = Range("A3:B" & Range("A" & Rows.Count).End(xlUp).Row).Value2
   ReDim Nary(1 To UBound(Ary) * 2, 1 To 1)
   With CreateObject("scripting.dictionary")
      For r = 1 To UBound(Ary)
         If Not .Exists(Ary(r, 1)) Then
            .Add Ary(r, 1), Nothing
            nr = nr + 2
            Nary(nr - 1, 1) = Ary(r, 1)
            Nary(nr, 1) = Ary(r, 2)
         Else
            nr = nr + 1
            Nary(nr, 1) = Ary(r, 2)
         End If
      Next r
   End With
   Sheets("sheet4").Range("A3").Resize(nr).Value = Nary
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub aryaden()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, nr As Long
  
   Ary = Range("A3:B" & Range("A" & Rows.Count).End(xlUp).Row).Value2
   ReDim Nary(1 To UBound(Ary) * 2, 1 To 1)
   With CreateObject("scripting.dictionary")
      For r = 1 To UBound(Ary)
         If Not .Exists(Ary(r, 1)) Then
            .Add Ary(r, 1), Nothing
            nr = nr + 2
            Nary(nr - 1, 1) = Ary(r, 1)
            Nary(nr, 1) = Ary(r, 2)
         Else
            nr = nr + 1
            Nary(nr, 1) = Ary(r, 2)
         End If
      Next r
   End With
   Sheets("sheet4").Range("A3").Resize(nr).Value = Nary
End Sub
Thank you! That helps, I will try to implement that
 
Upvote 0
I just tried it and is there anyway to modify the code so the output is this?
Book1345.xlsm
ABCD
1Description123
2
3Basketball
4AAAX
5BBBXX
6Soccer
7CCCCX
8DDDD
9EEEEXX
10Tennis
11FFFX
12GGGX
13
14
Sheet3


Currently this is what my output is using the code you posted:
Book1345.xlsm
ABCDE
1CategoryDescription123
2
3BasketballAAAX
4AAABBBXX
5BBBCCCCX
6SoccerDDDD
7CCCCEEEEXX
8DDDDFFFX
9EEEEGGGX
10Tennis
11FFF
12GGG
13
14
15
Sheet1


and the initial data sheet looks like this:
Book1345.xlsm
ABCDE
1CategoryDescription123
2
3BasketballAAAX
4BasketballBBBXX
5SoccerCCCCX
6SoccerDDDD
7SoccerEEEEXX
8TennisFFFX
9TennisGGGX
Sheet5


I really appreciate the help! I am an intern and I have multiple workbooks with 20-30 sheets each to process and this would save me a lot of time!
 
Upvote 0
You're welcome
I worked on it and tried to edit your code, but I still cannot get the contents to the right to stay intact and for the formatting to stay the same (like the example I posted above at 4:46pm) I am trying to get "basketball", "soccer" and "tennis" to become their own rows so "AAA-GGG" and their corresponding Xs stay intact. Is there anyway to make this adding of rows to happen?

Thanks in advance for any help!
Best Regards,
Arya
 
Upvote 0
The reason the code does not do what you want, is that you never asked for that to happen & you only showed one column of data in your op.
You should never simplify your request, as it is unlikely to work when you do.

As this is now a totally different question from your op, you need to start a new thread. Thanks
 
Upvote 0
Thanks! I will be sure to start a new thread for the question.

Best Regards
 
Upvote 0
The reason the code does not do what you want, is that you never asked for that to happen & you only showed one column of data in your op.
You should never simplify your request, as it is unlikely to work when you do.

As this is now a totally different question from your op, you need to start a new thread. Thanks
Is it ok if I add the code you provided in the new thread? I am fairly new to the forum and was unsure if that was acceptable.

Best Regards
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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