Run Macro for next row

keyra

New Member
Joined
Dec 5, 2016
Messages
11
Hello everybody!
I have this macro which concatenates cell L2 and M2 and paste the result in W2. then stops.
My question is how to make this macro to run for the entire columns(L:L & M:M = W:W)? I want to mention that my columns may contain blanks and the sheet is monthly updated (from scracth) and i'm seeking the macro to adapt to the nr of rows. Can you please help me? Thank you!

Dim String1 As String
Dim String2 As String
Dim full_string As String
String1 = Cells(2, 12).Value
String2 = Cells(2, 13).Value
Cells(2, 23).Value = String1 + String2
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try this:

VBA Code:
Sub macro3()
  With Range("W2:W" & Range("L:M").Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row)
    .Formula = "=L2&M2"
    .Value = .Value
  End With
End Sub
 
Upvote 0
Solution
Assuming there's a header row and that column L would always be the one with the most data, then perhaps
VBA Code:
Sub Concat()
Dim Lrow As Long, i As Integer

Lrow = Cells(Rows.count, 12).End(xlUp).Row
For i = 2 To Lrow
    Cells(i, 23) = Cells(i, 12) & Cells(i, 13)
Next

End Sub
If M might be the column with the most rows of data then that needs tweaking.
 
Upvote 0
It sounds as though you want to calculate the concatenation for all rows where at least column L is non-empty or column M is non empty. Unless you know, by some other mechanism, how many rows you are looking at, the end of your calculations come when both column L and column M in the same row are empty. Right?


VBA Code:
sub ConcatWhileDataIsThere (col1 as string, col2 as string, col3 as string,startRow as long)

'    Concat cells in column col1 and col2 into col3
'    Col1 is the first column to look at to find data
'    Col2 is the second column to look at to find data

'    Col3 is the column where you want the concatenated result to be placed

'    StartRow is the row on which you want to start. The routine runs until both Col1 and Col2 are empty on the same row
'    or until you run out of rows - but hopefully that won't happen;-)

dim ThisRow as long
dim str1 as string
dim str2 as string

ThisRow = startRow
str1 = range(Col1 & format(ThisRow)).value
str2 = range(Col2 & format(ThisRow)).value

while not (str1="" and str2="")
      range(Col3 & format(ThisRow)).formula=str1&str2        '    This does the work!
      ThisRow=ThisRow+1
      str1 = range(Col1 & format(Thisrow)).value
      Rtr2 = range(Col2 & format(ThisRow)).value
wend
End Sub

This generalizes your solution, and will start on the StartRow, and examine Col1 and Col2, and if at least one of them is non blank, it will concatenate them and put the result in Col3. It then moves on to the next row. When both Col1 and Col2 are empty, the routine stops.

HTH

Tony
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,004
Members
452,374
Latest member
keccles

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