VBA Code - Enter specific text if cell is blank

mickyflash

Board Regular
Joined
Jan 29, 2009
Messages
77
Hi I would like to have a VBA code that enters the word "search" into B20 if B20 is blank.


I do not want this to be a formula as I will require the user to enter text into B20


I know very little about VBA and have found the following codes that seem to do similar but I cant seem to adapt them, here is what I have below:


Code:
Option Explicit 
Sub RangeSetEmpty() 
    Replace_Blanks [b20], "Search"
End Sub 
 
Sub Replace_Blanks(rangeToEmpty As Range, replacementText As String) 
    Dim cell As Range 
    For Each cell In rangeToEmpty 
        If cell.Value = "" Then cell.Value = replacementText 
         'Application.Goto Reference:=rangeToEmpty
    Next cell 
End Sub


or


Code:
Sub Replace_Blanks() 
     
    Application.Goto Reference:="B20" 
    Selection.Replace What:="", Replacement:="Search",
LookAt:=xlPart, _ 
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ 
    ReplaceFormat:=False 
     
End Sub

Can anyone see what I am doing wrong?
 
It is usually better to start your own thread than it is to respond to a thread which is 8 years old!
By starting your own thread, it shows up as a new unanswered question on the "Unanswered threads" list, which stands a much better chance of being seen and answered.

That being said, I think you want something like this:
VBA Code:
Sub MyCheck()
    If Range("B4") <> "" And Range("F4") = "" Then Range("F4") = "X"
End Sub
Thanks for the help. When I tried that once, I was hit with "Please try and search other posts for your answer before starting a new one", so I found this one which was as close as possible to my inquiry and saw that the post had been replied to at various intervals (the original was 8 years with some back and forth within days, but then there was a new inquiry 4 years later and it was answered). I do not post very often as I try to figure out the answer for myself, so I am not aware of the whole posting etiquette. Seems like I am getting different ideas depending on who I interact with. Nevertheless, again, thank you for the help.
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Thanks for the help. When I tried that once, I was hit with "Please try and search other posts for your answer before starting a new one", so I found this one which was as close as possible to my inquiry and saw that the post had been replied to at various intervals (the original was 8 years with some back and forth within days, but then there was a new inquiry 4 years later and it was answered). I do not post very often as I try to figure out the answer for myself, so I am not aware of the whole posting etiquette. Seems like I am getting different ideas depending on who I interact with. Nevertheless, again, thank you for the help.
I can see how that is confusing. What the recommendation is trying to say is to search to see if you can find an answer first before posting anything. Many times, people can find the answer they need without having to post anything at all. Posting to old threads can be inefficient, as it won't be seen as much (for the reasons I mentioned in my previous post), and many times, the people involved in the original thread aren't that active here anymore. So it lowers your chance of getting a reply to post to the old thread, as opposed to creating a new one. Note that you can provide links in your question to old threads, if you find that helpful. I typically tell people only to post to the old one if their question is really about the original question, and their issue is EXACTLY the same (no "twists" or other differences).

I will bring it up to the Board to see if we can modify the language in that message, so it is more clearer. Thanks for bringing it to my attention.
 
Upvote 0
Can you tell me when/where you see this message?
"Please try and search other posts for your answer before starting a new one"

I see where a message that says:
"Has this question already been asked?"
but that makes no mention of starting a new post.

Also, when you do reply to an old thread, a message like this should pop-up:
1651241792822.png

advising you it is best to start a new thread.

Just trying to see if we have covered all our bases, or missed anything.
 
Upvote 0

Forum statistics

Threads
1,222,558
Messages
6,166,786
Members
452,070
Latest member
patbrunner2001

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