Excel macro to change parts of 3 cells based on two conditions

novaci

New Member
Joined
Aug 27, 2014
Messages
2
Hey!

I'm fairly familiar with macros that go through a document and fill empty cells based on information existing information, but I'm a little lost now that I need to make a macro to change some parts of existing information based on two conditions. Could someone point me to the right direction?

Cells in columns G and M contain information that both need to match specific conditions in order for the macro to run on that row.

Lets say column G needs to be "31" and column B needs to be "XXX000". If both conditions are met, the macro would change information on three other cells on that row and loop through the entire sheet.

Column U is the first that needs to be changed and the entire cell can be changed outright to "XXX2" since it contains no other relevant information.

Columns W and X where I'm running out of skill since I need to keep a part of the cell contents and only change the part that I need changed. Lets say the cell content is "XXX11%XXX1" where the "XXX11%" part varies on each row and is unique. I'd need to change only the part after the % symbol to "XXX2" while keeping rest of the information in that cell intact. Same change needs to be made on column X. The contents of cells on column X is in the style of "XXX11§XXX".

Any idea if this is doable with VBA?

Thanks!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
this should do
Code:
Sub novaci()
For i = 1 To Range("B" & Rows.Count).End(xlUp).Row
If Cells(i, "B").Value = "XXX000" And Cells(i, "G") = 31 Then
 Cells(i, "U").Value = "XXX2"
 Cells(i, "W").Value = Left(Cells(i, "W").Value, InStr(1, Cells(i, "W").Value, "%")) & "XXX2"
 Cells(i, "X").Value = Left(Cells(i, "X").Value, InStr(1, Cells(i, "X").Value, "§")) & "XXX2"
End If
Next i
End Sub
 
Upvote 0
this should do
Code:
Sub novaci()
For i = 1 To Range("B" & Rows.Count).End(xlUp).Row
If Cells(i, "B").Value = "XXX000" And Cells(i, "G") = 31 Then
 Cells(i, "U").Value = "XXX2"
 Cells(i, "W").Value = Left(Cells(i, "W").Value, InStr(1, Cells(i, "W").Value, "%")) & "XXX2"
 Cells(i, "X").Value = Left(Cells(i, "X").Value, InStr(1, Cells(i, "X").Value, "§")) & "XXX2"
End If
Next i
End Sub

That works perfectly, thank you so much!

One thing I missed though is column Y. That column doesn't always contain information but if it does, it needs to have the same operation performed on it that columns W and X go through. I could add another subroutine for those cases with an added condition, but is there a simpler way?
 
Upvote 0

Forum statistics

Threads
1,222,012
Messages
6,163,389
Members
451,835
Latest member
Arvindanallen

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