Consulting

Results 1 to 7 of 7

Thread: With Statement with multiple arguments

  1. #1
    VBAX Newbie
    Joined
    Apr 2005
    Posts
    5
    Location

    With Statement with multiple arguments

    I am trying to perform a particular action in 3 worksheets. I dint want to write the code thrice. does anyone know what the right syntax is for the following

    I am trying to do this

    With Worksheets(xxx) & worksheets(yyy) & worksheets (zzz)
    Perform some action


    But it doesnt work, does anyone know how I can do this?

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Try something like this. the 1-3 in the example are sheets to be copied to a 4th sheet.

    For i = 1 To 3 
        Sheets(i).Select
        GoSub DoCopy
        GoSub DoPaste
        n = n + r
    Next i
    Last edited by Aussiebear; 04-27-2023 at 07:02 PM. Reason: Adjusted the code tags
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    hehe...he said GoSub

  4. #4
    Quote Originally Posted by lucas
    Try something like this. the 1-3 in the example are sheets to be copied to a 4th sheet.

    For i = 1 To 3 
    Sheets(i).Select
    GoSub DoCopy
    GoSub DoPaste
    n = n + r
    Next i
    Did u said n= n+r or
    it shuld be i=i+1
    Last edited by Aussiebear; 04-27-2023 at 07:03 PM. Reason: Adjusted the code tags
    A mighty flame followeth a tiny sparkle!!



  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I should have posted more of the code to avoid confusion.

    Sub ConsolLoop()
        Sheets(4).Select
        Cells.ClearContents
        r = 0
        n = 0
    For i = 1 To 3
        Sheets(i).Select
        GoSub DoCopy
        GoSub DoPaste
        n = n + r
        Next i
    Exit Sub
    DoCopy:
        Cells(1, 1).CurrentRegion.Select
        Selection.Copy
        r = Selection.Rows.Count
    Return
    DoPaste:
        Sheets(4).Select
        Cells(1, 1).Offset(n, 0).Select
        ActiveSheet.Paste
        Return
    End Sub


    combines sheets 1-3 to sheet 4
    the next i is just telling it to go to the next sheet.
    I did say gosub....
    Last edited by Aussiebear; 04-27-2023 at 07:05 PM. Reason: Adjusted the code tags
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6

    Thumbs up

    Quote Originally Posted by lucas
    I should have posted more of the code to avoid confusion.

    Sub ConsolLoop()
        Sheets(4).Select
        Cells.ClearContents
        r = 0
        n = 0
    For i = 1 To 3
        Sheets(i).Select
        GoSub DoCopy
        GoSub DoPaste
        n = n + r
        Next i
    Exit Sub
    DoCopy:
        Cells(1, 1).CurrentRegion.Select
        Selection.Copy
        r = Selection.Rows.Count
        Return
    DoPaste:
        Sheets(4).Select
        Cells(1, 1).Offset(n, 0).Select
        ActiveSheet.Paste
        Return
    End Sub


    combines sheets 1-3 to sheet 4
    the next i is just telling it to go to the next sheet.
    I did say gosub....


    thanks for showing GoSub----Return statement..


    Chandan.
    Last edited by Aussiebear; 04-27-2023 at 07:06 PM. Reason: Adjusted the code tags

  7. #7
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    Hi anish201,
    and you could just combine your with statement with such a loop, the result could look like that:

    Option Explicit 
    Sub test()
       Dim i%
    For i = 1 To 3
       With Worksheets(i)
           Debug.Print .Name
        End With
       Next i
    End Sub

    But note that the code will work with one worksheet after another not all 3 of them at once.

    Daniel
    Last edited by Aussiebear; 04-27-2023 at 07:06 PM. Reason: Adjusted the code tags

Posting Permissions

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