问题
I have a combobox with list of page size in excel. i want to change page sizes with combobox selection change.
Following is not working
Public Sub UpdateSize()
Dim Papersizetext As String
Papersizetext = "xlPaper" & Worksheets("Static").Range("B7").value 'A4 is the value in cell B7
shgenerate.PageSetup.PaperSize = Papersizetext 'not working
shgenerate.PageSetup.PaperSize = "xlPaper" & Combobox1.value 'this also not working
shgenerate.PageSetup.PaperSize = xlPaperA4 'is working - i want above to work.
'shgenerate is sheet name
End sub
回答1:
You could whip up your own function to parse the input and return the correct enum member:
Private Function PaperSize(ByVal rawSize As String) As Long
Select Case rawSize
Case "A4"
PaperSize = xlPaperA4
Case "A3"
PaperSize = xlPaperA3
Case "A5"
PaperSize = xlPaperA5
Case Else
PaperSize = xlPaperUser ' or some other default
End Select
End Function
For more complex paper sizes, such as "Letter 8.5"x11" 22x28cm" or "Legal 8.5"x14" 22x36cm", you could possibly use InStr to test of the existence of "Letter" and "Legal" to return xlPaperLetter and xlPaperLegal.
Regex might be another approach to parse out the dimensions.
Without more detail though, it's hard to give a satisfactory answer, as this question is actually quite a broad topic.
来源:https://stackoverflow.com/questions/61197210/what-is-wrong-with-this-line-of-vba-code-for-excel-print