HELP!!! IF THEN STATEMENT - VISUAL BASIC

mags74

New Member
Joined
Jul 19, 2007
Messages
7
Is it possible to do something within visual basic to say ==

if A1 = "Hello"
Then B2= "World"


but i want to be able to edit B2 = so if it says "World" - I can type in something after it or within B2 it can be edited.

I have tried doing Outstring but can not figure out how to do it at a range.

Please help...

Hope this makes sense.

:rolleyes:
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
Sub test()
If Range("A1").Value = "Hello" Then Range("B2").Value = "World"
End Sub
 
Upvote 0

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Welcome to the Board.

Like this?

Code:
If Range("A2").Value = "Hello" Then
   Range("B2").Value = WorksheetFunction.Substitute(Range("B2").Value,"World","World Peace")
End If
 
Upvote 0

mags74

New Member
Joined
Jul 19, 2007
Messages
7
Thank you.

I am not able to edit the "World" though. I would like to be able to edit it if needed within the cell.
 
Upvote 0

mags74

New Member
Joined
Jul 19, 2007
Messages
7
thank you mr. poulsom

for some reason i am not able to get that to work. it does not fill in the cell with world or world peace.

and thank you for the welcome. this is pretty neat i must say.
 
Upvote 0

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
You can't edit a cell while a VBA procedure is running. But you can use the InputBox function to prompt for a value and put it in a cell.
 
Upvote 0

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
thank you mr. poulsom

for some reason i am not able to get that to work. it does not fill in the cell with world or world peace.

and thank you for the welcome. this is pretty neat i must say.

The code I posted replaces the word World with World Peace if B2 already contains it.

Maybe you want:

Code:
If Range("A2").Value = "Hello" Then 
   If Instr(1,Range("B2").Value,"World") > 0 Then
      Range("B2").Value = WorksheetFunction.Substitute(Range("B2").Value,"World","World Peace") 
   Else
      Range("B2").Value = Range("B2").Value & " World"
   End If
End If
 
Upvote 0

Forum statistics

Threads
1,191,121
Messages
5,984,765
Members
439,909
Latest member
daigoku

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
Top