动态单元格范围VBA

时间:2017-09-01 14:30:20

标签: excel vba excel-vba

我有一个正在研究的宏。如果单元格运行一段代码,它会检测单元格的值是否为NA。

问题:到目前为止,我只能在一个单元格上运行它。我正在尝试将以下单元格应用于我的宏。

Sheet3.Range D(20,24,25,27,28,30,31,32,33,34,35,37,38,40,42,43,44,54,55,56,58, 59,61,62,63,64,65)

Sheet3.Range E(20,24,25,27,28,30,31,32,33,34,35,37,38,40,42,43,44,54,55,56,58, 59,61,62,63,64,65)

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim sCellVal As String

sCellVal = Range("D20")

If sCellVal Like "*NA*" Then

    Range("D20") = "Not applicable" & "  " & Environ("Username") & "  " & Format(Now, "yyyy-MM-dd hh:mm:ss")

End If
End Sub

4 个答案:

答案 0 :(得分:1)

还有另一种方法。您可以按照以下步骤操作:

  1. 使用IFERROR(cellReferenceHere; functionName())
  2. 在VBA上将其写为公共功能。
  3. #N / A发生时会一直触发

    实施例: 细胞:

    =IFERROR(0/0;doSomething())
    

    VBA:

    Public Function doSomething()
        doSomething = "Done"
    End Function
    

答案 1 :(得分:0)

我没有看到此范围内使用的行有任何明显的逻辑,因此我们将对Case开关中的行号进行硬编码,然后检查Target.Row < / p>

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    '// Check if the target row number is in our array:
    Select Case Target.Row
        Case 20, 24, 25, 27, 28, 30, 31, 32, 33, 34, 35, 37, 38, 40, 42, 43, 44, 54, 55, 56, 58, 59, 61, 62, 63, 64, 65
            ' Do Something
            If Cstr(Target.Value) Like "*NA*" Then

                Target.Value = "Not applicable" & "  " & Environ("Username") & "  " & Format(Now, "yyyy-MM-dd hh:mm:ss")

            End If

        Case Else
            ' Do nothing
            Exit Sub
    End Select
End Sub

注意:如果您正在寻找错误值,而不是与Like运算符进行字符串比较,请检查真正的错误等效性:

If Target.Value = CvErr(2042) Then 
    Target.Value = "Not applicable" & "  " & Environ("Username") & "  " & Format(Now, "yyyy-MM-dd hh:mm:ss")
End If

答案 2 :(得分:0)

在这里,我只是制作了一个数组并对数组进行了快速循环,以查看所有范围并测试条件。

Sub test()
Dim D As Variant
Dim rng1 As Range
Dim rng2 As Range
Dim x As Integer


D = Array(20, 24, 25, 27, 28, 30, 31, 32, 33, 34, 35, 37, 38, 40, 42, 43, 44, 54, 55, 56, 58, 59, 61, 62, 63, 64, 65)


For x = LBound(D) To UBound(D)
Set rng1 = Cells(D(x), 4)
Set rng2 = Cells(D(x), 5)
If rng1 Like "*NA*" Then
rng1 = "Not applicable" & "  " & Environ("Username") & "  " & Format(Now, "yyyy-MM-dd hh:mm:ss")
End If
If rng2 like "*NA*" then 
   rng2 = "Not applicable" & "  " & Environ("Username") & "  " & Format(Now, "yyyy-MM-dd hh:mm:ss")
End if
Next x


End Sub

答案 3 :(得分:0)

对于范围似乎不是一个可辨别的模式,因此我将它们硬编码为数组。这可以满足您的需求,但我会留给您创建E列数组:

Sub Worksheet_Change()
Dim ws_data As Worksheet
Dim arr_Range(0 To 9) As String
Dim i as Integer

Set ws_data = ThisWorkbook.Sheets("Data") 'change to your sheet name

arr_Range(0) = "D20"
arr_Range(1) = "D24:D25"
arr_Range(2) = "D27:D28"
arr_Range(3) = "D30:D35"
arr_Range(4) = "D37:D38"
arr_Range(5) = "D40"
arr_Range(6) = "D42:D44"
arr_Range(7) = "D54:D56"
arr_Range(8) = "D58:D59"
arr_Range(9) = "D61:D65"

'Loop through array
For i = LBound(arr_Range) To UBound(arr_Range)
    For Each Cell In ws_data.Range(arr_Range(i)) 'check each cell in the current range string in the array
        If Cell.Value Like "*NA*" Then
            Cell.Value = "Not applicable" & "  " & Environ("Username") & "  " & Format(Now, "yyyy-MM-dd hh:mm:ss")
        Else: End If
    Next Cell
Next i
End Sub