x404.co.uk
http://www.x404.co.uk/forum/

Excel 2003 Return statment won't work?
http://www.x404.co.uk/forum/viewtopic.php?f=4&t=2213
Page 1 of 1

Author:  forquare1 [ Mon Aug 10, 2009 12:51 pm ]
Post subject:  Excel 2003 Return statment won't work?

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

Author:  big_D [ Mon Aug 10, 2009 3:45 pm ]
Post subject:  Re: Excel 2003 Return statment won't work?

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

Author:  forquare1 [ Mon Aug 10, 2009 5:21 pm ]
Post subject:  Re: Excel 2003 Return statment won't work?

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.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/