Update column in worksheet based on criteria with data from another worksheet

jayped

Board Regular
Joined
Mar 20, 2019
Messages
54
Hi there,

I'd like help creating a macro that allows me to essentially copy and paste information from one worksheet to another based on criteria that will save a great amount of time were it done manually. In the main worksheet I have a column labelled 'Reference' and a column labelled 'Statement Date' and the second worksheet would contain the Statement information which would have a reference column and a date column. I want the macro to update the "Statement Date' in the main worksheet with the date from the Statement tab based on the reference number in the main tab.


Thanks!!
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try this, Just adjust the names of the sheets and the reference and date columns.

Code:
Sub Update_column()
  Dim ws1 As Worksheet, ws2 As Worksheet, c As Range, f As Range
  Set ws1 = Sheets("[COLOR=#ff0000]Main[/COLOR]")
  Set ws2 = Sheets("[COLOR=#ff0000]Sheet2[/COLOR]")
  For Each c In ws1.Range("[COLOR=#ff0000]A[/COLOR]2", ws1.Range("[COLOR=#ff0000]A[/COLOR]" & Rows.Count).End(xlUp))
    Set f = ws2.Range("[COLOR=#ff0000]A:A[/COLOR]").Find(c, , xlValues, xlWhole)
    If Not f Is Nothing Then
      ws1.Cells(c.Row, "[COLOR=#ff0000]B[/COLOR]") = ws2.Cells(f.Row, "[COLOR=#ff0000]B[/COLOR]")
    End If
  Next
End Sub
 
Upvote 0
Thank you for your help!

Just one problem which I think lies at "If Not f Is Nothing" - I don't want dates that may have been entered previously to be deleted when I run the macro because the references can't be found on sheet 2. I just want blank cells to be updated and everything else remains as is. What would be the best way around this?
 
Upvote 0
Also, can you provide something similar for a single worksheet if i had the same 2 worksheets in one side by side?
 
Upvote 0
Thank you for your help!

Just one problem which I think lies at "If Not f Is Nothing" - I don't want dates that may have been entered previously to be deleted when I run the macro because the references can't be found on sheet 2. I just want blank cells to be updated and everything else remains as is. What would be the best way around this?

Try this

Code:
Sub Update_column()
  Dim ws1 As Worksheet, ws2 As Worksheet, c As Range, f As Range
  Set ws1 = Sheets("Main")
  Set ws2 = Sheets("Sheet2")
  For Each c In ws1.Range("A2", ws1.Range("A" & Rows.Count).End(xlUp))
    Set f = ws2.Range("A:A").Find(c, , xlValues, xlWhole)
    If Not f Is Nothing Then

     [COLOR=#0000ff] if ws1.Cells(c.Row, "B") = "" then [/COLOR]ws1.Cells(c.Row, "B") = ws2.Cells(f.Row, "B")
    End If
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,150
Members
453,021
Latest member
Justyna P

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