+ Reply to Thread
Results 1 to 5 of 5

Help Fixing Error on Print Macro

Hybrid View

groundin Help Fixing Error on Print... 03-08-2013, 04:56 PM
Leith Ross Re: Help Fixing Error on... 03-08-2013, 05:16 PM
groundin Re: Help Fixing Error on... 03-08-2013, 07:55 PM
groundin Re: Help Fixing Error on... 03-11-2013, 02:03 PM
groundin Re: Help Fixing Error on... 03-15-2013, 05:26 PM
  1. #1
    Forum Contributor
    Join Date
    11-25-2012
    Location
    Edmonton, Alberta
    MS-Off Ver
    2010
    Posts
    146

    Help Fixing Error on Print Macro

    I found this on Ron DeBruins site. Works great, until I tried to modify it...
    Basically I want it to print any hidden sheets that have a value in cell A600. I changed one line in the code and now get the error in the attached file on the line indicated in the code. Also added the original code so you can see the change I made

    Sub Print_All_Worksheets_With_Value_In_A600()
        Dim Sh As Worksheet
        Dim Arr() As String
        Dim N As Integer
        N = 0
        For Each Sh In ActiveWorkbook.Worksheets
            If Sh.Visible = False And Sh.Range("A600").Value <> "" Then  ' Original Code...If Sh.Visible = xlSheetVisible And Sh.Range("A1").Value <> "" Then
                N = N + 1
                ReDim Preserve Arr(1 To N)
                Arr(N) = Sh.Name
            End If
        Next
        With ActiveWorkbook
            .Worksheets(Arr).PrintOut  'This is the line hilighted as the error
        End With
    End Sub
    error.PNG

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Help Fixing Error on Print Macro

    Hello groundin,

    You need to change the With..End With portion of your code. You can use an Array with the Sheets object to select sheets in workbook but not the Worksheets object. here is the corrected code.
    Sub Print_All_Worksheets_With_Value_In_A600()
        Dim Sh As Worksheet
        Dim Arr() As String
        Dim N As Integer
        N = 0
        For Each Sh In ActiveWorkbook.Worksheets
            If Sh.Visible = False And Sh.Range("A600").Value <> "" Then  ' Original Code...If Sh.Visible = xlSheetVisible And Sh.Range("A1").Value <> "" Then
                N = N + 1
                ReDim Preserve Arr(1 To N)
                Arr(N) = Sh.Name
            End If
        Next
        With ActiveWorkbook
            .Sheets(Arr).PrintOut  'This is the line hilighted as the error
        End With
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    11-25-2012
    Location
    Edmonton, Alberta
    MS-Off Ver
    2010
    Posts
    146

    Re: Help Fixing Error on Print Macro

    Thanks, Changed it, but still getting same error...I was looking at the code, should i change
    For Each Sh In ActiveWorkbook.Worksheets
    worksheets here to sheets as well?

  4. #4
    Forum Contributor
    Join Date
    11-25-2012
    Location
    Edmonton, Alberta
    MS-Off Ver
    2010
    Posts
    146

    Re: Help Fixing Error on Print Macro

    Hi Again,
    I tried changing the .Worksheets to .Sheets, but still got the same error. Here is the existing code.


    Sub Print_All_Worksheets_With_Value_In_A600()
        Dim Sh As Worksheet
        Dim Arr() As String
        Dim N As Integer
        N = 0
        For Each Sh In ActiveWorkbook.Sheets
            If Sh.Visible = False And Sh.Range("A600").Value <> "" Then
                N = N + 1
                ReDim Preserve Arr(1 To N)
                Arr(N) = Sh.Name
            End If
        Next
        With ActiveWorkbook
            .Sheets(Arr).PrintOut 'This is where I get the debug error
        End With
    End Sub
    Any help would be appreciated.
    Thanks

  5. #5
    Forum Contributor
    Join Date
    11-25-2012
    Location
    Edmonton, Alberta
    MS-Off Ver
    2010
    Posts
    146

    Re: Help Fixing Error on Print Macro

    Hi All,
    Here is the code that ended up doing what I needed it to do. Thanks for your time.

    Sub Print_All_Worksheets_With_Value_In_A600()
        Dim Sh As Worksheet
        Dim Arr() As String
        Dim N As Integer
        N = 0
        Call PrintSlideSheet
        For Each Sh In ActiveWorkbook.Worksheets
            If Sh.Range("A600").Value <> "" Then
                N = N + 1
                ReDim Preserve Arr(1 To N)
                Arr(N) = Sh.Name
            End If
        Next
        With ActiveWorkbook
            For N = 1 To UBound(Arr)
    If ActiveWorkbook.Worksheets(Arr(N)).Visible = xlSheetHidden Then
    ActiveWorkbook.Worksheets(Arr(N)).Visible = xlSheetVisible
    ActiveWorkbook.Worksheets(Arr(N)).PrintOut
    ActiveWorkbook.Worksheets(Arr(N)).Visible = xlSheetHidden
    Else
    ActiveWorkbook.Worksheets(Arr(N)).PrintOut
    End If
    Next N
    
        End With
    End Sub

+ 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