感谢那些提供帮助的人,我自己解决了这个问题。
我有一个依赖于计数器的if语句,我从单元格"B3“中获取计数器的值,代码按原样工作得很好。然而,每次我循环程序时,我需要计数器从不同的单元格中获取它的值。例如,循环1从"B3“读取值,循环2从"C3”读取值,然后是"D3“,依此类推,直到它到达一个空单元格,我有dim c作为整数,c= 2到26将我带到第26列,但我不确定如何让它在每次循环开始时递增。有人能帮上忙吗?我可以发布我目前正在使用的完整代码,如果这有助于理解问题的话
Public Sub copyX()
Dim listofcells As Range
Dim currentname As String
Dim foundrow As Integer
Dim foundcolumn As Integer
Dim counter As Integer
Dim i As Integer
Dim c As Integer
For i = 2 To 26
Sheets("Availability").Activate
counter = Range("b3")
Sheets("Availability").Range("a2").Select
If Not Sheets("Availability").Cells(2, i) = "" Then
Sheets("Availability").Range(Cells(2, i), Cells(2, i).End(xlDown)).Select
Else
GoTo skip: 'If the column has no data then skip to next column
End If
Set listofcells = Selection
Sheets("allocation").Activate
Range("a2").Select
For Each singlecell In listofcells
If counter > 0 Then
If singlecell = "Available" Then
foundcolumn = singlecell.Column 'record the column number where "Available" was found
currentname = Sheets("availability").Range("A" & singlecell.Row) 'record the name of the person in the row where "Available" was found
Set foundName = Sheets("allocation").Range("A:A").Find(What:=currentname, LookIn:=xlValues) 'find the persons name in "Allocation" sheet
foundrow = foundName.Row
Sheets("allocation").Cells(foundrow, foundcolumn) = "X" 'place yes in the same cell as it appeared in "Availability" sheet
counter = counter - 1
End If
End If
Next singlecell
skip:
Next i
End Sub
我想出了下面的代码,它确实遍历了单元格并获得了它们的值,问题是我不能让一个for next循环在另一个for next循环中工作。为计数器范围下一个单元格中的每个单元格设置计数器范围=范围(“b3:z3”)
发布于 2017-07-12 09:55:20
试着用这个概念...
将您的代码修改为
Dim k as Integer
k=3;
counter =Range(Cells(3, k), Cells(3, k)).Value // through this you will get dynamic counter value like B3,D3,E3.,,,,
if(counter !="")
{
//execute some code
}
k=k+1;
https://stackoverflow.com/questions/45052835
复制相似问题