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

jayped

New Member
Joined
Mar 20, 2019
Messages
38
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!!
 

Some videos you may like

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
10,192
Office Version
2007
Platform
Windows
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
 

jayped

New Member
Joined
Mar 20, 2019
Messages
38
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?
 

jayped

New Member
Joined
Mar 20, 2019
Messages
38
Also, can you provide something similar for a single worksheet if i had the same 2 worksheets in one side by side?
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
10,192
Office Version
2007
Platform
Windows
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
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
10,192
Office Version
2007
Platform
Windows
I'm glad to help you. Thanks for the feedback.
 

Forum statistics

Threads
1,089,377
Messages
5,407,904
Members
403,169
Latest member
Luna17

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...
Top