VBA to update selected column values combining the current value of the selected column with the value in the column next to it

gberg

Board Regular
Joined
Jul 16, 2014
Messages
205
Office Version
  1. 365
Platform
  1. Windows
I have a table that has two columns. One column for quantity for the current order the second for quantities for previously ordered items

I want a macro that adds the two together and then updates the Prev Ordered column

It would start out like this
1660005656671.png


And end up like
1660005696566.png


If possible, I would like to filter by only the items that have a value in the Ordered Qty so there are not as many calculations to go through. Some tables could have thousands of rows but only have a handful of items to be ordered.

Thanks

Greg
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi Greg,
Still haven't tried to filter the data but give this simple one a try first.
Change table name to suit.

VBA Code:
Sub Sample()
    Dim c As Range
    Set c = Selection
    Application.ScreenUpdating = False
    Range("Table1[Order Qty]").Copy
    Range("Table1[Prev Ordered]").PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd
    Range("Table1[Order Qty]").ClearContents
    c.Select
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Solution
So I assumed the current order was in A and previous in B

1660025254349.png


VBA Code:
Sub OrdersUpdate()
Dim WbName As Workbook
Dim WsName1 As Worksheet
Dim CurOrder As Range
Dim PrevOrdr As Range
Dim LastRowOrders As Long
Dim OLoop As Long

'Sets the name of the workbook and sheet
Set WbName = ThisWorkbook
Windows(ThisWorkbook.Name).Activate
Set WsName1 = WbName.Sheets(1)

'Finds last row
LastRowOrders = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

'Assume headers are in row 1
'Assume Current order is in column A and previous in B

For OLoop = 2 To LastRowOrders
    If WsName1.Range("A" & OLoop).Value > 0 Then
        WsName1.Range("B" & OLoop).Value = WsName1.Range("B" & OLoop).Value + WsName1.Range("A" & OLoop).Value
        'If you want the Current order deleted uncomment the row below
        'WsName1.Range("A" & OLoop).Clear
    End If
Next OLoop

End Sub

Result
1660025499928.png
 
Upvote 0
Colo that worked great, but not if you have filters set. Without filters it worked perfectly. With filters the results were not good. It seems to run quickly so I will be fine just making sure there are no filters and all good. When the data was filtered the results showed up in incorrect corresponding cells and it was put into multiple cells. Again, I will just make sure the table is not filtered.
 
Upvote 0
Yes, it wouldn't handle a filter
 
Upvote 0
If you want to after turning off screen updating Application.ScreenUpdating = False you could add this:
VBA Code:
If ActiveSheet.FilterMode = True Then
    ActiveSheet.ShowAllData 
End If
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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