+ Reply to Thread
Results 1 to 5 of 5

with statement

Hybrid View

  1. #1
    Valued Forum Contributor Blake 7's Avatar
    Join Date
    10-01-2010
    Location
    Bogota, Colombia
    MS-Off Ver
    Excel 2010 64 bit and Excel 2007,
    Posts
    1,377

    with statement

    Hi Guys,

    I wonder if someone could take the time to help..

    I would like to amend this code slightly by forcing it to perform the exact same function on an additional tab called Forecast.

    So where the code says With Worksheets("Drill Down") I want it to say With Worksheets("Drill Down") and worksheet Forecast, however I am not having much success!

    Maybe it's something like

    Worksheets("Drill Down").select
    WorkSheets("Forecast").select

    Thanks for looking

    Option Explicit
    
    Sub dRILLDOWN()
     Dim x, i As Long, y, ii&, j&, k&
    Application.ScreenUpdating = 0
      With Worksheets("Drill Down")
        y = .Range("F8:S" & .Cells.Find("*", , , , xlByRows, xlPrevious).Row)
    End With
     With Worksheets("Source Data")
        x = .Range("A1:B" & .Cells.Find("*", , , , xlByRows, xlPrevious).Row)
    End With
     
    With Worksheets("Drill Down")
         
        For i = 2 To UBound(y, 1)
             For j = 1 To UBound(x)
                If y(i, 2) = "Actual" Then
             
                    If y(i, 1) = x(j, 1) Then
                         For ii = j + 1 To UBound(x)
                              k = 7
                               Do Until x(ii, 1) = "TOTAL"
                                    k = k + 1
                                    .Cells(i + 7, k) = x(ii, 2)
                                    ii = ii + 1
                                Loop
                               Exit For
                         Next
                    End If
                End If
            Next
        Next
    End With
     
    Application.ScreenUpdating = True
    End Sub
    Last edited by Blake 7; 02-27-2014 at 05:04 PM.
    Blake 7

    If your question has been answered, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If any member's response has helped to solve your problem, please consider using the scales icon on the top right of their post to show your appreciation.

    http://failblog.org/

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: with statement

    Add the following lines of code to your Dimensions

    Dim SheetsArray as Sheets
    Set SheetsArray = ActiveWorkbook.Sheets(Array("Drill Down", "Forecast"))
    Dim SheetObject as Worksheet
    Replace the With Statement with
    For each SheetObject in SheetArray
    Replace the End With Statement with
    Next SheetObject
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,777

    Re: with statement

    The minimal change to make to do this operation on two different sheets is to add the worksheet as an argument to your sub, then call it twice. See code highlighted in red. Did I understand the question?

    Sub Drill2()
       dRILLDOWN Worksheets("Drill Down")
       dRILLDOWN Worksheets("Forecast")
    End Sub
    
    Sub dRILLDOWN(WS As Worksheet)
     Dim x, i As Long, y, ii&, j&, k&
    Application.ScreenUpdating = 0
      With WS
        y = .Range("F8:S" & .Cells.Find("*", , , , xlByRows, xlPrevious).Row)
    End With
     With Worksheets("Source Data")
        x = .Range("A1:B" & .Cells.Find("*", , , , xlByRows, xlPrevious).Row)
    End With
     
    With WS
         
        For i = 2 To UBound(y, 1)
             For j = 1 To UBound(x)
                If y(i, 2) = "Actual" Then
             
                    If y(i, 1) = x(j, 1) Then
                         For ii = j + 1 To UBound(x)
                              k = 7
                               Do Until x(ii, 1) = "TOTAL"
                                    k = k + 1
                                    .Cells(i + 7, k) = x(ii, 2)
                                    ii = ii + 1
                                Loop
                               Exit For
                         Next
                    End If
                End If
            Next
        Next
    End With
     
    Application.ScreenUpdating = True
    End Sub
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: with statement

    may be try:
    Sub dRILLDOWN()
    call realdRILLDOWN("Drill Down")
    call realdRILLDOWN("Forecast")
    end sub
    
    
    Sub realdRILLDOWN(mysheet as string)
     Dim x, i As Long, y, ii&, j&, k&
    Application.ScreenUpdating = 0
      With Worksheets("Drill Down") 'may be here  With Worksheets(mysheet)
        y = .Range("F8:S" & .Cells.Find("*", , , , xlByRows, xlPrevious).Row)
    End With
     With Worksheets("Source Data")
        x = .Range("A1:B" & .Cells.Find("*", , , , xlByRows, xlPrevious).Row)
    End With
     
    With Worksheets(mysheet)
     ' the rest
    edit: almost the same way ;-) but as always slightly different way to skin the cat.
    Notice other type of argument, other way to call procedure with argument and my doubts about serching for value of y - shall it be always taken from Drill Down sheet? Can imagine that it shall do this way. Or shall it be determined as other value for Forecatst sheet - also pausible.
    Last edited by Kaper; 02-27-2014 at 04:56 PM.
    Best Regards,

    Kaper

  5. #5
    Valued Forum Contributor Blake 7's Avatar
    Join Date
    10-01-2010
    Location
    Bogota, Colombia
    MS-Off Ver
    Excel 2010 64 bit and Excel 2007,
    Posts
    1,377

    Re: with statement

    Awesome - many thanks guys for taking the time to help. I appreciate the different solutions! Solved!

    Jeff, you did understand!! good to hear from you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] If statement that will look among multiple criteria and generate a statement
    By liz5818 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-01-2013, 04:12 PM
  2. VBA Compile Error : line number or label or statement or end of statement
    By excellearner121 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-04-2013, 06:41 PM
  3. Replies: 4
    Last Post: 06-01-2012, 10:05 AM
  4. Replies: 4
    Last Post: 05-16-2012, 05:33 PM
  5. [SOLVED] Utilize a Select Case Statement in Target Intersect Statement
    By max57 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-29-2009, 08:55 PM

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