Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 将错误#N/A替换为今天';VBA中的日期_Excel_Vba - Fatal编程技术网

Excel 将错误#N/A替换为今天';VBA中的日期

Excel 将错误#N/A替换为今天';VBA中的日期,excel,vba,Excel,Vba,我希望使用以下代码将#N/A(由于查找搜索而产生)替换为今天的日期,但它不起作用。你能告诉我这个逻辑是否有效或者需要应用其他技术吗 Const StartRow As Byte=2 LastRow = Range("J" & Rows.count).End(xlUp).Row For i = StartRow To LastRow myValue = Range("B" & i).Value If myValue =S

我希望使用以下代码将
#N/A
(由于查找搜索而产生)替换为今天的日期,但它不起作用。你能告诉我这个逻辑是否有效或者需要应用其他技术吗

Const StartRow As Byte=2
LastRow = Range("J" & Rows.count).End(xlUp).Row

For i = StartRow To LastRow

    myValue = Range("B" & i).Value

    If myValue =Specialcells(xlCellTypeFormulas,xlErrors) 
          Then Range("B" & i).Value = "=Today()"

  Next i

您可以使用应用程序类的
工作表函数
方法检查特定的
#N/A
错误

For i = StartRow To LastRow
    
    If Application.WorksheetFunction.IsNA(Range("B" & i)) Then
        Range("B" & i).Formula = "=Today()"
    End If
    
Next
正如我经常说的,在任何情况下,都应该完全限定范围对象。即使您希望使用
活动…
无论它是什么:

Dim Rng As Range
Set Rng = ThisWorkbook.ActiveSheet.Range(...)
' Or
Set Rng = ActiveWorkbook.Worksheets(1).Range(...)
当像您这样为i=循环使用
时,我总是喜欢使用
Cells()
属性,而不是使用
Range()
,因为我不喜欢在每次循环迭代中使用VBA将字符串连接在一起:

For i = StartRow To LastRow

    If Application.WorksheetFunction.IsNA(Cells(i, 2)) Then
        Cells(i, 2).Formula = "=Today()"
    End If

Next

但我认为这最终取决于偏好。

您可以使用应用程序类的
工作表函数
方法检查特定的
\N/A
错误

For i = StartRow To LastRow
    
    If Application.WorksheetFunction.IsNA(Range("B" & i)) Then
        Range("B" & i).Formula = "=Today()"
    End If
    
Next
正如我经常说的,在任何情况下,都应该完全限定范围对象。即使您希望使用
活动…
无论它是什么:

Dim Rng As Range
Set Rng = ThisWorkbook.ActiveSheet.Range(...)
' Or
Set Rng = ActiveWorkbook.Worksheets(1).Range(...)
当像您这样为i=
循环使用
时,我总是喜欢使用
Cells()
属性,而不是使用
Range()
,因为我不喜欢在每次循环迭代中使用VBA将字符串连接在一起:

For i = StartRow To LastRow

    If Application.WorksheetFunction.IsNA(Cells(i, 2)) Then
        Cells(i, 2).Formula = "=Today()"
    End If

Next

但我想这最终要归结为偏好。

您可以尝试将与结合起来,如下所示:

ActiveSheet.Range(ActiveSheet.Cells(StartRow,2), ActiveSheet.Cells(LastRow,2)).Replace _
    CVErr(xlErrNA), Date, xlWhole

您可以尝试将与相结合,如下所示:

ActiveSheet.Range(ActiveSheet.Cells(StartRow,2), ActiveSheet.Cells(LastRow,2)).Replace _
    CVErr(xlErrNA), Date, xlWhole
用日期或现在替换错误
  • 第一个代码使用循环并用“日期”(当前日期)或“现在”(当前日期和时间)替换所有错误值
  • 要使用特殊单元格,您不需要在行中循环,而是将其应用于整个范围。这在第二个代码中显示
代码

Option Explicit

Sub replaceWithDate()

    ' Constants
    Const LastRowCol As Variant = "J"  ' Last Row Column Index
    Const StartRow As Long = 2         ' Start Row Number
    Const CriteriaCol As Variant = "B" ' Criteria Column Index
    
    ' Define the row of the last non-blank cell in column 'J' ('LastRow').
    Dim LastRow
    LastRow = Range(LastRowCol & Rows.Count).End(xlUp).Row

    ' Additional variables to be used in 'For Next' loop.
    Dim myValue As Variant ' Current Value
    Dim i As Long          ' Row Counter
    
    ' Loop through rows.
    For i = StartRow To LastRow
        ' Write each value to Current Value.
        myValue = Range(CriteriaCol & i).Value
        ' Test if Current Value contains an error value.
        If IsError(myValue) Then
            ' Write 'Today()'
            'Range(CriteriaCol & i).Value = "=Today()"
            ' If you use the previous line, then when you open
            ' the worksheet tomorrow, it will have tomorrow's date.
            ' Write 'Date'. You can also use 'Now' to include time.
            Range(CriteriaCol & i).Value = Date ' Now
            ' Additionally you can change the number format:
            ' e.g. for Date
            'Range(CriteriaCol & i).NumberFormat = "mm/dd/yyyy"
            ' e.g. for Now
            'Range(CriteriaCol & i).NumberFormat = "mm/dd/yyyy hh:mm:ss"
        
        End If
    Next i

End Sub

Sub replaceWithDateSpecialCells()

    ' Constants
    Const LastRowCol As Variant = "J"  ' Last Row Column Index
    Const StartRow As Long = 2         ' Start Row Number
    Const CriteriaCol As Variant = "B" ' Criteria Column Index

    ' Define the row of the last non-blank cell in column 'J' ('LastRow').
    Dim LastRow
    LastRow = Range(LastRowCol & Rows.Count).End(xlUp).Row
    
    ' Define Criteria Range ('rng').
    Dim rng As Range
    Set rng = Range(CriteriaCol & StartRow, CriteriaCol & LastRow)
    
    ' Apply 'SpecialCells'.
    On Error Resume Next
    rng.SpecialCells(xlCellTypeFormulas, xlErrors).Value = Date
    On Error GoTo 0
    
End Sub
用日期或现在替换错误
  • 第一个代码使用循环并用“日期”(当前日期)或“现在”(当前日期和时间)替换所有错误值
  • 要使用特殊单元格,您不需要在行中循环,而是将其应用于整个范围。这在第二个代码中显示
代码

Option Explicit

Sub replaceWithDate()

    ' Constants
    Const LastRowCol As Variant = "J"  ' Last Row Column Index
    Const StartRow As Long = 2         ' Start Row Number
    Const CriteriaCol As Variant = "B" ' Criteria Column Index
    
    ' Define the row of the last non-blank cell in column 'J' ('LastRow').
    Dim LastRow
    LastRow = Range(LastRowCol & Rows.Count).End(xlUp).Row

    ' Additional variables to be used in 'For Next' loop.
    Dim myValue As Variant ' Current Value
    Dim i As Long          ' Row Counter
    
    ' Loop through rows.
    For i = StartRow To LastRow
        ' Write each value to Current Value.
        myValue = Range(CriteriaCol & i).Value
        ' Test if Current Value contains an error value.
        If IsError(myValue) Then
            ' Write 'Today()'
            'Range(CriteriaCol & i).Value = "=Today()"
            ' If you use the previous line, then when you open
            ' the worksheet tomorrow, it will have tomorrow's date.
            ' Write 'Date'. You can also use 'Now' to include time.
            Range(CriteriaCol & i).Value = Date ' Now
            ' Additionally you can change the number format:
            ' e.g. for Date
            'Range(CriteriaCol & i).NumberFormat = "mm/dd/yyyy"
            ' e.g. for Now
            'Range(CriteriaCol & i).NumberFormat = "mm/dd/yyyy hh:mm:ss"
        
        End If
    Next i

End Sub

Sub replaceWithDateSpecialCells()

    ' Constants
    Const LastRowCol As Variant = "J"  ' Last Row Column Index
    Const StartRow As Long = 2         ' Start Row Number
    Const CriteriaCol As Variant = "B" ' Criteria Column Index

    ' Define the row of the last non-blank cell in column 'J' ('LastRow').
    Dim LastRow
    LastRow = Range(LastRowCol & Rows.Count).End(xlUp).Row
    
    ' Define Criteria Range ('rng').
    Dim rng As Range
    Set rng = Range(CriteriaCol & StartRow, CriteriaCol & LastRow)
    
    ' Apply 'SpecialCells'.
    On Error Resume Next
    rng.SpecialCells(xlCellTypeFormulas, xlErrors).Value = Date
    On Error GoTo 0
    
End Sub

请尝试下一个代码。无需迭代:

  Dim lastRow As Long, sh As Worksheet
  Set sh = ActiveSheet 'use here the necessary sheet
  lastRow = sh.Range("B" & Rows.count).End(xlUp).row
  sh.Range("B2:B" & lastRow).SpecialCells(xlCellTypeFormulas, xlErrors).Formula = "=Today()"
或在单个代码行中:

Range("B2:B" & Range("B" & Rows.count).End(xlUp).row).SpecialCells(xlCellTypeFormulas, xlErrors).Formula = "=Today()"
或者(没有公式):


请尝试下一个代码。无需迭代:

  Dim lastRow As Long, sh As Worksheet
  Set sh = ActiveSheet 'use here the necessary sheet
  lastRow = sh.Range("B" & Rows.count).End(xlUp).row
  sh.Range("B2:B" & lastRow).SpecialCells(xlCellTypeFormulas, xlErrors).Formula = "=Today()"
或在单个代码行中:

Range("B2:B" & Range("B" & Rows.count).End(xlUp).row).SpecialCells(xlCellTypeFormulas, xlErrors).Formula = "=Today()"
或者(没有公式):