+ Reply to Thread
Results 1 to 2 of 2

Same thing in 2 different worksheets

Hybrid View

  1. #1
    Registered User
    Join Date
    05-05-2009
    Location
    San Diego, CA
    MS-Off Ver
    Excel 2003
    Posts
    97

    Same thing in 2 different worksheets

    I need to do lots of things to 2 different worksheets (an hours and a budget worksheet). Different data goes into each sheet, but the format needs to be the same. In fact, even the cell placement is the same between the sheets. Is there any way that I can easily access both sheets at the same time?

    I find myself doing this alot:
    worksheet1.range("A1:B2") = myBudgetarray
    worksheet2.range("A1:B2") = myHoursArray
    This is a simple example, in my macro, there is alot more going on, so the repetition is getting annoying and my routines are growing very large. Any advice?

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Same thing in 2 different worksheets

    Repetitive actions done sequentially across

    ROWS
    COLUMNS
    Sheets
    Books
    Sequentially designated Ranges

    All of these (and more) benefit from a FOR / NEXT loop. Read up on them.

    http://www.anthony-vba.kefra.com/vba...oop_Structures
    http://exceltip.com/st/Using_Loops_i...Excel/628.html (Exercise 6)

    Example:
    Sub DeleteAllPictures()
    Dim DrObj, Pic
    Set DrObj = ActiveSheet.DrawingObjects
    'ActiveSheet.Protect UserInterfaceOnly:=True  'Optional
    
        For Each Pic In DrObj
            If Left(Pic.Name, 7) = "Picture" Then Pic.Delete
        Next
    End Sub
    This repetitively evaluates the name of every object in a sheet, if it's a "picture", it deletes it.

    Another:
    Sub DeleteErrorB()
    Dim i As Integer, lastrow As Long
    Application.ScreenUpdating = False
    lastrow = Range("A" & Rows.Count).End(xlUp).Row
    
    For i = lastrow To 1 Step -1
        If WorksheetFunction.IsError(Cells(i, 2)) Then Rows(i).EntireRow.Delete (xlShiftUp)
    Next i
    
    Application.ScreenUpdating = True
    End Sub
    This repetitively check the value in column B for each row in a data set, deletes the rows that have error values.
    Last edited by JBeaucaire; 06-04-2009 at 03:40 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

+ 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