【问题标题】:macro to fill in empty cell with assigned value?用分配的值填充空单元格的宏?
【发布时间】:2020-06-22 22:04:55
【问题描述】:

假设我有一个包含 5 列和约 30,000 列的工作表 - 其中两列是时间戳格式。两个 TS 列都有 ~300 个空白单元格,我想填充一个虚拟 TS 值(1900-01-01 00:00:00)以供以后过滤。如果我为 IF 语句插入一个额外的列,那么公式当然会如下所示:

=IF(B2="","1900-01-01 00:00:00",B2)

但是,我宁愿使用宏来遍历两个 TS 列(让我们将该范围定义为 B2:B30000,C2:C30000)。

非常感谢任何帮助。谢谢!

【问题讨论】:

  • Range.SpecialCells(xlCellTypeBlanks) 应该会有所帮助。您可以通过几个步骤手动完成此操作,无需使用 VBA。
  • 感谢您的快速回复!是的,我想我可以只添加过滤器并手动填充(它真的不会花很长时间,你是对的),但我一直在尝试学习更多的 VBA,并认为这将是一个很好的练习。谢谢!

标签: excel vba loops


【解决方案1】:

希望它能解决您的问题。

Option Explicit
    
Sub Fill_In_Value()
    
'Turn off following processes to speed up code
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual


Dim ws As Worksheet
Dim i As Long
Dim lRow As Long
Dim Column1 As Long
Dim Column2 As Long


Column1 = 1 '1 = To Column A, this setup is easier to change the column later on
Column2 = 2 '2 = To Column B
Set ws = ActiveWorkbook.Worksheets("Sheet1") 'Set the worksheetname



lRow = ws.Cells(Rows.Count, "A").End(xlUp).Row 'Find the last row in Column A

For i = 2 To lRow 'Loop from row 2 to last row

    If ws.Cells(i, Column1) = "" Or ws.Cells(i, "B") = "" Then 'If any of the column A or column B is empty then:
    
        If ws.Cells(i, Column1) = "" Then 'If column A have blank value then:
            ws.Cells(i, Column1).Value = "1900-01-01 00:00:00" 'Add dummy value
            
            If ws.Cells(i, Column2) = "" Then 'If column A have blank value, also check column B.
                ws.Cells(i, Column2).Value = "1900-01-01 00:00:00" 'Add dummy value
            End If
            
        ElseIf ws.Cells(i, Column2) = "" Then 'If Column A is not blank, B should be blank
            ws.Cells(i, Column2).Value = "1900-01-01 00:00:00"
        End If
    End If

Next i

'Turn on following processes to get back to normal state
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic


MsgBox ("Complete") 'Message box to show when code is finished

End Sub

更快的版本

Sub Fill_In_Value2()

'Turn off following processes to speed up code
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual


Dim ws As Worksheet
Dim i As Long
Dim lRow1 As Long
Dim lRow2 As Long
Dim Column1 As Long
Dim Column2 As Long


Column1 = 1 '1 = To Column A, this setup is easier to change the column later on
Column2 = 2 '2 = To Column B
Set ws = ActiveWorkbook.Worksheets("Sheet1") 'Set the worksheetname



lRow1 = ws.Cells(Rows.Count, Column1).End(xlUp).Row 'Find the last row in Column A
lRow2 = ws.Cells(Rows.Count, Column2).End(xlUp).Row 'Find the last row in Column B

ws.Range(Cells(2, Column1), Cells(lRow1, Column1)).SpecialCells(xlCellTypeBlanks).Value = "1900-01-01 00:00:00"
ws.Range(Cells(2, Column2), Cells(lRow2, Column2)).SpecialCells(xlCellTypeBlanks).Value = "1900-01-01 00:00:00"

'Turn on following processes to get back to normal state
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic


MsgBox ("Complete") 'Message box to show when code is finished

End Sub

【讨论】:

    猜你喜欢
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 1970-01-01
    相关资源
    最近更新 更多