Reply to topic  [ 3 posts ] 
Excel 2003 Return statment won't work? 
Author Message
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
Hi all,

I've got this code in a Macro within Excel 2003 (VB 6.3)
Code:
Public Function level1() As String
'
' test1 Macro
' Macro recorded 10/08/2009 by Ben Lavery
'
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "Level 1"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.Find.Execute
    Selection.EndKey Unit:=wdLine
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
    Selection.Copy
   
End Function


I want to return the selected text rather than copying it. I added this to the bottom of my function:
Code:
Return Selection.Text
End Function


However it highlights "Selection" and comes up with a syntax error. I've swapped it for "Hello" (With quotes) and "myText" which was a string variable, but I keep ketting syntax errors (no more helpful than that...

Can anyone help?

Many thanks,
Ben


Mon Aug 10, 2009 12:51 pm
Profile WWW
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
Return doesn't take parameters!

This is VisualBasic, not C or Java.

You need to assign the string to "level1".

Code:
Public Function level1() As String
    Selection.Find.ClearFormatting

    With Selection.Find
        .Text = "Level 1"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.Find.Execute
    Selection.EndKey Unit:=wdLine
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
    Selection.Copy
    level1 = Selection.Text
    Return
End Function

_________________
"Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari

Executive Producer No Agenda Show 246


Mon Aug 10, 2009 3:45 pm
Profile ICQ
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
Thanks Dave,

I should have deleted the topic, I worked around it by using global variables (ouch). For what I want it should be fine, and thankfully once I leave the code should never be needed again, so there should be little chance of anything bad happening from it.


Mon Aug 10, 2009 5:21 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 19 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.