mvandeynze
New Member
- Joined
- Apr 30, 2018
- Messages
- 9
I am trying to create a code that will form a PDF report based on criteria entered in on a Userform. Based on the inputs, certain tables will need to be selected to copy to a new sheet and then export that new sheet into a PDF. I am having troubles creating the range to be copied.
Here is a part of the code that I am having trouble with.
I have Dim'd all the parts within the union brackets as a range and I have also set all those ranges as
Dim RPT, MBHWP, MBLWP, MBHAG, MBLAG, MBLOY, MBHOY, MBLTY, MBHTY as range
Set RPT = Sheets("CODE").Range("A1") 'this is a blank cell I used to set it because I read on a forum that your union needs to be defined first as something because it can't start as nothing.
With Sheets("Report")
Set MBHWP = .Range("A1129:E1175")
Set MBLWP = .Range("A1176:E1222")
Set MBHAG = .Range("A223:E1269")
Set MBLAG = .Range("A1270:E1316")
Set MBLOY = .Range("A1411:E1457")
Set MBHOY = .Range("A1458:E1504")
Set MBLTY = .Range("A1317:E1363")
Set MBHTY = .Range("A1364:E1410")
end with
Then I have the code for the selection:
If ComboBox2.Value = 40 Or ComboBox2.Value = 45 Then
If CheckBox1.Value = True Then
Set RPT = Union(MBHWP, RPT)
Else
If CheckBox2.Value = True Then
Set RPT = Union(MBLWP, RPT)
Else
If CheckBox3.Value = True Then
Set RPT = Union(MBHAG, RPT)
Else
If CheckBox4.Value = True Then
Set RPT = Union(MBLAG, RPT)
Else
If CheckBox5.Value = True Then
Set RPT = Union(MBLOY, RPT)
If CheckBox6.Value = True Then
Set RPT = Union(MBHOY, RPT)
Else
If CheckBox12.Value = True Then
Set RPT = Union(MBLTY, RPT)
Else
If CheckBox7.Value = True Then
Set RPT = Union(MBHTY, RPT)
Else
End If
End If
End If
End If
End If
End If
End If
End If
Else
I also have those named ranges in my name manager on the excel sheet as the same thing, but with "report" on the end. (example "MBHWPReport") and I have tried
Set RPT = Range(MBHWP, RPT) for all of them, and
Set RPT = Union(Range("MBHWPReport"), RPT) as well. (again with "MBHWPReport" being a named range on my excel sheet)
The idea is that if the user selected 40 for the combobox, it would look to the MB tables of the Report pages and select any of HWP, LWP, HAG, LAG, LTY, HTY, LOY, HOY based on if their checkbox was true in the user form. I do have other IFs for the combobox to correspond to other table sets of the Report page. I need to have any combination of these selects because the user could request any number of these and I need the code to adjust accordingly. However there is only one selection for the combobox (if they select 40, they will only be able to access the MB tables, if they want more they will have to run the code again with the other value in the combobox). The error I get is that the 'union' method or 'range' method is out of range. (depending on which one I used. I have searched multiple forum entries and have not found an answer that works for me.
Once I have all the ranges in RPT, This is the following code:
With ThisWorkbook
.Sheets.Add(After:=.Sheets("Report")).Name = "Name1"
End With
Range(RPT).Copy Destination:=Sheets("Name1").Range("A1")
Dim FName As Variant
FName = Application.GetSaveAsFilename( _
InitialFileName:="Name1.pdf", _
FileFilter:="PDF files, *.pdf", _
Title:="Export to pdf")
If FName <> False Then
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FName _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
End If
I am fairly new to VBA, I am self taught for work but I have caught on quickly with the help of these forums. However, I have not been able to find this exact problem on a forum and similar ones that I have based this code off of have not worked to fix this issue.
hope this all makes sense, thanks for all your help!
Here is a part of the code that I am having trouble with.
I have Dim'd all the parts within the union brackets as a range and I have also set all those ranges as
Dim RPT, MBHWP, MBLWP, MBHAG, MBLAG, MBLOY, MBHOY, MBLTY, MBHTY as range
Set RPT = Sheets("CODE").Range("A1") 'this is a blank cell I used to set it because I read on a forum that your union needs to be defined first as something because it can't start as nothing.
With Sheets("Report")
Set MBHWP = .Range("A1129:E1175")
Set MBLWP = .Range("A1176:E1222")
Set MBHAG = .Range("A223:E1269")
Set MBLAG = .Range("A1270:E1316")
Set MBLOY = .Range("A1411:E1457")
Set MBHOY = .Range("A1458:E1504")
Set MBLTY = .Range("A1317:E1363")
Set MBHTY = .Range("A1364:E1410")
end with
Then I have the code for the selection:
If ComboBox2.Value = 40 Or ComboBox2.Value = 45 Then
If CheckBox1.Value = True Then
Set RPT = Union(MBHWP, RPT)
Else
If CheckBox2.Value = True Then
Set RPT = Union(MBLWP, RPT)
Else
If CheckBox3.Value = True Then
Set RPT = Union(MBHAG, RPT)
Else
If CheckBox4.Value = True Then
Set RPT = Union(MBLAG, RPT)
Else
If CheckBox5.Value = True Then
Set RPT = Union(MBLOY, RPT)
If CheckBox6.Value = True Then
Set RPT = Union(MBHOY, RPT)
Else
If CheckBox12.Value = True Then
Set RPT = Union(MBLTY, RPT)
Else
If CheckBox7.Value = True Then
Set RPT = Union(MBHTY, RPT)
Else
End If
End If
End If
End If
End If
End If
End If
End If
Else
I also have those named ranges in my name manager on the excel sheet as the same thing, but with "report" on the end. (example "MBHWPReport") and I have tried
Set RPT = Range(MBHWP, RPT) for all of them, and
Set RPT = Union(Range("MBHWPReport"), RPT) as well. (again with "MBHWPReport" being a named range on my excel sheet)
The idea is that if the user selected 40 for the combobox, it would look to the MB tables of the Report pages and select any of HWP, LWP, HAG, LAG, LTY, HTY, LOY, HOY based on if their checkbox was true in the user form. I do have other IFs for the combobox to correspond to other table sets of the Report page. I need to have any combination of these selects because the user could request any number of these and I need the code to adjust accordingly. However there is only one selection for the combobox (if they select 40, they will only be able to access the MB tables, if they want more they will have to run the code again with the other value in the combobox). The error I get is that the 'union' method or 'range' method is out of range. (depending on which one I used. I have searched multiple forum entries and have not found an answer that works for me.
Once I have all the ranges in RPT, This is the following code:
With ThisWorkbook
.Sheets.Add(After:=.Sheets("Report")).Name = "Name1"
End With
Range(RPT).Copy Destination:=Sheets("Name1").Range("A1")
Dim FName As Variant
FName = Application.GetSaveAsFilename( _
InitialFileName:="Name1.pdf", _
FileFilter:="PDF files, *.pdf", _
Title:="Export to pdf")
If FName <> False Then
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FName _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
End If
I am fairly new to VBA, I am self taught for work but I have caught on quickly with the help of these forums. However, I have not been able to find this exact problem on a forum and similar ones that I have based this code off of have not worked to fix this issue.
hope this all makes sense, thanks for all your help!