Print sheets depend on cell value

KlausW

Active Member
Joined
Sep 9, 2020
Messages
478
Office Version
  1. 2016
Platform
  1. Windows
Hi everyone

I use this VBA code to print tabs, except those in the VBA code. Now I would like to make it so that the VBA code only prints from the number in cell L1. Example I write 11 in L1, only tabs 11, 12, 13,14 up to 30 should be printed, if it says 2 in L1 it should only be tabs 2, 3,4,,5 up to 30. Of course there are not the same number of days in all months so some will also have 31.
I hope this makes sense, and some can help.
All help will be appreciated.

Best regards
Klaus W

VBA Code:
Sub Rektangelafrundedehjørner1_Klik()

Dim ws As Worksheet
 For Each ws In ActiveWorkbook.Worksheets
  If ws.Visible = True Then
  If ws.Name <> "Ark1" And _
    ws.Name <> "Stamdata" And _
    ws.Name <> "Ialt" And _
    ws.Name <> "Til BM" And _
    ws.Name <> "PRB" Then
       With ws.PageSetup
        .PrintArea = "a1:r20" ' USE YOUR PRINTAREA
        .Zoom = False
        .FitToPagesTall = 1
        .FitToPagesWide = 1
       End With
       ws.PrintOut
   End If
  End If
 Next ws

End Sub
 
I made a new code, which hopefully should work.

VBA Code:
Sub Rektangelafrundedehjørner1_Klik()

    Dim startArk As Integer
    Dim i As Integer
    Dim ws As Worksheet
    
    Application.ScreenUpdating = False
    startArk = Worksheets("Stamdata").Range("L1").Value

    For i = startArk To 30
        On Error Resume Next 'Sikrer, at koden fortsætter, hvis arket ikke findes
        Set ws = Worksheets(CStr(i)) 'Skift til arket med nummeret
        On Error GoTo 0

        If Not ws Is Nothing Then
            ws.PageSetup.PrintArea = "A1:R20" 'Det definerede printområde
            ws.PrintOut
        End If
        Set ws = Nothing
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
I made a new code, which hopefully should work.

VBA Code:
Sub Rektangelafrundedehjørner1_Klik()

    Dim startArk As Integer
    Dim i As Integer
    Dim ws As Worksheet
   
    Application.ScreenUpdating = False
    startArk = Worksheets("Stamdata").Range("L1").Value

    For i = startArk To 30
        On Error Resume Next 'Sikrer, at koden fortsætter, hvis arket ikke findes
        Set ws = Worksheets(CStr(i)) 'Skift til arket med nummeret
        On Error GoTo 0

        If Not ws Is Nothing Then
            ws.PageSetup.PrintArea = "A1:R20" 'Det definerede printområde
            ws.PrintOut
        End If
        Set ws = Nothing
    Next i
    Application.ScreenUpdating = True
End Sub
I made a new code, which hopefully should work.

VBA Code:
Sub Rektangelafrundedehjørner1_Klik()

    Dim startArk As Integer
    Dim i As Integer
    Dim ws As Worksheet
   
    Application.ScreenUpdating = False
    startArk = Worksheets("Stamdata").Range("L1").Value

    For i = startArk To 30
        On Error Resume Next 'Sikrer, at koden fortsætter, hvis arket ikke findes
        Set ws = Worksheets(CStr(i)) 'Skift til arket med nummeret
        On Error GoTo 0

        If Not ws Is Nothing Then
            ws.PageSetup.PrintArea = "A1:R20" 'Det definerede printområde
            ws.PrintOut
        End If
        Set ws = Nothing
    Next i
    Application.ScreenUpdating = True
End Sub
Dear ebea It works as it should, many thanks, Best Regards Klaus W
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top