VBA Macro to Find Text in One Column and Replace Text in Offset Column

truebluewoman

New Member
Joined
Sep 26, 2014
Messages
36
I am using Excel 2013 in Windows 7 Professional and need to do the following:

In Column GM, I have a property identification #.
In Column GI, I have a company identification #.

For every instance that the value 834 applears in column GM, I need a macro to enter the word "PROP 20" in the same row of Column GI. I have another group of similar items, but if I can figure the code for the one, I can repeat the same task for the other items.

I have been working on this for days and I am at my wit's end. I have been able to get ONE instance to happen, but I can't seem to get it to repeat until it runs through the entire list. I don't know a lot about VBA, but I've been able to construct a partial solution. Here is the code I have so far:

Sub Extract_Preparation()
'
' Macro that replaces PROP with PROP 20 and Yardi for properties 834,705, and 710 then
' sorts data by company, then by expense report #.
Dim colRange As Range
Dim cl As Range
Set colRange = Sheets("Extract Data").Range(Range("GM12"), Range("GM12").End(xlDown))
Set RngFound = Range("GM:GM").Find(What:=834, LookIn:=xlValues, _
LookAt:=xlWhole, _
MatchCase:=True)

For Each Found In colRange.Cells

RngFound.Offset(0, -4).Value = "PROP 20"

Next

Exit Sub

MsgBox "Extract Data Formatting Complete. You may now proceed to create the individual import files."

End Sub

Can anyone help me write the remaining code in order to repeat my function until it's complete? Thank you!
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try this:

Code:
Sub Extract_Preparation()
'
' Macro that replaces PROP with PROP 20 and Yardi for properties 834,705, and 710 then
' sorts data by company, then by expense report #.
Dim colRange As Range
Dim cl As Range
Set colRange = Sheets("Extract Data").Range(Range("GM12"), Range("GM12").End(xlDown))



For Each cell In colRange.Cells

If cell.Value = 834 Then

    cell.Offset(0, -4).Value = "PROP 20"

End If

Next


MsgBox "Extract Data Formatting Complete. You may now proceed to create the individual import files."

End Sub
 
Upvote 0
Try this:

Code:
Sub Extract_Preparation()
'
' Macro that replaces PROP with PROP 20 and Yardi for properties 834,705, and 710 then
' sorts data by company, then by expense report #.
Dim colRange As Range
Dim cl As Range
Set colRange = Sheets("Extract Data").Range(Range("GM12"), Range("GM12").End(xlDown))



For Each cell In colRange.Cells

If cell.Value = 834 Then

    cell.Offset(0, -4).Value = "PROP 20"

End If

Next


MsgBox "Extract Data Formatting Complete. You may now proceed to create the individual import files."

End Sub

Unfortunately, this did not work. The only item that worked was the Msg Box, but no text was changed.
 
Upvote 0
If 834 is present in GM12 then the code will put PROP 20 in GI12. Is that not correct?
 
Upvote 0
That is correct; however, the 834 doesn't just appear in GM 12. It appears throughout column GM. I just tested the process again, by putting an 834 only in cell GM12 and it did work for line 12. I need it to do it for every row where 834 appears in column GM. I think this tells me that the problem is in the setColRange parameters, right?
 
Last edited:
Upvote 0
Yes i presume there must be gaps? So xldown wont work.

Try:

Code:
Set colRange = Sheets("Extract Data").Range(Range("GM12"), <code>Range("GM" & .Rows.Count).End(xlUp).Row</code>)</pre>
 
Upvote 0
I'm getting Compile error: Invalid or unqualified reference. It's highlighting the ("GM" & .Rows.Count) portion of the code.
 
Upvote 0
Sorry this be better :)

Code:
Set colRange = Sheets("Extract Data").Range("GM12:GM" & Range("GM" & Rows.Count).End(xlUp).Row)
 
Upvote 0
Solution
"Steve the fish", YOU are my new best friend!!!! Thank you, thank you, thank you a million times from the bottom of my heart! I will toast in your name tonight and the rest of this year for getting this to work:)
 
Upvote 0

Forum statistics

Threads
1,222,115
Messages
6,164,015
Members
451,867
Latest member
csktwyr

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