Results 1 to 6 of 6

Thread: [RESOLVED] Switching worksheets

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,535

    Resolved [RESOLVED] Switching worksheets

    Hi,
    Becase of the way my project has expanded, I want to switch worksheets
    in the middle of a For-Next Loop.

    The trigger is when the Loop index reaches 13921.

    However the line,

    If Indx > 13921 Then With Worksheets("Sheet2")

    invokes

    Compile error: End If without block If

    Can this be circumvented, or should I try a different approach?

    Thanks, ABB

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Switching worksheets

    is this a for each loop or for var =

    show the code for the loop and leading into the loop
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Switching worksheets

    That should be:
    Code:
    If Indx > 13921 Then 
        With Worksheets("Sheet2")
            ...
        End With
    End If
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,535

    Re: Switching worksheets

    Thanks very much Anhn and Westconn. It was a For-Next. I tried that Anhn but I wanted the same
    'with' code to execute regardless of Indx, but Indx to change With to point at another sheet at 13921.

    I ended up using a variable then
    If Indx > 13921 Then SheetIWant = "Sheet2"
    myvalue = Worksheets(SheetIWant).Cells(row, col)

    Seems to work...

    Regards, ABB

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Switching worksheets

    Code:
    If Indx > 13921 Then myvalue = Worksheets("Sheet2").Cells(row, col)
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,535

    Thumbs up Re: [RESOLVED] Switching worksheets

    Thanks Koolsid, I should have known that.

    Regards, ABB

Posting Permissions

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



Click Here to Expand Forum to Full Width