VBA Macro to combine two row data into one row

cbacba

New Member
Joined
Feb 28, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello,
I am really struggling trying to figure out how to take data from two rows (from Sample File 1) and combining into single rows (Sample File Result).

Most of the sets are two row combinations that can be combined into one.
However, there are a few that the first line will have multiple rows underneath, that will need to be moved, then the data from the first row copied down to match when the multiple rows are moved. I've highlighted examples of that in gray on the result image.

Thanks for the help!
 

Attachments

  • Sample File 1.PNG
    Sample File 1.PNG
    39.8 KB · Views: 43
  • Sample File Result.PNG
    Sample File Result.PNG
    21.7 KB · Views: 44

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
VBA Code:
Option Explicit
Sub TransferData()
    'Assuming the data is being transfered from "Sheet1" to "Sheet2"... You might want to change the names in the code if you are using other names.
    Dim srcRowStart As Long: srcRowStart = 9 'The row on the main sheet where the starts
    Dim srcRowEnd As Long
    Dim destRowStart As Long: destRowStart = 1 'Change this to "destRowStart = 2" id you have a header on the first row of Sheet2
    Dim destRowCounter As Long: destRowCounter = destRowStart
    Dim prevRowIsBlank As Boolean: prevRowIsBlank = True
    Dim firstRange As Range
    Dim secondRange As Range
    Dim i As Long
    
    srcRowEnd = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    
    For i = srcRowStart To srcRowEnd
        If prevRowIsBlank Then
            If Worksheets("Sheet1").Range("A" & i).Value <> "" Then
                Set firstRange = Worksheets("Sheet1").Range("A" & i & ":D" & i)
                prevRowIsBlank = False
            End If
        Else
            Set secondRange = Worksheets("Sheet1").Range("A" & i & ":J" & i)
            Worksheets("Sheet2").Range("A" & destRowCounter & ":D" & destRowCounter).Value = firstRange.Value
            Worksheets("Sheet2").Range("E" & destRowCounter & ":N" & destRowCounter).Value = secondRange.Value
            destRowCounter = destRowCounter + 1
        End If
        If Worksheets("Sheet1").Range("A" & i + 1).Value = "" Then prevRowIsBlank = True
    Next i
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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