Cindy Giovanetti
asked on
What's wrong with this code?
Could somebody tell me what's wrong with this code? I swear this macro worked yesterday. (And I just got it yesterday.) There are no changes in the code. I've checked it against the original. That seems to mean that maybe the code perceives a change in my system, which is not impossible. I have a programmable keyboard which I change a lot. However, it won't run with a regular keyboard either.
Can you tell me what kind of things to look at? What is this error message asking me to change?
Here's the code. The attached picture shows the location of the error.
Sub JumpSelectRight()
'
' Macro for SELECTING WORD LEFT-RIGHT
'
'
On Error GoTo msg
Char = Selection.Next(unit:=wdCha racter, Count:=1)
If Char = " " Then
Selection.MoveRight unit:=wdWord, Count:=1, Extend:=wdExtend
Else
Selection.MoveRight unit:=wdWord, Count:=1, Extend:=wdExtend
If right$(Selection.Text, 1) = " " Then
Selection.MoveRight unit:=wdCharacter, Count:=-1, Extend:=wdExtend
If right$(Selection.Text, 1) = " " Then
Selection.MoveRight unit:=wdCharacter, Count:=-1, Extend:=wdExtend
End If
End If
End If
Done:
Exit Sub
msg:
End Sub
Can you tell me what kind of things to look at? What is this error message asking me to change?
Here's the code. The attached picture shows the location of the error.
Sub JumpSelectRight()
'
' Macro for SELECTING WORD LEFT-RIGHT
'
'
On Error GoTo msg
Char = Selection.Next(unit:=wdCha
If Char = " " Then
Selection.MoveRight unit:=wdWord, Count:=1, Extend:=wdExtend
Else
Selection.MoveRight unit:=wdWord, Count:=1, Extend:=wdExtend
If right$(Selection.Text, 1) = " " Then
Selection.MoveRight unit:=wdCharacter, Count:=-1, Extend:=wdExtend
If right$(Selection.Text, 1) = " " Then
Selection.MoveRight unit:=wdCharacter, Count:=-1, Extend:=wdExtend
End If
End If
End If
Done:
Exit Sub
msg:
End Sub
what does selection.text contain when the error happens? if it is empty it will error, if it is a number then It might error out too
i meant selection.text
I added a declaration for your variable Char as a string, and tested your code in 64-bit Word 2016/Office 365. There were no errors.
That said, I have occasionally experienced issues with VB functions like Right$ not working in Excel. The problem in these cases acted like the VBA code had lost the reference to the VB library containing those functions. Rebooting the computer fixed the problem each time.
That said, I have occasionally experienced issues with VB functions like Right$ not working in Excel. The problem in these cases acted like the VBA code had lost the reference to the VB library containing those functions. Rebooting the computer fixed the problem each time.
Sub JumpSelectRight()
'
' Macro for SELECTING WORD LEFT-RIGHT
'
'
Dim char As String
On Error GoTo msg
char = Selection.Next(unit:=wdCharacter, Count:=1)
If char = " " Then
Selection.MoveRight unit:=wdWord, Count:=1, Extend:=wdExtend
Else
Selection.MoveRight unit:=wdWord, Count:=1, Extend:=wdExtend
If Right$(Selection.Text, 1) = " " Then
Selection.MoveRight unit:=wdCharacter, Count:=-1, Extend:=wdExtend
If Right$(Selection.Text, 1) = " " Then
Selection.MoveRight unit:=wdCharacter, Count:=-1, Extend:=wdExtend
End If
End If
End If
Done:
Exit Sub
msg:
End Sub
ASKER
I added your dim statement to my macro (and resaved Normal). It did not help.
I'm using this with Word only, not Excel.
In your code, above, the Right$ is capitalized, and that's how it was originally. But Word changes it to lower case. I've tried to capitalize it, but as soon as I take my cursor off it, Word lower cases it. And, yes, of course then Word points to that word as the error.
As to your question, what is the selection.text: This macro is supposed to select text. (It works almost like Ctrl-Arrow in Word, but not exactly.) So anything could be selected or to the right of the insertion point.
Thank you for offering to help!
I'm using this with Word only, not Excel.
In your code, above, the Right$ is capitalized, and that's how it was originally. But Word changes it to lower case. I've tried to capitalize it, but as soon as I take my cursor off it, Word lower cases it. And, yes, of course then Word points to that word as the error.
As to your question, what is the selection.text: This macro is supposed to select text. (It works almost like Ctrl-Arrow in Word, but not exactly.) So anything could be selected or to the right of the insertion point.
Thank you for offering to help!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OMG. I think you nailed it! Hang on!
Please post your document if you can.
ASKER
Martin, I went back and deleted the macro named right, and my selection macro suddenly works! By happenstance, I did create a macro just last night named right. It never occurred to me that that could interfere with my existing macro!
That was just such a weird blip because I swear that macro worked last night, and I had made no changes to it. But you nailed it. Thank you so much!
That was just such a weird blip because I swear that macro worked last night, and I had made no changes to it. But you nailed it. Thank you so much!
You should never use a reserved word (like "right") as the name of your own procedure.
In any case you’re welcome and I’m glad I was able to help.
If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.
Marty - Microsoft MVP 2009 to 2017
Experts Exchange Most Valuable Expert (MVE) 2015, 2017
Experts Exchange Top Expert Visual Basic Classic 2012 to 2018
Experts Exchange Top Expert VBA 2018
Experts Exchange Distinguished Expert in Excel 2018
In any case you’re welcome and I’m glad I was able to help.
If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.
Marty - Microsoft MVP 2009 to 2017
Experts Exchange Most Valuable Expert (MVE) 2015, 2017
Experts Exchange Top Expert Visual Basic Classic 2012 to 2018
Experts Exchange Top Expert VBA 2018
Experts Exchange Distinguished Expert in Excel 2018