VBA excel:对于..下一个不会通过选定的范围?
我正在尝试运行我的代码:
Sub Test() Dim vuosi As Integer Dim kk As Integer Dim cF As Range Dim c As String Dim cell As Range vuosi = Application.InputBox(Prompt:="Syötä vuosi, jota haluat tarkastella.") kk = Application.InputBox(Prompt:="Syötä kuukausi(1-12), jota haluat tarkastella.") If vuosi = 2014 Then c = "BU" ElseIf vuosi = 2015 Then c = "CG" ElseIf vuosi = 2016 Then c = "CS" End If ActiveSheet.Range("F11:F60").Select For Each cell In Selection Cells(ActiveCell.Row, c).Activate Set cF = Range(ActiveCell.Offset(0, kk - 12), ActiveCell.Offset(0, kk)).Find(What:=1, LookIn:=xlFormulas, LookAT:=xlPart, SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False) If Not cF Is Nothing Then Cells(ActiveCell.Row, "F").Interior.ColorIndex = 6 End If Next cell End Sub
它不能正常工作。 看来For Each循环只经过第一行。 谁能告诉我为什么?
程序应该遍历F列中的所有单元格。对于每一行,它将检查在特定单元格中是否存在值1。 如果是的话,F列的单元格应该涂成黄色。 否则程序会一直持续到列F中的最后一个值。(在我的testing中,我只用了Range(“F11:F60”)
我对你的代码有一些观察。 让我试着掩盖我注意到的一切。
A )使用Excel行时,不要将它们声明为Integers
。 发布Excel 2007后,行数已经达到1048576,这对Integer
来说太大了。 在使用列时可以离开,但可能在行中有问题。 将它们声明为Long
是个好习惯
B )你假定用户将始终在input框中input值。 如果用户input一个空白或按取消怎么办? 捕获这些实例。 如果您只想在input框中input数字,请使用Type:=1
。 有关Excel帮助中的Application.InputBox Method
更多详细信息,请阅读。
C )避免使用。select.Select/.Activate
您的代码可以轻松运行, .Select/.Activate
select任何东西。 你可能想看到这个
这是你正在尝试? ( 未经testing )
Sub Test() Dim vuosi As Integer, kk As Long Dim rng As Range, aCell As Range, rngToFind As Range, cF As Range Dim c As String Dim ws As Worksheet vuosi = Application.InputBox(Prompt:="Syötä vuosi, jota haluat tarkastella.", Type:=1) If vuosi < 1 Then Exit Sub kk = Application.InputBox(Prompt:="Syötä kuukausi(1-12), jota haluat tarkastella.", Type:=1) If kk < 1 Then Exit Sub If vuosi = 2014 Then c = "BU" If vuosi = 2015 Then c = "CG" If vuosi = 2016 Then c = "CS" Set ws = ThisWorkbook.Sheets("Sheet1") With ws Set rng = ws.Range("F11:F60") For Each aCell In rng Set rngToFind = .Range(aCell.Offset(0, kk - 12), aCell.Offset(0, kk)) Set cF = rngToFind.Find(What:=1, LookIn:=xlFormulas, LookAT:=xlPart, _ SearchOrder:=xlByColumns, MatchCase:=False, _ SearchFormat:=False) If Not cF Is Nothing Then .Range("F" & aCell.Row).Interior.ColorIndex = 6 Next cell End With End Sub
这个页面将解释ActiveCell.Row
如何引用选中的单个行。
在这种情况下,您可以replace:
ActiveSheet.Range("F11:F60").Select For Each cell In Selection Cells(ActiveCell.Row, c).Activate
有:
Set rng = ActiveSheet.Range("F11:F60") For Each cell In rng Cells(cell.Row, c).Activate
另外, rng
应该事先声明为Range 。
对于嵌套的If … End If语句,请确保在每个封闭的If … End If结构中都有正确匹配的If … End If结构。
Sub Test() Dim Vuosi As Integer Dim KK As Integer Dim CF As Range Dim C As String Dim cell As Range Vuosi = Application.InputBox(Prompt:="Enter the year you wish to view.") KK = Application.InputBox(Prompt:="Enter the month (1-12) you wish to view.") If Vuosi = 2014 Then C = "BU" Else If Vuosi = 2015 Then C = "CG" Else If Vuosi = 2016 Then C = "CS" End If ActiveSheet.Range("F11:F60").Select For Each cell In Selection Cells(ActiveCell.Row, C).Activate Set CF = Range(ActiveCell.Offset(0, KK - 12), _ ActiveCell.Offset(0, KK)).Find(What:=1, _ LookIn:=xlFormulas, LookAT:=xlPart, _ SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False) If Not CF Is Nothing Then Cells(ActiveCell.Row, "F").Interior.ColorIndex = 6 End If Next cell End If End If End Sub
你在你的循环中改变你的select。 每次for each cell in selection
Excel for each cell in selection
都会重新评估select,因此它只运行一次。
代替
... ActiveSheet.Range("F11:F60").Select For Each cell In Selection ...
尝试
... dim r as range ... set r = activesheet.range("F11:F60") For Each cell In Selection.cells ...