Consulting

Results 1 to 5 of 5

Thread: Looping through worksheets question

  1. #1

    Looping through worksheets question

    Hi,

    I hope someone can help with this. I am trying to write a macro in Excel via Visual Basic and have hit a wall. I have a workbook with about 80 worksheets. I want to select a formula from sheet 1 (Index), and copy and paste it into certain places on evrey worksheet.

    The cell where the formula needs to go is different on each worksheet, there are two result lines in each worksheet and the formula is pasted three times in along the lines.

    My code (so far) will search through one sheet and replace the formula in the places I need it, however when I add code to tell it to do this in every sheet, it comes up with:

    Run-time error '91':
    Object variable or With block variable not set.

    It doesn't seem to like the cell.find....

    I have attached code below, does anyone have any ideas?

    Thanks

    [VBA]
    Sub_Find_and_Replace()
    '
    ' Find_and_Replace Macro
    ' Macro recorded 31/05/2007
    '
    Dim Current As Worksheet

    ' Copy formula
    Sheets("Index").Select
    Range("S1").Select
    Selection.Copy


    ' Select sheets, search for Results and paste formula

    For Each Current In Worksheets

    Cells.Find(What:="Result", After:=ActiveCell, LookIn:=xlValues, _
    LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext).Activate

    ActiveCell.Offset(0, 3).Select
    ActiveSheet.Paste

    ActiveCell.Offset(0, 3).Select
    ActiveSheet.Paste

    ActiveCell.Offset(0, 3).Select
    ActiveSheet.Paste

    Cells.FindNext(After:=ActiveCell).Activate
    Cells.FindNext(After:=ActiveCell).Activate

    ActiveCell.Offset(0, 3).Select
    ActiveSheet.Paste

    ActiveCell.Offset(0, 3).Select
    ActiveSheet.Paste

    ActiveCell.Offset(0, 3).Select
    ActiveSheet.Paste


    Next

    End Sub[/VBA]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub Find_and_Replace()
    '
    ' Find_and_Replace Macro
    ' Macro recorded 31/05/2007
    '
    Dim Current As Worksheet
    Dim oCell As Range
    Dim firstAddress As String
    Dim sSaved


    ' Copy formula
    sSaved = Worksheets("Index").Range("S1")

    ' Select sheets, search for Results and paste formula
    For Each Current In Worksheets

    With Current.Cells
    Set oCell = .Find(What:="Result", _
    After:=ActiveCell, _
    LookIn:=xlValues, _
    LookAt:=xlPart, _
    SearchOrder:=xlByColumns, _
    SearchDirection:=xlNext)

    If Not oCell Is Nothing Then
    firstAddress = oCell.Address
    oCell.Offset(0, 3).Value = sSaved
    Do
    Set oCell = .FindNext(After:=oCell)
    If Not oCell Is Nothing Then
    If oCell.Address <> firstAddress Then
    oCell.Offset(0, 3) = sSaved
    End If
    End If
    Loop Until oCell Is Nothing Or firstAddress = oCell.Address
    End If
    End With
    Next Current

    End Sub
    [/vba]

  3. #3
    Alternatively you could just select the first sheet, shift click the last sheet, select the cell and Type it in once !
    2+2=9 ... (My Arithmetic Is Mental)

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi FGC,
    Welcome to VBAX,
    FYI if you select your code and select the VBA button, rather than "code", it will format as shown.
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Quote Originally Posted by unmarkedheli
    Alternatively you could just select the first sheet, shift click the last sheet, select the cell and Type it in once !
    The cell where the formula needs to go is different on each worksheet, there are two result lines in each worksheet and the formula is pasted three times in along the lines.

Posting Permissions

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