Sub cell_print_area_set() '文字入力のある最後のセルを把握し、シートあたりの設定頁数を把握する。 ' Dim ad_str As String Dim k As Long Dim i As Integer '文字の入っているセルを力技で得る ad_str = Range("a1").SpecialCells(xlCellTypeConstants, xlTextValues).Address For i = Len(ad_str) To 1 Step -1 If Mid(ad_str, i, 1) = "$" Then Exit For End If Next k = Right(ad_str, Len(ad_str) - i)
私は2000を使用していますが
Sub Macro1()
k = ActiveSheet.UsedRange.Find(What:="*", After:=Range("A1"), LookIn:=xlTextValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
MsgBox k
End Sub
で実行可能ですよ
'オブジェクトのある最期のセルアドレスを得る
Dim mySh As ShapeRange
Dim adr_bottom As String
ActiveSheet.Shapes.SelectAll '全てを選択
Set mySh = Selection.ShapeRange
With mySh
.Group 'グループ化
.Select 'セレクトしておかないとアングループでエラー
adr_bottom = Selection.BottomRightCell.Address
.Ungroup 'グループ化解除
End With
こんなので、写真の挿入位置の最終セルも確定させ。緻密な計算(爆)で、いくつもあるシートごとの、印刷頁範囲を確定させて、PDFに一括返還しちゃう・・・というのが今回のVBAの最終目的です。
Sub Macro2()
k = ActiveSheet.UsedRange.Find(What:="*", After:=Range("A1"), LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
MsgBox k
End Sub
こんな感じではどうでしょう?
Sub cell_print_area_set()
With Cells.SpecialCells(xlCellTypeConstants, xlTextValues)
k = .Areas(.Areas.Count).Row + .Areas(.Areas.Count).Rows.Count - 1
End With
End Sub
最終的には
sub hoge_hoge()
'文字あるいは写真の挿入されている最後のセルを把握し、シートあたりの設定頁数を把握する。
'
Dim ad_str As String
Dim k As Long '文字最終セル
Dim k_p As Long '写真最終セル
Dim i As Integer
Dim mySh As ShapeRange
Dim adr_bottom As String
k = ActiveSheet.UsedRange.Find(What:="*", After:=Range("A1"), _
LookIn:=xlValues, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
'写真等の最終位置を得る
ActiveSheet.Shapes.SelectAll '全てを選択
Set mySh = Selection.ShapeRange
With mySh
.Group 'グループ化
.Select 'セレクトしておかないとアングループでエラー
adr_bottom = Selection.BottomRightCell.Address
.Ungroup 'グループ化解除
End With
For i = Len(adr_bottom) To 1 Step -1
If Mid(adr_bottom, i, 1) = "$" Then
Exit For
End If
Next
k_p = Int(Val(Right(adr_bottom, Len(adr_bottom) - i)))
'大きいほうを把握
If k > k_p Then
k = k
Else
k = k_p
End If
ad_str = "$A$1:$AC$" & Trim(Str(Int((((k - 1) \ 40) + 1) * 40)))
ActiveSheet.PageSetup.PrintArea = ad_str
end sub
このルーチンを
Sub select_page_area()
'複数セレクトされたシートの最大印刷頁範囲を設定する
Dim kw As Worksheet
Dim msg As String
Dim tmp
For Each kw In ActiveWindow.SelectedSheets
Sheets(kw.Name).Select
msg = "40行設定を行います" & vbCrLf & "選択中の対象シート名[ " & kw.Name & " ]"
tmp = MsgBox(prompt:=msg, Title:="印刷範囲の自動設定", Buttons:=vbInformation + vbYesNoCancel + vbDefaultButton3)
If tmp = vbYes Then
Call acell_print_area_set
ElseIf tmp = vbNo Then
Exit Sub
End If
Next
End Sub