VBA query

palaeontology

Active Member
Joined
May 12, 2017
Messages
444
Office Version
  1. 2016
Platform
  1. Windows
Here is a code I'm using to ...
* choose students from four list tables on a userform
* make copies of a template sheet .. each sheet named one of the names found in the range that houses the names of the students chosen from the userform list boxes
* print page 1 of each of them
* delete the newly made sheets
* then clear the contents of a given range.

Rich (BB code):
Private Sub CommandButton3_Click()Dim Addme As Range
Dim x As Integer
If IsEmpty(Sheets("PrintTemplate").Range("V49")) Then
    Set Addme = Sheets("PrintTemplate").Range("V49")
Else
    Set Addme = Sheets("PrintTemplate").Range("V" & Rows.Count).End(xlUp).Offset(1, 0)
End If
For x = 0 To Me.ListBox_1st_Class.ListCount - 1
If Me.ListBox_1st_Class.Selected(x) Then
        Addme = Me.ListBox_1st_Class.List(x)
        Addme.Offset(, 1).Value = Me.ListBox_1st_Class.List(x, 1)
        Set Addme = Addme.Offset(1, 0)
        End If
    Next x
For x = 0 To Me.ListBox_1st_Class.ListCount - 1
If Me.ListBox_1st_Class.Selected(x) Then Me.ListBox_1st_Class.Selected(x) = False
Next x


'###########
'Code2
' "Y49", Column "Y" &  "Me.ListBox_2nd_Class"


If IsEmpty(Sheets("PrintTemplate").Range("Y49")) Then
    Set Addme = Sheets("PrintTemplate").Range("Y49")
Else
    Set Addme = Sheets("PrintTemplate").Range("Y" & Rows.Count).End(xlUp).Offset(1, 0)
End If
For x = 0 To Me.ListBox_2nd_Class.ListCount - 1
If Me.ListBox_2nd_Class.Selected(x) Then
        Addme = Me.ListBox_2nd_Class.List(x)
        Addme.Offset(, 1).Value = Me.ListBox_2nd_Class.List(x, 1)
        Set Addme = Addme.Offset(1, 0)
        End If
    Next x
For x = 0 To Me.ListBox_2nd_Class.ListCount - 1
If Me.ListBox_2nd_Class.Selected(x) Then Me.ListBox_2nd_Class.Selected(x) = False
Next x




'###########
'Code3
' "AB49", Column "AB" &  "Me.ListBox_3rd_Class"


If IsEmpty(Sheets("PrintTemplate").Range("AB49")) Then
    Set Addme = Sheets("PrintTemplate").Range("AB49")
Else
    Set Addme = Sheets("PrintTemplate").Range("AB" & Rows.Count).End(xlUp).Offset(1, 0)
End If
For x = 0 To Me.ListBox_3rd_Class.ListCount - 1
If Me.ListBox_3rd_Class.Selected(x) Then
        Addme = Me.ListBox_3rd_Class.List(x)
        Addme.Offset(, 1).Value = Me.ListBox_3rd_Class.List(x, 1)
        Set Addme = Addme.Offset(1, 0)
        End If
    Next x
For x = 0 To Me.ListBox_3rd_Class.ListCount - 1
If Me.ListBox_3rd_Class.Selected(x) Then Me.ListBox_3rd_Class.Selected(x) = False
Next x






'###########
'Code4
' "AE49", Column "AE" &  "Me.ListBox_4th_Class"


If IsEmpty(Sheets("PrintTemplate").Range("AE49")) Then
    Set Addme = Sheets("PrintTemplate").Range("AE49")
Else
    Set Addme = Sheets("PrintTemplate").Range("AE" & Rows.Count).End(xlUp).Offset(1, 0)
End If
For x = 0 To Me.ListBox_4th_Class.ListCount - 1
If Me.ListBox_4th_Class.Selected(x) Then
        Addme = Me.ListBox_4th_Class.List(x)
        Addme.Offset(, 1).Value = Me.ListBox_4th_Class.List(x, 1)
        Set Addme = Addme.Offset(1, 0)
        End If
    Next x
For x = 0 To Me.ListBox_4th_Class.ListCount - 1
If Me.ListBox_4th_Class.Selected(x) Then Me.ListBox_4th_Class.Selected(x) = False
Next x
 
 
 'Copy Template Multiple Times and Rename them with names from a List
Dim ws As Worksheet, Ct As Long, c As Range
Set ws = Worksheets("Student Profile Template")
Application.ScreenUpdating = False
For Each c In Sheets("PrintTemplate").Range("AH49:AH100")
    If c.Value <> "" Then
        ws.Copy after:=Sheets(Sheets.Count)
        ActiveSheet.Name = c.Value
        Ct = Ct + 1
    End If
Next c
Application.ScreenUpdating = True






'Print all Sheets named with a 5-digit number
For Each ws In ThisWorkbook.Worksheets
If ws.Name Like "#####" Then
ws.PrintOut from:=1, To:=1
End If
Next ws




'Delete all Sheets named with a 5-digit number
  Application.DisplayAlerts = False
  For Each ws In Worksheets
    If ws.Name Like "#####" Then ws.Delete
  Next ws




'Clear student names from chosen list
Dim tbl As Range
Set tbl = Sheets("PrintTemplate").Range("V49:AF400")
tbl.ClearContents


End Sub

However, I'm getting an error message on the following line ...
Rich (BB code):
ws.Copy after:=Sheets(Sheets.Count)

which is found in the following part of the code ...

Rich (BB code):
 'Copy Template Multiple Times and Rename them with names from a ListDim ws As Worksheet, Ct As Long, c As Range
Set ws = Worksheets("Student Profile Template")
Application.ScreenUpdating = False
For Each c In Sheets("PrintTemplate").Range("AH49:AH100")
    If c.Value <> "" Then
        ws.Copy after:=Sheets(Sheets.Count)
        ActiveSheet.Name = c.Value
        Ct = Ct + 1
    End If
Next c
Application.ScreenUpdating = True


I've pieced this entire code together from numerous codes given to me, and as my coding is extremely limited, I'm not sure what I've done incorrectly.

Can anyone help point me in the right direction ?

Kind regards

John
 
Last edited:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hia
What error message does it give?
 
Upvote 0

Forum statistics

Threads
1,223,693
Messages
6,173,872
Members
452,536
Latest member
Chiz511

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