Converting a string to an object /rang
I am attempting to create a macro to dynamically set a range based on user input.
The spreadsheet is based on 13 yearly periods.
I have sucessfully set the ranges and manually entered the selected ranges using the set Multirange = Union(P1,P2,P3....)
This works
However when I created a for next loop to build the union string based on starting and ending periods , I get a type mismatch
I know I need an object/range type because that is what is created when I enter it manually. The result of the for next loop is
"Union(P1,P2,P3,.....)"
The quotes are probably what is genereating the error.
Is there an easy way to convert the string to a range ?
I have tried just assigning the string to a range/object variable.
That does not work.
Here is the complete macro code. User input part is not done yet.
Multirange is the variable that needs to get the result of bunion and be set to a range.
Sub PrintRange()
Dim P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13 As Range
Dim Multirange
Dim Pstart, Pend
Dim Multi, Period
Dim bunion
With ActiveSheet.PageSetup
.PrintTitleRows = "$7:$10"
.PrintTitleColumns = "$C:$C"
End With
ActiveSheet.PageSetup.PrintArea = "$AU$141:$CF$157"
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.25)
.RightMargin = Application.InchesToPoints(0.25)
.TopMargin = Application.InchesToPoints(0.5)
.BottomMargin = Application.InchesToPoints(0.25)
.HeaderMargin = Application.InchesToPoints(0.5)
.FooterMargin = Application.InchesToPoints(0.5)
.PrintHeadings = True
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = True
.Zoom = 74
.PrintErrors = xlPrintErrorsBlank
End With
Pstart = 1
Pend = 13
bunion = "Union" & "("
'build Union string
For x = Pstart To Pend
Period = "P" & x
If x = Pend Then
Multi = Period
Else
Multi = Period & "," & " "
End If
bunion = bunion + Multi
If x = Pend Then
bunion = bunion & ")"
End If
Next
MsgBox bunion
'COMBINE SELECTED RANGES INTO ONE. MUST BE CONTIGUOUS
Set P1 = Sheets("2005").Range("E12:J157") 'SELECT A PERIOD RANGE INCLUDE HIDDEN COLUMNS
Set P2 = Sheets("2005").Range("K12:P157")
Set P3 = Sheets("2005").Range("Q12:V157")
Set P4 = Sheets("2005").Range("W12:AB157")
Set P5 = Sheets("2005").Range("AC12:AH157")
Set P6 = Sheets("2005").Range("AI12:AN157")
Set P7 = Sheets("2005").Range("AO12:AT157")
Set P8 = Sheets("2005").Range("AU12:AZ157")
Set P9 = Sheets("2005").Range("BA12:BF157")
Set P10 = Sheets("2005").Range("BG12:BL157")
Set P11 = Sheets("2005").Range("BM12:BR157")
Set P12 = Sheets("2005").Range("BS12:BX157")
Set P13 = Sheets("2005").Range("BY12:BZ157")
Set Multirange = bunion
Multirange.Select
Selection.PrintOut Copies:=1, Preview:=True, Collate:=True
End Sub
Pablo
|