为什么在我select另一张纸后,在第三排停下来

我正在通过删除和添加更多数据来testing我的编码,而当我意识到编码出现问题时。 最初,我在“LEAVE”部分下开始了3人的编码。

所以我试图删除这个部分的人,编码仍然有效。 当时我在那个部分增加了一个人,然后我意识到我的编码不起作用。 当我将数据从3人改为4人时,仍停在第三人。 它没有继续循环检测第四人。

我testing并意识到,当我开始的时候

Sheets("Cash").Select 

从此它不起作用。

没有这个,循环仍然有效,它可以检测到第四人。

 Do While Cells(ShiftRowName, 1) <> "" Set ShiftName = Sheets("Shift Roster").Cells(ShiftRowName, ShiftColName) If ShiftName = "" Or IsEmpty(ShiftName) Then Exit Do Else 'When the following coding was added, 'It stopped detecting at the 3rd person. Sheets("Cash").Select Range("C1").Select Cells.Find("Name").Activate r2 = ActiveCell.Row CashRowName = r2 + 1 Do While Cells(CashRowName, 1) <> "" Set CashName = Sheets("Cash").Cells(CashRowName, CashColName) If CashName = "" Or IsEmpty(CashName) Then Exit Do Else MsgBox CashName MsgBox ShiftName End If CashRowName = CashRowName + 1 Loop End If ShiftRowName = ShiftRowName + 1 Loop 

任何可能解决这个问题?

编辑:

由于前锋埃德的启示,我find了我的问题的答案。 谢谢。

我使用了下面的代码

 With Sheets("Cash") Set FindRow = .Range("C:C").Find(What:="Name", LookIn:=xlValues) End With Dim FindRowNumber As Long CashRowName = FindRow.Row 

  'When the following coding was added, 'It stopped detecting at the 3rd person. r2 = Sheets("Cash").Range("C1").Find("Name").row 'not sure if the above coding will actually work, 'but it avoids switching sheets and tries to work 'directly on worksheet "CASH". CashRowName = r2 + 1 

这是你的代码编辑的一小段。 我很确定我的语法是closures的。 要点是避免在循环中间改变工作表。 另外,重复使用select会减慢你的代码,应该避免。