Ad

VBA How Do I Edit Cell Contents Based On Contents Of Two Other Cells In Same Row

- 1 answer

I need help creating a macro for cleaning up a csv sheet that contains product sales information including the shipping method that needs to be used. There are about 10 of our products that cannot be sent via our usual shipping methods and I need to overwrite the shipping method that is assigned to orders that contain these products.

  • Column A is order numbers
  • Column B is the product purchased
  • Column C is the shipping method

If someone purchases multiple products in one order the order number is duplicated in the next row, the shipping method only displays in the first row with that order number and each consecutive product purchased in the same order just adds a new row with the product name in column B

I need to use VBA to check for a list of 10 or so specific products in column B and update the shipping method for the whole order (the upper most cell in column C that has the same value in cell A) if it contains one of these 10 products.

I think it needs to do something like this:

  1. look at column B and search for a string of text
  2. if cell matches the search criteria look at the value in column A in the same row
  3. find the first instance of this value in Column A
  4. replace the contents in Column B in the same row with the new shipping method

I would need this in a loop so that it does it for each instance of the search string in column B in the spreadsheet.

I hope this question is clear, I have searched SO and can't find an answer to this question. Any help or alternative fixes are welcome.

Ad

Answer

Try out the following code, given the setup and result in the following screenshots:

Sub test()

Dim pRange As Range
Dim nShip As Range

Set pRange = Range("E2")
pRange.Select

While ActiveCell.Value <> ""

Range("B2").Select

While ActiveCell.Value <> ""

If ActiveCell.Offset(0, -1).Value <> ActiveCell.Offset(-1, -1).Value Then

Set nShip = ActiveCell.Offset(0, 1)

End If

If ActiveCell.Value = pRange.Value Then

nShip.Value = pRange.Offset(0, 1).Value
ActiveCell.Offset(1, 0).Select

Else

ActiveCell.Offset(1, 0).Select

End If

Wend

Set pRange = pRange.Offset(1, 0)
pRange.Select

Wend

End Sub

Starting List:

enter image description here

Result after macro:

enter image description here

Ad
source: stackoverflow.com
Ad