# VBA coding help - How do I say "If there is nothing selected"

**URL:** <https://boards.straightdope.com/t/vba-coding-help-how-do-i-say-if-there-is-nothing-selected/510122>\
**Category:** Factual Questions\
**Created:** [September 15, 2009, 1:36am UTC](https://boards.straightdope.com/t/vba-coding-help-how-do-i-say-if-there-is-nothing-selected/510122 "2009-09-15T01:36:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chessic\_Sense](https://avatars.discourse-cdn.com/v4/letter/c/7c8e57/32.png) [@Chessic\_Sense](https://boards.straightdope.com/u/Chessic_Sense)\
**Post date:** [September 15, 2009, 1:36am UTC](https://boards.straightdope.com/t/vba-coding-help-how-do-i-say-if-there-is-nothing-selected/510122/1 "2009-09-15T01:36:34Z")

</div>

I was able to build the code that I asked for in [this](http://boards.straightdope.com/sdmb/showthread.php?t=529699)thread. Turns out, I can just paste the webpage into Word and run the macro there. Now I’m hitting a snag. I have a document that’s a mix of tables and text. Part of the document goes something like

> [@](#):
>
> Paint Price: $32  
> Color: Red  
> Amount: 6 gallons

I want to highlight “Red” and copy it. Part of the macro I’ve built goes

> [@](#):
>
> Selection.find.text “Color:”  
> Selection.find.execute  
> Selection.moveright count:=2  
> Selection.endkey extend:=wdExtend  
> Selection. Moveleft extend:=wdExtend  
> Selection.copy

This gets my cursor into the right position, then highlights what I need, then backs up a character so I don’t copy the return at the end of the line. This leaves me with the color. Usually, it’s highlighting one word. But sometimes the line is blank so it ends up copying nothing because Endkey takes the highlight one character to the right, then then MoveLeft undoes that. This gives me an error that terminates the macro. So I tried inserting an If…Then where it types out a message for me to copy, a la:

> [@](#):
>
> Selection.find.text “Color:”  
> Selection.find.execute  
> Selection.moveright count:=2  
> Selection.endkey extend:=wdExtend  
> Selection. Moveleft extend:=wdExtend  
> If IsNull(Selection.Text) = True Then  
> Selection.typetext “No Data Given”  
> Selection.HomeKey Extend:=wdExtend  
> End If  
> Selection.copy

But that doesn’t work. If I hover over Selection.Text, it says it’s equal to two squares (which I don’t know how to render on the Dope). So maybe I have to use = “”? I’ve tried changing it to all of the following, none of which work:

If Selection.Text \<\> 0 Then  
If Selection \<\> 0 Then  
If Selection is Null Then  
If Selection = “” Then  
and a bunch of other combinations like setting the selection to a variable, then trying that in the above statements.

So what’s the right way to say “If there is nothing selected, then write a sentence and highlight that”? I don’t want the return on the end of the line unless I absolutely have to, because I need to paste the result elsewhere sans the extra return, so please try to avoid getting rid of the “MoveLeft” line.

---

<div class="post-metadata">

**Author:** ![mecaenas](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@mecaenas](https://boards.straightdope.com/u/mecaenas)\
**Post date:** [September 15, 2009, 1:43pm UTC](https://boards.straightdope.com/t/vba-coding-help-how-do-i-say-if-there-is-nothing-selected/510122/2 "2009-09-15T13:43:44Z")

</div>

I don’t know VBA but I know VBScript, I’d suggest testing if Selection = Nothing and if that doesn’t work then does selection.text have a length? If so you can try testing length = 0

---

<div class="post-metadata">

**Author:** ![Don\_t\_Call\_Me\_Shirley](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@Don\_t\_Call\_Me\_Shirley](https://boards.straightdope.com/u/Don_t_Call_Me_Shirley)\
**Post date:** [September 15, 2009, 2:59pm UTC](https://boards.straightdope.com/t/vba-coding-help-how-do-i-say-if-there-is-nothing-selected/510122/3 "2009-09-15T14:59:50Z")

</div>

> [@Chessic\_Sense](#):
>
> I was able to build the code that I asked for in [this](http://boards.straightdope.com/sdmb/showthread.php?t=529699)thread. Turns out, I can just paste the webpage into Word and run the macro there. Now I’m hitting a snag. I have a document that’s a mix of tables and text. Part of the document goes something like  
> I want to highlight “Red” and copy it. Part of the macro I’ve built goes
> 
> This gets my cursor into the right position, then highlights what I need, then backs up a character so I don’t copy the return at the end of the line. This leaves me with the color. Usually, it’s highlighting one word. But sometimes the line is blank so it ends up copying nothing because Endkey takes the highlight one character to the right, then then MoveLeft undoes that. This gives me an error that terminates the macro. So I tried inserting an If…Then where it types out a message for me to copy, a la:  
> But that doesn’t work. If I hover over Selection.Text, it says it’s equal to two squares (which I don’t know how to render on the Dope). So maybe I have to use = “”? I’ve tried changing it to all of the following, none of which work:
> 
> If Selection.Text \<\> 0 Then  
> If Selection \<\> 0 Then  
> If Selection is Null Then  
> If Selection = “” Then  
> and a bunch of other combinations like setting the selection to a variable, then trying that in the above statements.
> 
> So what’s the right way to say “If there is nothing selected, then write a sentence and highlight that”? I don’t want the return on the end of the line unless I absolutely have to, because I need to paste the result elsewhere sans the extra return, so please try to avoid getting rid of the “MoveLeft” line.

The first thing I would try is the text length suggested by mecaenas. Then I would try putting the "If’ statement before the copy. If there is nothing there, don’t even select or copy it. The third thing would be to have a default color value, like “none” in my table, so the color field would never be blank.

ETA: Have you tried Selection.value? Can’t remember if that’s valid or not.

---

<div class="post-metadata">

**Author:** ![Chessic\_Sense](https://avatars.discourse-cdn.com/v4/letter/c/7c8e57/32.png) [@Chessic\_Sense](https://boards.straightdope.com/u/Chessic_Sense)\
**Post date:** [September 15, 2009, 3:18pm UTC](https://boards.straightdope.com/t/vba-coding-help-how-do-i-say-if-there-is-nothing-selected/510122/4 "2009-09-15T15:18:57Z")

</div>

So what’s the syntax for the length = 0 and selection = nothing ideas? I’ll give them a shot.

I can’t make a default in the table because these tables were written a long time ago in a country far far away, and there are thousands upon thousands of them. The idea of the code was to write in something as a default when I came across a blank one.

---

<div class="post-metadata">

**Author:** ![Don\_t\_Call\_Me\_Shirley](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@Don\_t\_Call\_Me\_Shirley](https://boards.straightdope.com/u/Don_t_Call_Me_Shirley)\
**Post date:** [September 15, 2009, 3:33pm UTC](https://boards.straightdope.com/t/vba-coding-help-how-do-i-say-if-there-is-nothing-selected/510122/5 "2009-09-15T15:33:17Z")

</div>

This is what I would do. Disclaimer- I am totally self-taught and usually end up doing things the really, really hard way.

I would create a userform with a textbox and paste the result into that textbox. Then you can use “If textbox1.value= “” then…” command. Make the userform invisible.

---

<div class="post-metadata">

**Author:** ![Chessic\_Sense](https://avatars.discourse-cdn.com/v4/letter/c/7c8e57/32.png) [@Chessic\_Sense](https://boards.straightdope.com/u/Chessic_Sense)\
**Post date:** [September 15, 2009, 3:48pm UTC](https://boards.straightdope.com/t/vba-coding-help-how-do-i-say-if-there-is-nothing-selected/510122/6 "2009-09-15T15:48:35Z")

</div>

I’ve tried the len(Selection) idea, but it says that the length is 2, because Selection = (my best rendering of those two squares. If someone could tell me how to make those squares, I could just say “If Selection = ” and be done with it.

---

<div class="post-metadata">

**Author:** ![ZipperJJ](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/zipperjj/32/211_2.png) [@ZipperJJ](https://boards.straightdope.com/u/ZipperJJ)\
**Post date:** [September 15, 2009, 4:00pm UTC](https://boards.straightdope.com/t/vba-coding-help-how-do-i-say-if-there-is-nothing-selected/510122/7 "2009-09-15T16:00:38Z")

</div>

The problem is that those squares are something. As you’ve found, the value is not “” nor is it null and nor is its length 0.

You’re going to have to dig to see what the squares represent - they could be the tab character, the VbCrLf (line break) character, the space character, or anything else ASCII 0-32.

Check out this tutorial: [Situs Slot Gacor Gampang Menang Hari Ini Maxwin Slot Online Terpercaya 2023 \> Slot88](http://www.dreamincode.net/forums/showtopic12952.htm) It shows you how to figure out what sort of characters are in a string. It might give you some insight to make some code that checks to see if the characters are NOT letters or numbers, then selection = blank. Or, google “vb characters” to find some more info that can help you.

---

<div class="post-metadata">

**Author:** ![ProfessorComputer](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@ProfessorComputer](https://boards.straightdope.com/u/ProfessorComputer)\
**Post date:** [July 22, 2015, 4:29pm UTC](https://boards.straightdope.com/t/vba-coding-help-how-do-i-say-if-there-is-nothing-selected/510122/8 "2015-07-22T16:29:34Z")

</div>

Sorry for the mega-necropost, but after three years of writing stupid VBA macros, I finally stumbled upon the answer to OP’s question. I figure if I can spare anyone else the frustration of dealing with Word’s unintuitive Selection object, it’s worth it.

```auto

 If Word.Selection.Type = wdSelectionIP Then
     MsgBox "Nothing is selected"
 Exit Sub

```

:smack:
