VBA to insert lines and bring in looked up value

Iron_Man

New Member
Joined
Aug 26, 2014
Messages
25
Hi folks,

I am hoping for some advice on how to best perform a task in Excel, hopefully via a macro.

I assume two spreadsheets in the same book.

Sheet 1
Column A has a unique identifier, approximately 2,000 lines filled out from column A to T

Sheet 2 has the same identifier in column A with about 10,000 lines (some identifiers in sheet 2 don't exist in sheet1), filled out from column A to K.

I am trying to write a macro to insert a new line under each identifier in Sheet 1, merge all cells from A to T and insert the matching data from column K Sheet 2 (ie identifier 1 in sheet 1 will correlate to identifier 2 in sheet 2 and return the value on the same line in column K).

Ideally, this means by sheet 1 will have 4,000 lines in the end with every second line containing data imported from sheet 2.

As I'm writing this I'm thinking I'm dreaming but maybe some clever cogs will have a good idea :)

Thanks in advance.
IM
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Give this a shot:

Code:
Public Sub Iron_Man_001()

Dim thisRow As Long
Dim sheetOne As Worksheet
Dim sheetTwo As Worksheet
Dim foundRow As Variant

Set sheetOne = Sheets("Sheet1") ' change the name to match what you have
Set sheetTwo = Sheets("Sheet2") ' change the name to match what you have
thisRow = 1 ' Change to be the first row on sheet 1

Application.ScreenUpdating = False

While sheetOne.Cells(thisRow, "A").Value <> ""
    sheetOne.Rows(thisRow + 1).Insert xlShiftDown
    sheetOne.Range(sheetOne.Cells(thisRow + 1, "A"), sheetOne.Cells(thisRow + 1, "T")).Merge
    foundRow = Application.Match(sheetOne.Cells(thisRow, "A").Value, sheetTwo.Range("A:A"), 0)
    If Not IsError(foundRow) Then sheetOne.Cells(thisRow + 1, "A").Value = sheetTwo.Cells(foundRow, "K").Value
    thisRow = thisRow + 2
Wend

Application.ScreenUpdating = True

End Sub

WBD
 
Upvote 0
:eeek: wideboydixon, this is perfect.

Thank you very much, nothing else to say :) Out of interest, if I wanted to apply specific formatting (ie the existing line is blue shaded but the new inserted is white with an indent), are they conditions I need to add after the .Merge? And is it possible or would it add too much lag so I should do it manually?
 
Upvote 0
You can mess around with the formatting like this:

Code:
Public Sub Iron_Man_001()

Dim thisRow As Long
Dim sheetOne As Worksheet
Dim sheetTwo As Worksheet
Dim foundRow As Variant

Set sheetOne = Sheets("Sheet1") ' change the name to match what you have
Set sheetTwo = Sheets("Sheet2") ' change the name to match what you have
thisRow = 1 ' Change to be the first row on sheet 1

Application.ScreenUpdating = False

While sheetOne.Cells(thisRow, "A").Value <> ""
    sheetOne.Rows(thisRow + 1).Insert xlShiftDown
    With sheetOne.Range(sheetOne.Cells(thisRow + 1, "A"), sheetOne.Cells(thisRow + 1, "T"))
        .Merge
        .Interior.Color = RGB(224, 224, 255)
        .HorizontalAlignment = xlCenter
    End With
    foundRow = Application.Match(sheetOne.Cells(thisRow, "A").Value, sheetTwo.Range("A:A"), 0)
    If Not IsError(foundRow) Then sheetOne.Cells(thisRow + 1, "A").Value = sheetTwo.Cells(foundRow, "K").Value
    thisRow = thisRow + 2
Wend

Application.ScreenUpdating = True

End Sub

Just put the necessary colors, alignments etc. there.

WBD
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

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