1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Excel, running a scipt on multiple sheets

Discussion in 'Business Applications' started by Brian Rohan, Apr 7, 2009.

Thread Status:
Not open for further replies.
  1. Brian Rohan

    Brian Rohan Thread Starter

    Joined:
    Mar 30, 2009
    Messages:
    66
    Hello,

    I have a spreadsheet with multiple sheet to it. I have a VB script that runs on the currently activated sheet. How do I get the script to run on all of the sheets in the spreadsheet?
     
  2. Brian Rohan

    Brian Rohan Thread Starter

    Joined:
    Mar 30, 2009
    Messages:
    66
    Below is the code I am running. I can get the script to work as I want the problem is that it take WAY too long. When I run the test script, it takes about 30 seconds to run on the currently active sheet. When I add the Allsheets code to the top to run a loop, it took an hour, and it had only gotten through half of the sheets, 11 of the total 21 sheets (they were done correctly BTW). What do I need to do with the looping code to speed it up? In theory if a single sheet took only 30 seconds, then 20 sheets should only take 10 minutes, not a couple of hours.

    Thanks!



    Sub AllSheets()

    Dim Y As Integer
    Y = 1

    Do While Y < 22
    Sheets(Y).Activate
    Call test
    Y = Y + 1
    Loop

    End Sub



    Sub test()

    Application.ScreenUpdating = False

    x = Range("A" & Rows.Count).End(xlUp).Row

    Range("C2") = "Active"
    For Each Cell In Range("A1:A" & x)
    If Cell = " DETACHD" Then
    Range("C" & Rows.Count).End(xlUp).Offset(1) = WorksheetFunction.Trim(Cell.Offset(-1))
    Range("C" & Rows.Count).End(xlUp).Offset(, 1) = Cell.Offset(7)
    ElseIf Cell = "Bumpable Buyer " Then
    Range("C" & Rows.Count).End(xlUp).Offset(1) = "Bumpable Buyer"
    ElseIf Cell = "Canceled " Then
    Range("C" & Rows.Count).End(xlUp).Offset(1) = "Canceled"
    ElseIf Cell = "Expired " Then
    Range("C" & Rows.Count).End(xlUp).Offset(1) = "Expired"
    ElseIf Cell = "Pending " Then
    Range("C" & Rows.Count).End(xlUp).Offset(1) = "Pending"
    ElseIf Cell = "Sold " Then
    Range("C" & Rows.Count).End(xlUp).Offset(1) = "Sold"
    ElseIf Cell = "Withdrawn " Then
    Range("C" & Rows.Count).End(xlUp).Offset(1) = "Withdrawn"
    End If
    Next Cell
    Columns("C:D").AutoFit

    x = Range("C" & Rows.Count).End(xlUp).Row
    Range("B2:B" & x).FormulaR1C1 = "=IF(RC[2]="""",RC[1],R[-1]C)"
    Columns(1).Delete
    Columns(1).Value = Columns(1).Value
    Range("D1:D" & x).FormulaR1C1 = "=IF(OR(RC[-3]="""",RC[-3]=RC[-2]),1,""X"")"

    Columns(4).SpecialCells(xlCellTypeFormulas, 1).EntireRow.Delete
    Columns(4).Delete
    Range("A1:C1").Insert Shift:=xlDown
    Range("A1") = "Category"
    Range("B1") = "ID"
    Range("C1") = "Amount"

    x = Range("C" & Rows.Count).End(xlUp).Row
    For Each Cell In Range("C2:C" & x)
    If InStr(Cell, "-") <> 0 Then
    Cell.Value = Left(Cell, InStr(Cell, "-") - 2)
    End If
    Next Cell

    Range("E1") = 0
    Range("F1") = 199999
    Range("G1") = 249999
    Range("H1") = 349999
    Range("I1") = 499999
    Range("J1") = 749999
    Range("K1") = 999999
    Range("L1") = 99999000
    Range("E2") = "Active"
    Range("E3") = "Bumpable Buyer"
    Range("E4") = "Canceled"
    Range("E5") = "Expired"
    Range("E6") = "Pending"
    Range("E7") = "Sold"
    Range("E8") = "Withdrawn"

    Range("F2").FormulaR1C1 = _
    "=SUMPRODUCT((Category=RC5)*(Amount>R1C[-1])*(Amount<=R1C))"

    Range("F2").Copy Range("F2:L8")

    Application.ScreenUpdating = True

    End Sub
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/816548

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice