【问题标题】:vba search range for value, if value exists change each value?vba搜索值的范围,如果值存在更改每个值?
【发布时间】:2017-03-02 10:02:32
【问题描述】:

我有一个这样的电子表格:

Column D        Column G

1               #VALUE!
2               #VALUE!
#VALUE!         3

我正在尝试在我的范围内搜索值 #VALUE!。

在哪里找到这个值,我想更改每次出现的#VALUE!待定。

这是我的代码:

    Range("D30:G39").Select
                    Set cell = Selection.Find(What:="#VALUE!", After:=ActiveCell, LookIn:=xlFormulas, _
                    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=False, SearchFormat:=False)

                    If cell Is Nothing Then
                    'do it something
                    Else

For Each Cell.Value
                    cell.value = "TBC"
Next Cell
'End For

                    wStemplaTE.Range("A41").value = "Please fill in the pallet factor and case size accordingly. Please amend total volume if necessary to accommodate full pallets."
                    End If

我需要尝试创建一个循环,以便将每个单元格值更改为 TBC。

我是 VBA 的新手,不太确定该怎么做。

请有人指出我正确的方向/告诉我如何正确地做到这一点?谢谢

编辑:

For Each Cell.Value

【问题讨论】:

  • 试试看 xlPart
  • 为什么不使用查找和替换?
  • @Nathan_Sav : 不,这里的问题是LookIn:=xlFormulas,但应该是xlValues
  • 我写了Cells.Find(What:="#ref", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= False, SearchFormat:=False) 并且工作正常
  • @Nathan_Sav 你能告诉我你为这行写了什么:For Each Cell.Value?此行表示格式不正确并突出显示红色

标签: vba excel


【解决方案1】:

这样就可以了:

Sub Macro1()
Dim Cel, Rng As Range
Set Rng = Range("D30:G39")

For Each Cel In Rng
    If Err.Number = 13 Then Cel.Value = "TBC"
Next

End Sub

【讨论】:

  • “#VALUE!”是否重要?是一个值而不是一个错误?此范围内的值已从另一个电子表格复制 - 这产生了原始错误
  • VBA 识别“#VALUE!”作为一个错误(而不是绝对值) - 我认为这是由于 # 前缀(但不确定)
【解决方案2】:

这是使用Range.Find方法的正确方法,你应该每次都使用:

(顺便说一句,我之前已经在更大的代码块中用这样的结构回答过你)

Sub test_user7415328()
Dim FirstAddress As String, cF As Range, LookForString As String
LookForString = "#VALUE!"

With ThisWorkbook.Sheets("Sheet's name").Range("D30:G39")
    .Cells(1, 1).Activate
    'First, define properly the Find method
    Set cF = .Find(What:=LookForString, _
                After:=ActiveCell, _
                LookIn:=xlValues, _
                LookAt:=xlWhole, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, _
                MatchCase:=False, _
                SearchFormat:=False)

    'If there is a result, store the address of the 1st one
    If Not cF Is Nothing Then
        FirstAddress = cF.Address
        Do
            'Do your actions
            cF.Value = "TBC"
            'Keep looking with FindNext method
            Set cF = .FindNext(cF)
        'Look until you find again the first result
        Loop While Not cF Is Nothing And cF.Address <> FirstAddress
    End If
End With

End Sub

【讨论】:

    【解决方案3】:

    您可以检查单元格的值是否等于CVErr(xlErrValue)。

    Option Explicit
    
    Sub ReplaceErrVal()
    
    Dim C As Range
    
    For Each C In Range("D30:G39")
        On Error Resume Next
        If C.Value = CVErr(xlErrValue) Then
            If Err.Number <> 0 Then
                On Error GoTo 0
            Else
                C.Value = "TBC"
            End If
        End If
    Next
    
    End Sub
    

    【讨论】:

      【解决方案4】:

      使用

      Range("D30:G39").Replace what:="#VALUE!", Replacement:="TBC", lookat:=xlWhole
      

      【讨论】:

      • @user7415328,你试过了吗?
      猜你喜欢
      • 1970-01-01
      • 2018-03-14
      • 1970-01-01
      • 1970-01-01
      • 2018-12-13
      • 1970-01-01
      • 2022-01-09
      • 2019-04-12
      • 1970-01-01
      相关资源
      最近更新 更多