饒舌な茶碗 《oshaberi-cup》
ひねもすのたり のたり…日々の暮らし。 定職につけない遠距離介護中。 能天気なプチ持病持ち日記♪
備忘録です⇒Excel マクロ。
Option Explicit
Option Explicit
Sub シート名一覧の作成()
On Error GoTo Error_Exit
Dim mypath, ext As String
Dim i, j, k, x As Long
Dim targetBook As Workbook
Dim fn(1000)
Dim sn(10000, 10)
'フォルダの選択
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "フォルダを選択"
.AllowMultiSelect = False
If .Show = -1 Then
mypath = .SelectedItems(1) & "\"
Else
Exit Sub
End If
End With
'ファイル名の取得
fn(1) = Dir(mypath, vbDirectory)
i = 1
Do
i = i + 1
fn(i) = Dir
Loop Until fn(i) = ""
'シート名の取得
x = 0
For j = 1 To i - 1
ext = Mid(fn(j), InStrRev(fn(j), ".") + 1, 3)
If ext = "xls" Or ext = "xlsx" Then
Set targetBook = Workbooks.Open(mypath & "\" & fn(j))
For k = 1 To targetBook.Sheets.Count
sn(x, 1) = fn(j)
sn(x, 2) = targetBook.Sheets(k).Name
x = x + 1
Next k
targetBook.Close
End If
Next j
'シート名一覧の作成
Cells.ClearContents
Cells(1, 1) = mypath
Cells(3, 1) = "ファイル名"
Cells(3, 2) = "シート名"
x = 0
Do
Cells(x + 4, 1) = sn(x, 1)
Cells(x + 4, 2) = sn(x, 2)
x = x + 1
Loop Until sn(x, 1) = ""
Exit Sub
Error_Exit:
MsgBox Err.Description
On Error Resume Next
Set targetBook = Nothing
End Sub
仕事で必要にせまられ、他人様のブログを検索してたら参考になりそうなものを発見。
⇒参照
うまく動かず、改良して…もらいましたぁぁぁ。(親切な友人に感謝

ああ、自分でも理解できるようになりたいのぉ。
自分でも、少しわかりそうなことを載せてあったのを見つけたのでこちらも。
⇒シート名をマクロを使わずに取得
・Visual Basic Editorを起動
・Visual Basic Editorを起動
・イミディエイトウインドウに
For Each i In ThisWorkbook.Sheets: debug.print i.name : next iと入力しenter
(イミディエイトウインドウが表示されていない場合はCtrl+G)
PR
ブログ内検索
カレンダー
最新記事
(07/23)
(08/23)
(08/22)
(08/20)
(08/01)
アーカイブ
プロフィール
HN:
のたりパン狸
性別:
女性
趣味:
昼寝