Replace text if a cell is populated via vba

excel_2009

Active Member
Joined
Sep 14, 2009
Messages
318
Hi All,

Would it be possible (via VBA) to replace text in an already existing column IF a cell is not blank, the existing text is as follows (in one column on worksheet one):

Code:
[TABLE="width: 108"]
<colgroup><col></colgroup><tbody>[TR]
[TD]Name:Clark Kent[/TD]
[/TR]
[TR]
[TD]Address:Smallville[/TD]
[/TR]
[TR]
[TD]State:Kansas[/TD]
[/TR]
[TR]
[TD]Country:US[/TD]
[/TR]
</tbody>[/TABLE]

on worksheet two the following fields are visible:

Code:
[TABLE="width: 108"]
<colgroup><col></colgroup><tbody>[TR]
[TD]Name:Tony S[/TD]
[/TR]
[TR]
[TD]Address:[/TD]
[/TR]
[TR]
[TD]State:[/TD]
[/TR]
[TR]
[TD]Country:DE[/TD]
[/TR]
</tbody>[/TABLE]

So the plan would be for the code to replace Clark Kent with Tony S and replace US with DE and leave the Address, State as is.

Would this be possible, if so how?

Many thanks!!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Something tells me there's more to this than a possible 4-cell replacement.

Code:
Sub Replacements()
If Sheets("Sheet2").Range("B1") <> "" Then
  Sheets("Sheet1").Range("B1") = Sheets("Sheet2").Range("B1").Value
  ElseIf Sheets("Sheet2").Range("B2") <> "" Then
  Sheets("Sheet1").Range("B2") = Sheets("Sheet2").Range("B2").Value
  ElseIf Sheets("Sheet2").Range("B3") <> "" Then
  Sheets("Sheet1").Range("B3") = Sheets("Sheet2").Range("B3").Value
  ElseIf Sheets("Sheet2").Range("B4") <> "" Then
  Sheets("Sheet1").Range("B4") = Sheets("Sheet2").Range("B4").Value
End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
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