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

jayped

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

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
7,919
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
33
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
33
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
7,919
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
7,919
Office Version
2007
Platform
Windows
I'm glad to help you. Thanks for the feedback.
 

Forum statistics

Threads
1,078,442
Messages
5,340,305
Members
399,366
Latest member
ahmed elsaid

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top