Macro to loop worksheets and copy and merge values to single cell

Flea2

New Member
Joined
Nov 3, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am just getting started with VBA and my need described below is beyond my current knowledge - I would greatly appreciate anyone who could advance my learning to achieve the following:

In the uploaded minisheet I show the "before and after" of what I need a VBA routine to do.

Specifically, in the Range A1:B9, I need the code to:
- check each row in turn, and where
the value in column A is = "", copy the value in the adjacent cell in Column B and
Add it (concatenate) to the existing value (on a new line) in the cell immediately above it and then
Delete the complete row that had the value "" in Column A.

Thanks in advance for your help/coaching.

Merge Values on blank condition .xlsm
ABCDEFG
101-Nov-21Description 1of2< BEFOREAFTER >01-Nov-21Description 1of2 Description 2of2
2Description 2of202-Nov-21Description 1of2 Description 2of2
302-Nov-21Description 1of202-Nov-21Description 1
4Description 2of203-Nov-21Description 1
502-Nov-21Description 104-Nov-21Description 1of2 Description 2of2
603-Nov-21Description 105-Nov-21Description 1
704-Nov-21Description 1of2
8Description 2of2
905-Nov-21Description 1
Data-before&after
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi & welcome to MrExcel.
How about
VBA Code:
Sub Flea()
   Dim Rng As Range
   
   For Each Rng In Range("A1:A" & Range("B" & Rows.Count).End(xlUp).Row).SpecialCells(xlBlanks).Areas
      Rng.Offset(-1, 1).Value = Join(Application.Transpose(Rng.Offset(-1, 1).Resize(Rng.Count + 1).Value), vbLf)
   Next Rng
   Range("A:A").SpecialCells(xlBlanks).EntireRow.Delete
End Sub
 
Upvote 0
Solution
Hi & welcome to MrExcel.
How about
VBA Code:
Sub Flea()
   Dim Rng As Range
  
   For Each Rng In Range("A1:A" & Range("B" & Rows.Count).End(xlUp).Row).SpecialCells(xlBlanks).Areas
      Rng.Offset(-1, 1).Value = Join(Application.Transpose(Rng.Offset(-1, 1).Resize(Rng.Count + 1).Value), vbLf)
   Next Rng
   Range("A:A").SpecialCells(xlBlanks).EntireRow.Delete
End Sub
Brilliant - thank you. I will be analyzing the solution to accelerate my learning!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,547
Members
449,089
Latest member
davidcom

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