2016-05-01 13 views
1

私は、特定のシートを見つけるために一連のワークブックとそのシートをループする方法を理解しようとするVBA初心者ですが、オブジェクト変数。一連のワークブックをループするためにそれぞれを使用する

以下は、私が「書いた」コードです(一緒に貼り付けると、より適切な説明になるかもしれません)。私は様々な修正を試みましたが、問題をある場所から別の場所に移しているようです。どんな助けもありがとう!

Option Explicit 

Sub NestedForEach() 
'Create a Worksheet variable to represent one worksheet 
Dim WS As Worksheet 
Dim WB As Workbook 

'create a boolen variable to hold the status of whether we found worksheet "D" 
Dim IsFound As Boolean 
'initialise the IsFound boolean variable 
IsFound = False 

    For Each WB In Application.Workbooks 
     For Each WS In WB.Worksheets 
      If WS.Name = "d" Then 
       IsFound = True 
       MsgBox "sheet D has been found in " & WB.Name 
       Exit Sub 
      End If 
     Next WS 
    Next WB 

    MsgBox "we could not locate sheet D in any of the open workbooks" & _ 
     Chr(10) & "which are open in this instance of Excel" & _ 
     Chr(10) & "(in case multiple Excels are running)" 

End Sub 

は、あなたが変更に関するご質問がある場合は、私に教えてください:

Sub NestedForEach() 
'Create an object variable to represent each worksheet 
Dim WS As Worksheet 
Dim WB As Workbook 
Set WB = ActiveWorkbook 
Set WS = Workbook.Sheets 
'create a boolen variable to hold the status of whether we found worksheet "D" 
Dim IsFound As Boolean 
'initialise the IsFound boolean variable 
IsFound = False 

    For Each WB In Application.Workbooks 
     For Each WS In WB.Worksheets 
      If WS.Name = "d" Then 
       IsFound = True 
       Exit For 
      End If 
     Next WS 
    Next WB 

    If IsFound Then 
     MsgBox "sheet D has been found in " & ActiveWorkbook.Name 
    Else 
     MsgBox "we could not locate sheet D in any of the open workbooks" 
    End If 


End Sub 

答えて

1

のみいくつかの変更は、あなたのコードを動作させるために必要でした。

+0

こんにちはラルフ、これは完璧に動作しています。時間と労力を本当に感謝しています! – user6189814

0

ちょうど1週間前に、指定したフォルダ(ユーザーが選択)に移動し、そのフォルダ内のすべてのExcelファイルとシート名を一覧表示するスクリプトを作成しました。

Public Sub LoopAllExcelFilesInFolder() 

Dim WB As Workbook 
Dim myPath As String 
Dim myFile As String 
Dim myExtension As String 
Dim FldrPicker As FileDialog 
Dim sht As Worksheet 
Dim LastRow As Long 

Application.DisplayAlerts = False 

Sheets("ListFilesInFolder").Select 
Set sht = ThisWorkbook.Worksheets("ListFilesInFolder") 
sht.Activate 
Rows("2:2").Select 
Range(Selection, Selection.End(xlDown)).Select 
Selection.ClearContents 
Range("A1").Select 


    Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker) 

    With FldrPicker 
     .Title = "Select A Target Folder" 
     .AllowMultiSelect = False 
     If .Show <> -1 Then GoTo NextCode 
     myPath = .SelectedItems(1) & "\" 
    End With 

'In Case of Cancel 
NextCode: 
    myPath = myPath 
    If myPath = "" Then GoTo ResetSettings 

'Target File Extension (must include wildcard "*") 
myExtension = "*.xl*" 

'Target Path with Ending Extention 
myFile = Dir(myPath & myExtension) 

    Do While myFile <> "" 

     Set WB = Workbooks.Open(Filename:=myPath & myFile) 

     With Application 
      .AskToUpdateLinks = False 
     End With 

     For Each Sheet In Workbooks(myFile).Worksheets 
     LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row + 1 
      Workbooks("Questionaire-Mock-Up.xlsb").Worksheets("ListFilesInFolder").Cells(LastRow, 1).Value = myPath & myFile 
      Workbooks("Questionaire-Mock-Up.xlsb").Worksheets("ListFilesInFolder").Cells(LastRow, 2).Value = myFile 
      Workbooks("Questionaire-Mock-Up.xlsb").Worksheets("ListFilesInFolder").Cells(LastRow, 3).Value = Sheet.Name 
       File = InStr(myFile, ".xl") - 1 
       LeftName = Left(myFile, File) 
      Workbooks("Questionaire-Mock-Up.xlsb").Worksheets("ListFilesInFolder").Cells(LastRow, 4).Value = LeftName 
      LastRow = LastRow + 1 
     Next Sheet 

     Workbooks(myFile).Close SaveChanges:=False 
     myFile = Dir 
    Loop 

ResetSettings: 

Application.DisplayAlerts = True 

End Sub 
関連する問題