2008/2/10, Jonathan Kaye <[EMAIL PROTECTED]>:
>
> Hi all,
> Trying to write these Basic macros is extremely frustrating. I'm sure I'm
> missing some basic documentation somewhere but I can't find it for the
> life
> of me.
> I have written the follow macro:
> Function SimpSet(ProcessString) As String
>         Dim Position As Integer
>         Position = InStr(ProcessString, ".")
>         SimpSet = mid(ProcessString, Position)
> end


This is what I use to do when testing functions that doesn't work:
Write a testprogram above the function (just because the first procedure
will run when running in the API window), in this case something like this:
sub main
     dim test as string
     test=SimpSet("abc.def")
     print test
end sub

Now, when trying to run, the error will appear and when going back to the
API window, a place near where the error occured will be highlighted.

Also, a good thing to do is to type Option explicit above everything. This
gives you a compilation error (kind of) when you use a variable name that is
not declared (for exampled when you spell a name wrong). This is good,
because it can be very hard to find where the problem is in those cases, if
no error messages appears.

Here's the "complete" code that I would have typed for the whole thing
(before correcting the errors in the code):
Option explicit

sub main
     dim test as string
     test=SimpSet("abc.def")
     print test
end sub

Function SimpSet(ProcessString As String) As String
        Dim Position As Integer
        Position = InStr(ProcessString, ".")
        SimpSet = mid(ProcessString, Position)
end

Now let's do a test run:
Press F5 to run the main program within the API window. Oops, it complains
immediately: No "end function", so we correct it:

Option explicit

sub main
     dim test as string
     test=SimpSet("abc.def")
     print test
end sub

Function SimpSet(ProcessString As String) As String
        Dim Position As Integer
        Position = InStr(ProcessString, ".")
        SimpSet = mid(ProcessString, Position)
end function

Press F8 again.
A box appears and the text is ".def". As I understood, you wanted "abc" as
the result in this case. Okay, let's do some changes in the code:

Option explicit

sub main
     dim test as string
     test=SimpSet("abc.def")
     print test
end sub

Function SimpSet(ProcessString As String) As String
        Dim Position As Integer
        Position = InStr(ProcessString, ".")
        SimpSet = mid(ProcessString, 1, Position-1)
end function

In this case "." was found at position 4 and we want the characters to the
left of it, which is characters 1-3. The syntax of the mid function is:
mid(InputString, StartPosition, LengthOfOutputString), so the line above
will give "abc" in the example above.

Now you can try the macro on a cell, so I'll now enter the following:
A1: abc.def
B1: =simpset(A1)

B1 now displays: abc

The thing that worries me a bit is that I didn't get the same error message
as you did. No matter how I tried, it said nothing about optional arguments.
Strange.

J.R.

In cell A1 I have abc.def In cell B1 I have =SimpSet(A1)
> My objective is to wind up with abc in cell B1. When I run the macro I
> get "BASIC runtime error. Argument is not optional. I can find no
> reference
> to this extremely informative error message and I have no idea how to
> debug
> the code. Is there anyone out there who can:
> 1. Tell me what the error is and where exactly I can find out that kind of
> information so I can do it myself in the future?
> Thanks,
> Jonathan
> --
> Registerd Linux user #445917 at http://counter.li.org/
> Please do not send me copies of list mail. I read the lists. Thanks!
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>

Reply via email to