' [151.xls] ' [Module1] のコード '★★☆ データ範囲の最大値を取得する ☆★★ Option Explicit Sub start() UserForm1.Show End Sub ' [UserForm1] のコード Option Explicit Private Sub CommandButton1_Click() Dim r As Integer Dim c As Integer '乱数の生成を利用してランダムな空白を含むデータベースを作成します 'データになる文字や数字も乱数の生成とAsc 関数を利用しています For c = 5 To 11 For r = 8 To 13 Cells(r, c) = Int(200 * Rnd + 2) Next r Next c 'データ範囲が出来上がった時点で、最大値表示ボタンを使用可能にする CommandButton2.Enabled = True End Sub Private Sub CommandButton2_Click() Dim i As Integer Dim myrang As Range Dim all As Range 'データ範囲の取得 Set all = [E8].CurrentRegion 'データ範囲の最大値取得 i = Application.Max(all) 'セル範囲から与えられた条件でそのセルを特定 Set myrang = all.Find(i) '取り出したセル情報の表示 If Not myrang Is Nothing Then _ MsgBox "最大値は " & i & " そのアドレスは " & myrang.Address, , _ "指定セル範囲の最大値のセル情報" End Sub Private Sub UserForm_Initialize() '最初はデータ範囲にデータが無いので最大値表示ボタンを使用不可にします CommandButton2.Enabled = False Me.Caption = "データ範囲の最大値を取得" Me.StartUpPosition = 2 CommandButton1.Caption = "サンプルデータの作成" CommandButton2.Caption = "実 行" End Sub Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) 'フォーム終了時使用したサンプルデータを削除 [E8:K13].ClearContents End Sub