Sub open_d() Dim MyStr As String Dim i, col, num As Integer 'Dim OpenFileName As String Dim SheetName As String, Target As String, buf As String Dim OpenFileName Dim book As String OpenFileName = Application.GetOpenFilename("Microsoft Excelブック,*.xls") 'ファイルオープン If OpenFileName <> "False" Then Workbooks.Open Filename:=OpenFileName buf = Dir(OpenFileName) Workbooks(buf).Activate 'オープンしたファイルに対する処理 Application.ScreenUpdating = False '画面更新停止 book = ActiveWorkbook.Name For col = 4 To 26 '列幅調整 Sheet2.Columns(col).ColumnWidth = 30.25 Next col blnSaveStatus = Application.DisplayStatusBar 'プログレスバー Application.DisplayStatusBar = True strBarNow = "現在 0%:□□□□□□□□□□" Application.StatusBar = strBarNow For i = 4 To Sheet1.Cells(Rows.Count, 2).End(xlUp).Row 'Cells(Rows.Count, 2)はセル(最終行, 2列目) For j = 2 To 24 MyStr = Sheet1.Cells(i, j) If MyStr <> "" Then Sheet2.Cells(i + 2, j + 2) = MyStr End If Next j progress = Int(i * 100 / Sheet1.Cells(Rows.Count, 2).End(xlUp).Row) ' 進捗率(%) mass = Int(i * 10 / Sheet1.Cells(Rows.Count, 2).End(xlUp).Row) ' 10%単位 strBarWk = "現在 " & progress & "%:" & _ String(mass, "■") & String(10 - mass, "□") If (strBarWk <> strBarNow) Then Application.StatusBar = strBarWk 'バー更新 strBarNow = strBarWk End If Application.StatusBar = False ' ステータスバーの制御をExcelに戻す Application.DisplayStatusBar = blnSaveStatus Next i End If Application.ScreenUpdating = True End Sub
(2)Withでブックを明示的に指定する下記の方法
With Workbooks(buf)
.Sheet2.Columns(col).ColumnWidth = 30.25
End With
でも、オブジェクト名でシートを指定するとエラーが発生する(実行時エラー438)
したがってシートを指定する際はインデックスやワークシート名で指定する必要がある。