Hi, I would like to VBA create a print range as such: From cell A3 down to a row number specified in cell A1, and then to the rightmost column that has data in it. However there will be hidden columns between column A and the last column. Thanks
What do you get if you'd tried
Code:MsgBox Cells(1, 27).Left
Sub Maybe()
Dim lr As Long, lc As Long, hite As Double, wits As Double
Dim i As Long, ii As Long
lr = Cells.Find("*", , , , xlByRows, xlPrevious).Row
lc = Cells.Find("*", , , , xlByColumns, xlPrevious).Column
wits = 0
hite = 0
For i = 1 To lc
If Columns(i).Hidden = False Then wits = wits + Columns(i).Width
Next i
For ii = 1 To lr
If Rows(ii).Hidden = False Then hite = hite + Rows(ii).Height
Next ii
MsgBox "Total Height = " & hite & ", Total Width = " & wits
wits = 0
hite = 0
End Sub
Sub DETAIL_PDF2_REPLACE_FILE_AUTO_RANGE_LAND()
Dim response As String
Dim PrintAreaString As String
Dim fpath As String
Dim fName As String
Dim fileSaveName As String, filePath As String
Dim reply As Variant
Dim lc As Long, GT As Long
Dim shArr, i As Long
shArr = Array("DETAIL FORM2") '<---- Sheets that the macro should work on. Change to your requirements
For i = LBound(shArr) To UBound(shArr)
With Sheets(shArr(i))
lc = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
GT = .Cells(1, 2).Value
.PageSetup.PrintArea = Range("A4:A" & GT).Resize(, lc).Address
.PageSetup.Orientation = xlLandscape
.PageSetup.Zoom = False
.PageSetup.FitToPagesWide = 1
.PageSetup.FitToPagesTall = False
.PageSetup.LeftMargin = 36
.PageSetup.TopMargin = 36
.PageSetup.RightMargin = 36
.PageSetup.BottomMargin = 36
End With
Next i
fileSaveName = "FRIEDLAND QUOTE " & [B6] & " " & "JOB " & [B7] & " " & [A29] & " " & [AB29] & " " & [AC29] & [B9] & " PCS" & " " & Format(Date, "mmddyy")
filePath = ActiveWorkbook.Path & "\" & fileSaveName & ".pdf"
reply = vbNo
While Dir(filePath) <> vbNullString And fileSaveName <> "" And reply = vbNo
reply = MsgBox("THE PDF " & fileSaveName & " ALREADY EXISTS." & vbCrLf & vbCrLf & "DO YOU WANT TO REPLACE THE FILE? CHOOSE NO TO RENAME.", vbYesNo, "Save as PDF")
If reply = vbNo Then
fileSaveName = InputBox("Please enter a new file name:", "Save as PDF", fileSaveName)
End If
filePath = ActiveWorkbook.Path & "\" & fileSaveName & ".pdf"
Wend
If fileSaveName <> "" Then
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=filePath, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True '(change this to "False" to prevent the file from opening after saving)
Else
MsgBox "PDF not created"
End If
End Sub
Dim witsMsg As String
.PageSetup.Orientation = xlLandscape
If wits > 900 Then
.PageSetup.Orientation = xlLandscape
Else
witsMsg = MsgBox("Width is " & wits &". Do you want to print in portrait mode?", vbyesno,"Printing Options.")
If witsMsg = vbyes Then
.PageSetup.Orientation = xlPortrait
Else
.PageSetup.Orientation = xlLandscape
End If
End If
Sub DETAIL_FORM2_PDF_REPLACE_FILE_AUTO_RANGE_BY_WIDTH_TESTING()
Dim response As String
Dim PrintAreaString As String
Dim fpath As String
Dim fName As String
Dim fileSaveName As String, filePath As String
Dim reply As Variant
Dim lc As Long, GT As Long
Dim shArr, i As Long
Dim witsMsg As String
shArr = Array("DETAIL FORM2") '<---- Sheets that the macro should work on. Change to your requirements
For i = LBound(shArr) To UBound(shArr)
With Sheets(shArr(i))
lc = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
GT = .Cells(1, 2).Value
.PageSetup.PrintArea = Range("A4:A" & GT).Resize(, lc).Address
If wits > 900 Then
.PageSetup.Orientation = xlLandscape
Else
witsMsg = MsgBox("Width is " & wits & ". Do you want to print in portrait mode?", vbYesNo, "Printing Options.")
If witsMsg = vbYes Then
.PageSetup.Orientation = xlPortrait
Else
.PageSetup.Orientation = xlLandscape
End If
End If
.PageSetup.Zoom = False
.PageSetup.FitToPagesWide = 1
.PageSetup.FitToPagesTall = False
.PageSetup.LeftMargin = 36
.PageSetup.TopMargin = 36
.PageSetup.RightMargin = 36
.PageSetup.BottomMargin = 36
End With
Next i
fileSaveName = "FRIEDLAND QUOTE " & [B7] & " " & "JOB " & [B8] & " " & [A30] & " " & [AB30] & " " & [AC30] & [B10] & " PCS" & " " & Format(Date, "mmddyy")
filePath = ActiveWorkbook.Path & "\" & fileSaveName & ".pdf"
reply = vbNo
While Dir(filePath) <> vbNullString And fileSaveName <> "" And reply = vbNo
reply = MsgBox("THE PDF " & fileSaveName & " ALREADY EXISTS." & vbCrLf & vbCrLf & "DO YOU WANT TO REPLACE THE FILE? CHOOSE NO TO RENAME.", vbYesNo, "Save as PDF")
If reply = vbNo Then
fileSaveName = InputBox("Please enter a new file name:", "Save as PDF", fileSaveName)
End If
filePath = ActiveWorkbook.Path & "\" & fileSaveName & ".pdf"
Wend
If fileSaveName <> "" Then
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=filePath, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True '(change this to "False" to prevent the file from opening after saving)
Else
MsgBox "PDF not created"
End If
End Sub
lc = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column