Row.Cells不返回目标范围内的所有单元格

时间:2018-06-06 18:12:06

标签: excel excel-vba vba

如果满足某些条件,我正在尝试编写一些VB代码来填充单元格中的值:

Private Sub CommandButton1_Click()

    Dim targetRange As range
    Dim isBBset As Boolean
    Dim isDiagnosisSet As Boolean
    Dim isDiureticSet As Boolean
    Dim rowCounter As Long

    rowCounter = 1
    Set targetRange = Union(range("AC:AE"), range("AH:AL"), range("AP:AS"), range("AW:BA"), range("BE:BH"))
    targetRange.Font.Bold = True
    For Each Row In targetRange.Rows

        If rowCounter > 1 And rowCounter <= 201 Then
            isBBset = False
            For Each cell In Row.Cells
                Dim stringVal As String

                If IsNumeric(cell.Value) = True Then
                    stringVal = CStr(cell.Value)
                Else
                    stringVal = cell.Value
                End If
                If stringVal = "19" Or stringVal Like "19+*" Or stringVal Like "*+19+*" Or stringVal Like "*+19" Then
                    range("BJ" & rowCounter).Value = 0
                    isBBset = True
                ElseIf stringVal = "20" Or stringVal Like "20+*" Or stringVal Like "*+20+*" Or stringVal Like "*+20" Then
                    range("BJ" & rowCounter).Value = 0
                    isBBset = True
                ElseIf stringVal = "21" Or stringVal Like "21+*" Or stringVal Like "*+21+*" Or stringVal Like "*+21" Then
                    range("BJ" & rowCounter).Value = 0
                    isBBset = True
                ElseIf stringVal = "22" Or stringVal Like "22+*" Or stringVal Like "*+22+*" Or stringVal Like "*+22" Then
                    range("BJ" & rowCounter).Value = 0
                    isBBset = True
                ElseIf stringVal = "23" Or stringVal Like "23+*" Or stringVal Like "*+23+*" Or stringVal Like "*+23" Then
                    range("BJ" & rowCounter).Value = 0
                    isBBset = True
                ElseIf stringVal = "24" Or stringVal Like "24+*" Or stringVal Like "*+24+*" Or stringVal Like "*+24" Then
                    range("BJ" & rowCounter).Value = 0
                    isBBset = True
                End If
            Next cell
            If isBBset = False Then
                range("BJ" & rowCounter).Value = 1
            End If
        End If
        rowCounter = rowCounter + 1
    Next Row
End Sub

我通过union得到目标范围,然后我遍历每一行。我希望当我做Row.Cells时,我会从我的范围中获取该行的所有单元格,但是我只从我放入联合的第一个范围中获取单元格。如何得到其余的?

1 个答案:

答案 0 :(得分:0)

你可以考虑什么(这只是作为提示):

  • 获取目标变量的最后一行:

    Dim LR as long
    LR = targetRange.Find("*", , , , xlByRows, xlPrevious).Row
    
  • 然后从第1行循环到LR,如:

    Dim RW as long
    For RW = 1 to LR
        'Your code to do stuff
    Next RW
    
  • 在此循环中,遍历targetrange中使用过的列

    Dim CL as range
    For Each CL in targetRange.Columns
        Debug.Print Cells(RW, CL.Column).value
    Next CL
    
  • 所以总计看起来像:

    Dim LR as long, RW as long
    Dim CL as range
    
    LR = targetRange.Find("*", , , , xlByRows, xlPrevious).Row
    For RW = 1 to LR
        If RW > 1 AND RW <= 200 then
            For Each CL in targetRange.Columns
                Debug.Print Cells(RW, CL.Column).value 'Your code goes here
            Next CL
        End if    
    Next RW
    

希望有助于解决您的问题