VBA Script Help Is this possible

MikeExcelNovice

New Member
Joined
Feb 6, 2018
Messages
3
I use the below script at work to easily and quickly concatenate large amounts of data. We use this to create a common denominator for vlookups between reports. I want to see if I can make this report as automated as possible. The script reads as follows:

Sub combine_columns()
Dim store As Variant
Dim sku As Variant
Range("D1").Select 'Change as appropriate
For Each sku In Range("B1:B") 'Change as appropriate
For Each store In Range("A1:A") 'Change as appropriate
ActiveCell.Formula = store & sku
ActiveCell.Offset(1, 0).Select
Next
Next
Range("D1").Select
End Sub

All I have to do is paste my data into columns A and B. Once that is complete I go into VBA and update the range to read B1:B6 and A1:A155 for example. Once I run the macro the concatenation populates in column D. To save time I wrote a COUNT formula in G2 to know my total range for A and a COUNT formula in H2 to know my total range for column B. Is there anyway to insert these values into the macro so I don't have to manually type them in every time.


For example, if my range in column A is A1:A64, cell G2 will read A64 so I know to go into the macro and edit A1:A(blank) to A1:A64.


Thanks for any help!!!!!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hello MikeExcelNovice,

This version of your macro will automatically detect the end of both columns "A" and "B" and place the concatenated result in column "D".

Code:
Sub combine_columns()


    Dim rngOut      As Range
    Dim rngSKU      As Range
    Dim rngStore    As Range
    Dim store       As Variant
    Dim sku         As Variant
    
        Set rngStore = Range("A1", Cells(Rows.Count).End(xlUp))
        Set rngSKU = Range("B1", Cells(Rows.Count).End(xlUp))
        
        Set rngOut = Range("D1")
        
            For Each sku In rngSKU
                For Each store In rngStore
                    rngOut = store & sku
                    Set rngOut = rngOut.Offset(1, 0)
                Next store
            Next sku
            
        Range("D1").Select


End Sub
 
Last edited:
Upvote 0
Hi & welcome to the board
Try changing these 2 lines as shown
Code:
For Each sku In Range("B1",Range("B")&Rows.Count).End(xlup)) 'Change as appropriate
    For Each store In Range("A1",Range("A"&Rows.count).End(xlup)) 'Change as appropriate
 
Upvote 0
another version
Code:
Sub combine_columns()
 Dim store As Variant, cnt1 As Long, cnt2 As Long
 Dim sku As Variant
 cnt1 = Cells(Rows.Count, 1).End(xlUp).Row
 cnt2 = Cells(Rows.Count, 2).End(xlUp).Row
 For Each sku In Range("B1:B" & cnt2) 'Change as appropriate
    For Each store In Range("A1:A" & cnt1) 'Change as appropriate
        If Range("D1") = "" Then
            Range("D1") = store & sku
        Else
            Cells(Rows.Count, 4).End(xlUp)(2) = store & sku
        End If
        Range("G2") = cnt1
        Range("H2") = cnt2
    Next
 Next
 Range("D1").Select
 End Sub
 
Upvote 0
Hi & welcome to the board
Try changing these 2 lines as shown
Code:
For Each sku In Range("B1",Range("B")&Rows.Count).End(xlup)) 'Change as appropriate
    For Each store In Range("A1",Range("A"&Rows.count).End(xlup)) 'Change as appropriate


Thanks for the quick response. I've tried changed the 2 lines but keep getting a compile error saying Expected:End of statement.
 
Upvote 0
Oops, had an extra ) in there. It should be
Code:
   For Each sku In Range("B1", Range("B" & Rows.Count).End(xlUp)) 'Change as appropriate
      For Each store In Range("A1", Range("A" & Rows.Count).End(xlUp)) 'Change as appropriate
 
Last edited:
Upvote 0
Oops, had an extra ) in there. It should be
Code:
   For Each sku In Range("B1", Range("B" & Rows.Count).End(xlUp)) 'Change as appropriate
      For Each store In Range("A1", Range("A" & Rows.Count).End(xlUp)) 'Change as appropriate
That did the trick. Thanks everyone all 3 answers worked.
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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