Excel VBA循环通过一系列工作簿上的工作表

我有一个主macros工作簿,唯一的目的是运行一个macros,循环通过特定文件夹中的所有工作簿,进行一堆更改,然后将它们保存到不同的文件夹。

所有这些工作,除了一些新的代码,我想循环所有不同的工作表。 代码只是一次又一次地在第一个工作表上运行代码。

Sub BlendBCoding() Dim Filename, Pathname As String Dim wb As Workbook Dim ws As Worksheet Dim NameOfWorkbook Dim cel As Variant Dim myrange As Range Pathname = ActiveWorkbook.Path & "\ToProcess\" Filename = Dir(Pathname & "*.xml") Do While Filename <> "" Set wb = Workbooks.Open(Pathname & Filename) For Each ws In wb.Sheets Call DoWork(ws) Next NameOfWorkbook = Left(ActiveWorkbook.Name, (InStrRev(ActiveWorkbook.Name, ".", -1, vbTextCompare) - 1)) ActiveWorkbook.SaveAs Filename:= _ "I:\Common\BlendBCoding\Processed\" & NameOfWorkbook & ".xlsx", FileFormat _ :=xlOpenXMLWorkbook, CreateBackup:=False wb.Close SaveChanges:=False Filename = Dir() Loop End Sub Sub DoWork(ws As Worksheet) With ws Range("A1:G1").EntireColumn.Insert Range("A1").Value = "Scan Components" Range("A1").ColumnWidth = 16 //Blah Blah lots of standard text code cut Set myrange = Range("H1:H100") myrange.Interior.ColorIndex = xlNone For Each cel In myrange If Application.WorksheetFunction.CountIf(myrange, cel) > 1 Then cel.Interior.ColorIndex = 4 End If Next 'Set myrange = Range("H2:H25") 'For Each xCell In myrange ' xCell.Value = CDec(xCell.Value) ' Next xCell End With End Sub 

任何帮助是极大的赞赏。

你不是指着ws的范围

使用. 否则你会引用ActiveSheet

 With ws .Range("A1:G1").EntireColumn.Insert .Range("A1").Value = "Scan Components" .Range("A1").ColumnWidth = 16 //Blah Blah lots of standard text code cut Set myrange = .Range("H1:H100") myrange.Interior.ColorIndex = xlNone For Each cel In myrange If Application.WorksheetFunction.CountIf(myrange, cel) > 1 Then cel.Interior.ColorIndex = 4 End If Next End With