+ Reply to Thread
Results 1 to 6 of 6

GoTo code

Hybrid View

  1. #1
    Registered User
    Join Date
    09-13-2008
    Location
    Barrie, ON, CA
    Posts
    47

    GoTo code

    Hello,

    How do I use the GoTo so that I can Specify A line of Code to go to...

    for example

    On Error Goto Ln 43
    Is this posible??
    Last edited by RBI; 10-30-2008 at 09:09 AM. Reason: mark as solved

  2. #2
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Use the same code posted above, but then, where your reference is, you need to include the description. Example:

    Sub Test()
    
        If Date > Date Then GoTo imessedup
        
        MsgBox "Today is greater than today?!?"
        Exit Sub
        
    imessedup:
        MsgBox "Duh! Today can't be greater than today!"
    
    End Sub
    You can also do the same by using On Error GoTo imessedup

    HTH

    Jason

  3. #3
    Registered User
    Join Date
    09-13-2008
    Location
    Barrie, ON, CA
    Posts
    47
    Ya but does this work If the GoTO refers to a line that is before it

    ex

    Private Sub CommandButton4_Click()
    Dim Ump As String
    Dim Mns As String
    Dim Pth As String
    Dim Mnt As String
    Dim Fln As String
    Ump = Application.InputBox(Prompt:="Please Enter Cheque Date")
    Mnt = Month(Ump)
    Range("E54").Value = Mnt
    Mns = Range("F54").Value
    Pth = "Q:\CRS PAID FILE (Lai-Yin)\2008\" & Range("D42").Value & "_" & Mns
    
    Range("E86").Select
    Tst:  Windows("Operating Leases 08").activate   
            Do While ActiveCell <> "End"
            ActiveCell.Offset(1, 0).Select
            Fln = ActiveCell.Value
                ChDir Pth
            On Error GoTo Tst
                Workbooks.Open Filename:= _
                    Pth & "\" & Fln
            
            Range("F:F").Select
           On Error GoTo Nxt
          Selection.Find(What:="GE FLEET SERVICES", After:=ActiveCell, LookIn:=xlFormulas _
          , LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
          MatchCase:=False, SearchFormat:=False).Activate
        Exit Sub
    Loop
    Exit Sub
    Nxt:    ActiveWindow.Close
            GoTo Tst
    End Sub
    You see the macro is just to open a file, search for a value then exit if it finds it, if not it closes and goes to the next but it's not working yet... The first Goto is to go back to the start if the filename can not be found t ofind the next file on the list, The secod Goto is to close the current file and go back to the list if the value being searchd for cannot be found... another problem is that i don't know if, when the "find" function doens't find anything is that an error?? cause if it's not then the second Goto won't work....


    Any Help on these issues is apprecieated
    Last edited by RBI; 10-24-2008 at 03:46 PM.

  4. #4
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    I wasn't able to test it due to the amount of work that would be involved in replicating your situation, but try this code to see if it works:

    Private Sub CommandButton4_Click()
        Dim Ump As String
        Dim Mns As String
        Dim Pth As String
        Dim Mnt As String
        Dim Fln As String
        Dim Wkb As Workbook
        Dim Wks As Worksheet
        Dim Rng As Range
        Dim c As Range
        Dim i As Long
        
        Ump = Application.InputBox(Prompt:="Please Enter Cheque Date", Type:=1)
        Mnt = Month(Ump)
        Range("E54").Value = Mnt
        Mns = Range("F54").Value
        Pth = "Q:\CRS PAID FILE (Lai-Yin)\2008\" & Range("D42").Value & "_" & Mns
        Set Wks = ActiveSheet
        i = 87
        
        With Wks
            While .Cells(i, 5) <> "End"
                Fln = .Cells(i, 5).Value
                ChDir Pth
                Set Wkb = Workbooks.Open(Filename:= _
                    Pth & "\" & Fln)
                If Not Wkb Is Nothing Then
                    Set Rng = Wkb.ActiveSheet.Columns("F:F")
                    Set c = Rng.Find(What:="GE FLEET SERVICES", After:=Rng.Resize(1, 1). _
                        Offset(0, -1), LookIn:=xlFormulas, LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
                        SearchFormat:=False)
                    If c Is Nothing Then Wkb.Close
                    c.Activate
                    Exit Sub
                End If
                i = i + 1
            Wend
        End With
    End Sub
    Jason
    Last edited by jasoncw; 10-24-2008 at 07:31 PM.

  5. #5
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    In the code in post #3
    change one line
    Nxt:    
            ActiveWindow.Close
            Resume Tst
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  6. #6
    Registered User
    Join Date
    09-13-2008
    Location
    Barrie, ON, CA
    Posts
    47

    Calling Macros

    Sorry Wrong Spot
    Last edited by RBI; 10-27-2008 at 10:12 AM. Reason: Wrong Post

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1