计算列中的所有行

时间:2014-10-13 15:09:36

标签: vba excel-vba excel

我在vba中搜索一个具有特定标题的列,然后当我发现我想要搜索该列中的所有行并用1' s替换所有X时。我写了所有代码但由于某种原因它不允许下面显示的行:

r2 = Range(i, i).EntireColumn.Rows.Count



Sub PA_Change()

Dim i As Long, r As Range, rRow As Range, r2 As Long

Set r = Range("A1")
Set rRow = r.EntireRow

For i = 1 To rRow.Columns.Count

    If Cells(1, i) = PA_REQUIRED Then
        r2 = Range(i, i).EntireColumn.Rows.Count
    For j = 1 To r2
        If Cells(j, i).Value = "X" Then
            Cells(j, i).Value = "1"
        End If
    Next j
    End If

Next i

End Sub

1 个答案:

答案 0 :(得分:0)

尝试用此替换所有代码,并告知我们是否有效:

*将"boo"中的searchFor替换为实际的标题名称/ PA_REQUIRED

Sub PA_Change()

    Dim searchFor As String
    searchFor = "boo"

    Dim grabColumn As Range
    Set grabColumn = Rows("1:1").Find(What:=searchFor, _
                     After:=Range("A1"), LookIn:=xlValues, _
                     LookAt:=xlPart, SearchOrder:=xlByColumns, _
                     SearchDirection:=xlNext, MatchCase:=False, _
                     SearchFormat:=False)

    If Not grabColumn Is Nothing Then
        Dim entireColumn As Range
        Set entireColumn = Range(grabColumn.Address & ":" & Split(grabColumn.Address, "$")(1) & Range(Split(grabColumn.Address, "$")(1) & Rows.Count).End(xlUp).Row)
        Dim cell As Range
        For Each cell In entireColumn
            If UCase(cell) = "X" Then
                cell = "1"
            End If
        Next
    Else
        Exit Sub ' not found
    End If

End Sub