VBA to print

KlausW

Active Member
Joined
Sep 9, 2020
Messages
378
Office Version
  1. 2016
Platform
  1. Windows
Hello

I have a challenge.

I use this VBA code to print different pages and I put X next to the sheet name that I like to be printed in the sheets Stamdata (yellow) see picture 1. It works as it shall. Now I would like to insert 2 new sheets (red) before the sheets Stamdata (yellow) see image 2. But then the VBA code does not work, it does not follow the X.

Any help will be appreciated.

Best Regards

Klaus W
VBA Code:
Sub Rektangelafrundedehjørner4_Klik()

Dim intRaekke As Integer

Dim SidsteSide As Integer

Dim SiderUdskrevet As Integer

SiderUdskrevet = 0

On Error GoTo fejl:

If Worksheets.Count < 1 Then

MsgBox "Der er ingen sider at udskrive?"

End If

For intRaekke = 5 To Worksheets.Count '<--Rettet til 5. Print første

'Tilføjet - 2 for at læse X fra række 3 i Stamdata

If Cells(intRaekke - 2, 2).Value = "x" Or _

Cells(intRaekke - 2, 2).Value = "X" Then

SidsteSide = Sheets(intRaekke).Range("G1")

If Not (IsNumeric(SidsteSide)) Or SidsteSide = 0 Then

MsgBox Worksheets(intRaekke).Name & " Celle G1 Er ikke et tal eller > 0?"

Exit Sub

End If

Sheets(intRaekke).PrintOut From:=1, To:=SidsteSide

SiderUdskrevet = SiderUdskrevet + 1

End If

Next

If SiderUdskrevet = 0 Then

MsgBox "Ingen sider valgt til udskrift."

End If

fejl:

If Err.Number = 1004 Then

MsgBox "Du skal indtaste et tal på Fane" & intRaekke & " celle G1"

End If

End Sub
 

Attachments

  • Picture 1.png
    Picture 1.png
    45.1 KB · Views: 17
  • Picture 2.png
    Picture 2.png
    50.5 KB · Views: 18

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi KlausW,

both your pictures show the same scenario.

According to what I understand the order of the worksheets in the workbook after inserting 2 ws at the beginning: 2 ws to be printed, 2 sheets not to be printed, 6 ws more to be printed.

MrE_1229672_1702B11_vba to print_230212.xlsm
ABC
2Sheets NameX for printIndex
315
426
53x7
648
759
8610
971
108x2
Stamdata


VBA Code:
Sub Rektangelafrundedehjørner4_Klik()
' https://www.mrexcel.com/board/threads/vba-to-print.1229672/
Dim intRaekke As Integer
Dim SidsteSide As Integer
Dim SiderUdskrevet As Integer
Dim lngWsIndex As Long         'will be used for Index of worksheets

SiderUdskrevet = 0

On Error GoTo fejl:
If Worksheets.Count < 1 Then
  MsgBox "Der er ingen sider at udskrive?"
End If

intRaekke = 5
With Worksheets("Stamdata")
  Do While .Cells(intRaekke - 2, 1).Value <> ""
    'Tilføjet - 2 for at læse X fra række 3 i Stamdata
    If LCase(.Cells(intRaekke - 2, 2).Value) = "x" Then
      Select Case intRaekke
        'rows in Stamdata referencing ws with Index 1 and 2: subtract 10 from intRaekke as you start with Index 5 at Row 3
        Case 11 To 12
          lngWsIndex = intRaekke - 10
        Case Else
          lngWsIndex = intRaekke
      End Select
      SidsteSide = Worksheets(lngWsIndex).Range("G1")
      If Not (IsNumeric(SidsteSide)) Or SidsteSide = 0 Then
        MsgBox Worksheets(lngWsIndex).Name & " Celle G1 Er ikke et tal eller > 0?"
        Exit Sub
      End If
      Worksheets(lngWsIndex).PrintOut From:=1, To:=SidsteSide
      SiderUdskrevet = SiderUdskrevet + 1
    End If
    intRaekke = intRaekke + 1
  Loop
End With

If SiderUdskrevet = 0 Then
  MsgBox "Ingen sider valgt til udskrift."
End If

fejl:
If Err.Number = 1004 Then
  MsgBox "Du skal indtaste et tal på Fane" & intRaekke & " celle G1"
End If

End Sub

Holger
 
Upvote 0
Hi KlausW,

both your pictures show the same scenario.

According to what I understand the order of the worksheets in the workbook after inserting 2 ws at the beginning: 2 ws to be printed, 2 sheets not to be printed, 6 ws more to be printed.

MrE_1229672_1702B11_vba to print_230212.xlsm
ABC
2Sheets NameX for printIndex
315
426
53x7
648
759
8610
971
108x2
Stamdata


VBA Code:
Sub Rektangelafrundedehjørner4_Klik()
' https://www.mrexcel.com/board/threads/vba-to-print.1229672/
Dim intRaekke As Integer
Dim SidsteSide As Integer
Dim SiderUdskrevet As Integer
Dim lngWsIndex As Long         'will be used for Index of worksheets

SiderUdskrevet = 0

On Error GoTo fejl:
If Worksheets.Count < 1 Then
  MsgBox "Der er ingen sider at udskrive?"
End If

intRaekke = 5
With Worksheets("Stamdata")
  Do While .Cells(intRaekke - 2, 1).Value <> ""
    'Tilføjet - 2 for at læse X fra række 3 i Stamdata
    If LCase(.Cells(intRaekke - 2, 2).Value) = "x" Then
      Select Case intRaekke
        'rows in Stamdata referencing ws with Index 1 and 2: subtract 10 from intRaekke as you start with Index 5 at Row 3
        Case 11 To 12
          lngWsIndex = intRaekke - 10
        Case Else
          lngWsIndex = intRaekke
      End Select
      SidsteSide = Worksheets(lngWsIndex).Range("G1")
      If Not (IsNumeric(SidsteSide)) Or SidsteSide = 0 Then
        MsgBox Worksheets(lngWsIndex).Name & " Celle G1 Er ikke et tal eller > 0?"
        Exit Sub
      End If
      Worksheets(lngWsIndex).PrintOut From:=1, To:=SidsteSide
      SiderUdskrevet = SiderUdskrevet + 1
    End If
    intRaekke = intRaekke + 1
  Loop
End With

If SiderUdskrevet = 0 Then
  MsgBox "Ingen sider valgt til udskrift."
End If

fejl:
If Err.Number = 1004 Then
  MsgBox "Du skal indtaste et tal på Fane" & intRaekke & " celle G1"
End If

End Sub

Holger
Hello Holger, if you look at picture 2, I have colored the 2 inserted sheets red, and after that the VBA code does not work. And the VBA code you sent my do not work. Regards Klaus W
 
Upvote 0
I think the solution must be found in the line. If Worksheets.Count < 1 Then or For intRaekke = 5 To Worksheets.Count.

Regards Klaus W
 
Upvote 0
Hi Klaus,

you're right about the pictures - I did not catch that on my laptop.

Working with the Index (the position of the worksheets in the workbook) may be daring as users may drag around the sheets.

2023-02-12 20 00 16.png


I hope this comes near to what your setup may look like with the registers with no colour to be treated for printout.

MrE_1229672_1702B11_vba to print_230212.xlsm
ABCD
2Sheets NameX for printRow CounterIndex
3157
4268
5379
64810
75911
861012
971113
1081214
119133
1210144
Stamdata


The numbers in Columns C and D are added just to explain how the Index numbers of the sheets are determined.

The code must be altered due to the new sheets adding to the collection of worksheets:

VBA Code:
Sub Rektangelafrundedehjørner4_Klik()
' https://www.mrexcel.com/board/threads/vba-to-print.1229672/
Dim intRaekke As Integer
Dim SidsteSide As Integer
Dim SiderUdskrevet As Integer
Dim lngWsIndex As Long         'will be used for Index of worksheets

SiderUdskrevet = 0

On Error GoTo fejl:
If Worksheets.Count < 1 Then
  MsgBox "Der er ingen sider at udskrive?"
End If

intRaekke = 5
With Worksheets("Stamdata")
  Do While .Cells(intRaekke - 2, 1).Value <> ""
    'Tilføjet - 2 for at læse X fra række 3 i Stamdata
    If LCase(.Cells(intRaekke - 2, 2).Value) = "x" Then
      Select Case intRaekke
        'rows in Stamdata referencing ws with Index 1 and 2: subtract 10 from intRaekke as you start with Index 5 at Row 3
        Case 13 To 14
          lngWsIndex = intRaekke - 10
        Case Else
          lngWsIndex = intRaekke + 2
      End Select
      SidsteSide = Worksheets(lngWsIndex).Range("G1")
      If Not (IsNumeric(SidsteSide)) Or SidsteSide = 0 Then
        MsgBox Worksheets(lngWsIndex).Name & " Celle G1 Er ikke et tal eller > 0?"
        Exit Sub
      End If
      Worksheets(lngWsIndex).PrintOut From:=1, To:=SidsteSide
      SiderUdskrevet = SiderUdskrevet + 1
    End If
    intRaekke = intRaekke + 1
  Loop
End With

If SiderUdskrevet = 0 Then
  MsgBox "Ingen sider valgt til udskrift."
End If

fejl:
If Err.Number = 1004 Then
  MsgBox "Du skal indtaste et tal på Fane" & intRaekke & " celle G1"
End If

End Sub

Holger
 
Upvote 0
Solution
Hi Klaus,

you're right about the pictures - I did not catch that on my laptop.

Working with the Index (the position of the worksheets in the workbook) may be daring as users may drag around the sheets.

View attachment 85235

I hope this comes near to what your setup may look like with the registers with no colour to be treated for printout.

MrE_1229672_1702B11_vba to print_230212.xlsm
ABCD
2Sheets NameX for printRow CounterIndex
3157
4268
5379
64810
75911
861012
971113
1081214
119133
1210144
Stamdata


The numbers in Columns C and D are added just to explain how the Index numbers of the sheets are determined.

The code must be altered due to the new sheets adding to the collection of worksheets:

VBA Code:
Sub Rektangelafrundedehjørner4_Klik()
' https://www.mrexcel.com/board/threads/vba-to-print.1229672/
Dim intRaekke As Integer
Dim SidsteSide As Integer
Dim SiderUdskrevet As Integer
Dim lngWsIndex As Long         'will be used for Index of worksheets

SiderUdskrevet = 0

On Error GoTo fejl:
If Worksheets.Count < 1 Then
  MsgBox "Der er ingen sider at udskrive?"
End If

intRaekke = 5
With Worksheets("Stamdata")
  Do While .Cells(intRaekke - 2, 1).Value <> ""
    'Tilføjet - 2 for at læse X fra række 3 i Stamdata
    If LCase(.Cells(intRaekke - 2, 2).Value) = "x" Then
      Select Case intRaekke
        'rows in Stamdata referencing ws with Index 1 and 2: subtract 10 from intRaekke as you start with Index 5 at Row 3
        Case 13 To 14
          lngWsIndex = intRaekke - 10
        Case Else
          lngWsIndex = intRaekke + 2
      End Select
      SidsteSide = Worksheets(lngWsIndex).Range("G1")
      If Not (IsNumeric(SidsteSide)) Or SidsteSide = 0 Then
        MsgBox Worksheets(lngWsIndex).Name & " Celle G1 Er ikke et tal eller > 0?"
        Exit Sub
      End If
      Worksheets(lngWsIndex).PrintOut From:=1, To:=SidsteSide
      SiderUdskrevet = SiderUdskrevet + 1
    End If
    intRaekke = intRaekke + 1
  Loop
End With

If SiderUdskrevet = 0 Then
  MsgBox "Ingen sider valgt til udskrift."
End If

fejl:
If Err.Number = 1004 Then
  MsgBox "Du skal indtaste et tal på Fane" & intRaekke & " celle G1"
End If

End Sub

Holger
Hi HaHoBe thanks it is just as I want. Thanks a lot. Have a nice evening. Best regards from Denmark Klaus W
 
Upvote 0
Hi KlausW,

maybe you can test this slightly different approach as well as that will work with the actual names for the worksheets.

In ThisWorkbook you need to use (or amend to)
VBA Code:
Private Sub Workbook_Open()
  FillStamdata
End Sub

So any time the workbook is opened the code for procedure FillStamdata will run.

Behind sheet Stamdata you should use (or amend)

VBA Code:
Private Sub Worksheet_Activate()
  FillStamdata
End Sub

This will run the procedure any time you switch to sheet Stamdata.

The procedure should be placed in a standard module, please change the name for the last sheet as I named it AddLastName:

VBA Code:
Sub FillStamdata()
Dim ws As Worksheet

Application.ScreenUpdating = False
With Worksheets("Stamdata")
  'Clear the entries form Row 3 on in Columns A and B
  .Range("A3:B" & .Cells(.Rows.Count, "A").End(xlUp).Row).ClearContents
 
  For Each ws In ThisWorkbook.Worksheets
    Select Case ws.Name
      Case "Materiale", "Master", "AddLastName", .Name
        'we don*t want these names as they are not to be printed
      Case Else
        .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Value = ws.Name
    End Select
  Next ws
End With
Application.ScreenUpdating = True
End Sub

And the main procedure needs to be updated as well to

VBA Code:
Sub Rektangelafrundedehjørner4_Klik()
' https://www.mrexcel.com/board/threads/vba-to-print.1229672/
'Update: uses the names of the worksheets instead the Index in the workbook
Dim intRaekke As Integer
Dim SidsteSide As Integer
Dim SiderUdskrevet As Integer
Dim wsPrint As Worksheet

SiderUdskrevet = 0

On Error GoTo fejl:
If Worksheets.Count < 1 Then
  MsgBox "Der er ingen sider at udskrive?"
End If

With Worksheets("Stamdata")
  For intRaekke = 3 To .Cells(.Rows.Count, "A").End(xlUp).Row
    'Tilføjet - 2 for at læse X fra række 3 i Stamdata
    If LCase(.Cells(intRaekke, 2).Value) = "x" Then
      Set wsPrint = Worksheets(.Cells(intRaekke, 1))
      SidsteSide = wsPrint.Range("G1")
      If Not (IsNumeric(SidsteSide)) Or SidsteSide = 0 Then
        MsgBox wsPrint.Name & " Celle G1 Er ikke et tal eller > 0?"
        GoTo fejl
      End If
      wsPrint.PrintOut From:=1, to:=SidsteSide
      SiderUdskrevet = SiderUdskrevet + 1
    End If
  Next intRaekke
End With

If SiderUdskrevet = 0 Then
  MsgBox "Ingen sider valgt til udskrift."
End If

fejl:
If Err.Number = 1004 Then
  MsgBox "Du skal indtaste et tal på Fane" & intRaekke & " celle G1"
End If
Set wsPrint = Nothing
End Sub

Have fun ;)

Holger
 
Upvote 0
Hi KlausW,

maybe you can test this slightly different approach as well as that will work with the actual names for the worksheets.

In ThisWorkbook you need to use (or amend to)
VBA Code:
Private Sub Workbook_Open()
  FillStamdata
End Sub

So any time the workbook is opened the code for procedure FillStamdata will run.

Behind sheet Stamdata you should use (or amend)

VBA Code:
Private Sub Worksheet_Activate()
  FillStamdata
End Sub

This will run the procedure any time you switch to sheet Stamdata.

The procedure should be placed in a standard module, please change the name for the last sheet as I named it AddLastName:

VBA Code:
Sub FillStamdata()
Dim ws As Worksheet

Application.ScreenUpdating = False
With Worksheets("Stamdata")
  'Clear the entries form Row 3 on in Columns A and B
  .Range("A3:B" & .Cells(.Rows.Count, "A").End(xlUp).Row).ClearContents
 
  For Each ws In ThisWorkbook.Worksheets
    Select Case ws.Name
      Case "Materiale", "Master", "AddLastName", .Name
        'we don*t want these names as they are not to be printed
      Case Else
        .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Value = ws.Name
    End Select
  Next ws
End With
Application.ScreenUpdating = True
End Sub

And the main procedure needs to be updated as well to

VBA Code:
Sub Rektangelafrundedehjørner4_Klik()
' https://www.mrexcel.com/board/threads/vba-to-print.1229672/
'Update: uses the names of the worksheets instead the Index in the workbook
Dim intRaekke As Integer
Dim SidsteSide As Integer
Dim SiderUdskrevet As Integer
Dim wsPrint As Worksheet

SiderUdskrevet = 0

On Error GoTo fejl:
If Worksheets.Count < 1 Then
  MsgBox "Der er ingen sider at udskrive?"
End If

With Worksheets("Stamdata")
  For intRaekke = 3 To .Cells(.Rows.Count, "A").End(xlUp).Row
    'Tilføjet - 2 for at læse X fra række 3 i Stamdata
    If LCase(.Cells(intRaekke, 2).Value) = "x" Then
      Set wsPrint = Worksheets(.Cells(intRaekke, 1))
      SidsteSide = wsPrint.Range("G1")
      If Not (IsNumeric(SidsteSide)) Or SidsteSide = 0 Then
        MsgBox wsPrint.Name & " Celle G1 Er ikke et tal eller > 0?"
        GoTo fejl
      End If
      wsPrint.PrintOut From:=1, to:=SidsteSide
      SiderUdskrevet = SiderUdskrevet + 1
    End If
  Next intRaekke
End With

If SiderUdskrevet = 0 Then
  MsgBox "Ingen sider valgt til udskrift."
End If

fejl:
If Err.Number = 1004 Then
  MsgBox "Du skal indtaste et tal på Fane" & intRaekke & " celle G1"
End If
Set wsPrint = Nothing
End Sub

Have fun ;)

Holger
Thanks KW
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,352
Members
449,080
Latest member
Armadillos

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