VBA concatenate

sethae

New Member
Joined
Apr 28, 2010
Messages
8
I'm trying to use a concatenate function in a macro across several sheets. The problem I'm running into is that the number of columns in each sheet is dynamic, meaning it changes from day to day or week to week.

Is there a way I can incorporate a dynamic concatenate function into my macro?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
If you can post a mock-up or a sample of the columns as they might occur, in a before-and-after sort of way, you might get more specific guidance.

In general, you can have VBA retrieve the columns as they currently appear, and then loop to concatenate each one, or picking and choosing as needed.

This example takes all the cells in the first row of the active sheet, and concatenates them, then places the result in cell A2 of the active sheet.

Code:
Sub ConcatDynCol()
  Dim s As String
  Dim rngRow As Excel.Range
  Dim rngCell As Excel.Range
  
  Set rngRow = Intersect(ActiveSheet.Rows(1), ActiveSheet.UsedRange)
  
  For Each rngCell In rngRow.Cells
    s = s & rngCell.Value
  Next rngCell
  
  ActiveSheet.Range("A2").Value = s
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,531
Messages
6,179,380
Members
452,907
Latest member
Roland Deschain

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