使用每个循环无法循环通过工作表

您好我一直在试图将几个macros连接在一起,以自动化一个过程,涉及从文本导入数据文件和复制部分数据到“核心表”。 我的问题出现时,我尝试使用“For Each循环”移动通过工作表检查条件,如果条件满足运行数据提取macros。 基本上macros不循环,我研究了各种解决scheme,但没有任何工作。 它在第一次运行时工作正常(通常情况下显示的表格符合条件,因此移动到“shima”macros),然后显示“core sheet”,因为这不符合我只想要的条件它移动到下一张表,但它没有。 下面的代码让我知道,如果任何这是不清楚的。

Sub FullAuto() Call Module1.Myfolderselector 'this macro imports all the text file in a given folder Dim Msg, Style, Title, Response, MyString Public ws As Worksheet Msg = "Yes for Fluorescence, No for UV" Style = vbYesNo + vbCritical + vbDefaultButton1 Title = "Choose data" 'this parts asks the user which type of Data they want to import Response = MsgBox(Msg, Style, Title) If Response = vbYes Then MyString = "Yes" Else MyString = "No" End If For Each ws In Worksheets 'this is the part with issues. If MyString = "Yes" And Range("A1").Value <> "Core" Then Call Module1.Detector_B_Shima_9_0 ElseIf MyString = "No" And Range("A1").Value <> "Core" Then Call Module1.Detector_A_Shima_9_1 End If Next ws Worksheets.Add(After:=Worksheets(1)).Name = "Plot Sheet" End Sub Sub Detector_B_Shima_9_0() Cells.Select Selection.Find(What:="[LC Chromatogram(Detector B-Ch1)]", After:=ActiveCell _ , LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _ SearchDirection:=xlNext, MatchCase:=False).Activate ActiveCell.Offset(9, 1).Select ActiveCell.Value = Range("B20") Range(ActiveCell, Cells(ActiveCell.Row + 8401, ActiveCell.Column)).Select Selection.Copy Sheets("Core Sheet").Select Range("C3").Select Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False ActiveCell.EntireColumn.Insert End Sub 

尝试改变这个代码:

 For Each ws In Worksheets 'this is the part with issues. If MyString = "Yes" And Range("A1").Value <> "Core" Then Call Module1.Detector_B_Shima_9_0 ElseIf MyString = "No" And Range("A1").Value <> "Core" Then Call Module1.Detector_A_Shima_9_1 End If Next ws 

到下一个

 For Each ws In Worksheets If MyString = "Yes" And ws.Range("A1").Value <> "Core" Then Call Module1.Detector_B_Shima_9_0 ElseIf MyString = "No" And ws.Range("A1").Value <> "Core" Then Call Module1.Detector_A_Shima_9_1 End If Next ws 

请注意,我已经添加了ws. Range("A1")ws.Range("A1").Value 。 这个小改进有助于VBA理解Range("A1")属于工作表ws