+ Reply to Thread
Results 1 to 8 of 8

Do..Loop in multi sheets

  1. #1
    Registered User
    Join Date
    01-14-2004
    Posts
    22

    Do..Loop in multi sheets

    Hi all,
    I am writing a do loop vba, it is fine for applying one sheet. Now I want to write vba to automate the do loop program for several sheets. How can I do it?

    The following program is only appling to one sheet:
    Sub Sub_total()
    Range("J2").Select
    Do While IsEmpty(ActiveCell.Offset(0, -6)) = False
    ActiveCell.FormulaR1C1 = _
    "=IF(RC[-8]<>R[1]C[-8],SUMIF(R2C[-8]:RC[-8],RC[-8],R2C[-1]:RC[-1]),"""")"
    ActiveCell.Offset(1, 0).Select
    Loop
    End Sub

    I tried the follwoing code, but it failed.

    Sub Sub_total2l()
    For Each sh In ActiveWorkbook.Worksheets
    With sh
    .Range("J2").Select
    Do While IsEmpty(ActiveCell.Offset(0, -6)) = False
    ActiveCell.FormulaR1C1 = _
    "=IF(RC[-8]<>R[1]C[-8],SUMIF(R2C[-8]:RC[-8],RC[-8],R2C[-1]:RC[-1]),"""")"
    ActiveCell.Offset(1, 0).Select
    Loop
    End With
    Next sh
    End Sub

    norika

  2. #2
    Forum Contributor
    Join Date
    06-10-2004
    Location
    India
    Posts
    1,066
    Add the following line
    sh.Select
    after line 2 in your second sub.

    It should be

    Sub Sub_total2l()
    For Each sh In ActiveWorkbook.Worksheets
    sh.Select
    With sh
    .Range("J2").Select
    Do While IsEmpty(ActiveCell.Offset(0, -6)) = False
    ActiveCell.FormulaR1C1 = _
    "=IF(RC[-8]<>R[1]C[-8],SUMIF(R2C[-8]:RC[-8],RC[-8],R2C[-1]:RC[-1]),"""")"
    ActiveCell.Offset(1, 0).Select
    Loop
    End With
    Next sh
    End Sub



    Mangesh

  3. #3
    Registered User
    Join Date
    01-14-2004
    Posts
    22
    Thank you. It works.
    I have a minor question. In my case, vba applies for all sheets in active workbook. Is it possible to exclude some sheets not applying vba?

    TIA

    norika

  4. #4
    Forum Contributor
    Join Date
    06-10-2004
    Location
    India
    Posts
    1,066
    Yes. Try the following if statement:

    Sub Sub_total2l()
    For Each sh In ActiveWorkbook.Worksheets
    if sh.Name <> "Sheet1" then ' Sheet1 is then excluded
    sh.Select
    With sh
    .Range("J2").Select
    Do While IsEmpty(ActiveCell.Offset(0, -6)) = False
    ActiveCell.FormulaR1C1 = _
    "=IF(RC[-8]<>R[1]C[-8],SUMIF(R2C[-8]:RC[-8],RC[-8],R2C[-1]:RC[-1]),"""")"
    ActiveCell.Offset(1, 0).Select
    Loop
    End With
    end if
    Next sh
    End Sub


    Use or to include more sheets


    Mangesh

  5. #5
    Registered User
    Join Date
    01-14-2004
    Posts
    22
    Thank you very much.
    Where do I add 'or' ? I try to add 'or' in line 3
    if sh.Name <> "Sheet1" or "Sheet9" then .... but it failed.

    TIA
    norika

  6. #6
    Registered User
    Join Date
    01-31-2005
    Posts
    11
    Logical statements have to be output fully i.e.

    sh.Name <> "Sheet1" or sh.Name <> "Sheet9"

  7. #7
    Forum Contributor
    Join Date
    06-10-2004
    Location
    India
    Posts
    1,066
    Hi Norika,

    in your case you'll have to use an AND

    if ((sh.Name <> "Sheet1") AND (sh.Name <> "Sheet9")) then

    you could add more ANDs

    Mangesh

  8. #8
    Registered User
    Join Date
    01-14-2004
    Posts
    22
    Thank you very much. I learn much more.

    norika

+ 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