Copy any values in column Q to Column G

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,

OK so this is what I need.

when I run the macro,

Sheet "Stocked Products" has a list of all the products we stock in Rows 3:200

Column G is the current selling price.

Column Q is generated when the price from our suppliers has gone up or down to show a price change and shows the new price.
So I want a macro to update my Column G with new prices when the macro is run but not over write the ones that have not changed see example below.

Ideal would be macro is run and asks "There are new prices do you want to update?"
if I select yes do it
No exit sub


AGQ
1
2
3Productother columnsCurrent PriceothercolumnsNew Price
4cup10.00
5spoon11.0015.00so the macro would replace G5 with 15.00 as this is the new price
6hat5.00but the ones with no one price don't change.
7coatcoat7.00
8fork4.00
9spoon9.007.00
10duck12.00

<tbody>
</tbody>
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try:

Code:
Sub UpdatePrices()
Dim MyData As Variant, i As Long, ctr1 As Long

    MyData = Range("A4:Q" & Cells(Rows.Count, "A").End(xlUp).Row).Value
    
    For i = 1 To UBound(MyData)
        If MyData(i, 17) <> "" Then
            If MyData(i, 17) <> MyData(i, 3) Then ctr1 = ctr1 + 1
        End If
    Next i
            
    If ctr1 = 0 Then
        MsgBox "No changes found."
        Exit Sub
    End If
    
    If MsgBox(ctr1 & " changes found.  Continue?", vbYesNo, "Change status") = vbNo Then Exit Sub
    
    Application.ScreenUpdating = False
    
    For i = 1 To UBound(MyData)
        If MyData(i, 17) <> "" Then
            If MyData(i, 17) <> MyData(i, 3) Then Cells(i + 3, "C") = Cells(i + 3, "Q")
        End If
    Next i
    
    Application.ScreenUpdating = True
        
End Sub
Let us know if you need help installing it.
 
Upvote 0
Urk! I just noticed, your current price is in column G, not C. You'll need to make the following changes:

Rich (BB code):
Sub UpdatePrices()
Dim MyData As Variant, i As Long, ctr1 As Long


    MyData = Range("A4:Q" & Cells(Rows.Count, "A").End(xlUp).Row).Value
    
    For i = 1 To UBound(MyData)
        If MyData(i, 17) <> "" Then
            If MyData(i, 17) <> MyData(i, 7) Then ctr1 = ctr1 + 1
        End If
    Next i
            
    If ctr1 = 0 Then
        MsgBox "No changes found."
        Exit Sub
    End If
    
    If MsgBox(ctr1 & " changes found.  Continue?", vbYesNo, "Change status") = vbNo Then Exit Sub
    
    Application.ScreenUpdating = False
    
    For i = 1 To UBound(MyData)
        If MyData(i, 17) <> "" Then
            If MyData(i, 17) <> MyData(i, 7) Then Cells(i + 3, "G") = Cells(i + 3, "Q")
        End If
    Next i
    
    Application.ScreenUpdating = True
        
End Sub
 
Upvote 0
Another option if your interested
Code:
Sub Copyprice()
   With Range("G5", Range("G" & Rows.count).End(xlUp))
      .Value = Evaluate(Replace(Replace("if(@q="""",@,@q)", "@q", .Offset(, 10).Address), "@", .Address))
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,840
Members
449,096
Latest member
Erald

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