将特定string插入到VBA中的空单元格中

目标:使用string(“/ N”)填充Worksheet中的所有空单元格。

问题:在代码运行期间,由于某种原因,我一直得到一个types不匹配。 该错误出现在行上:

If Worksheets("Comp").Cells(lRow, lColumn) = "" Then 

问题:我做错了什么,有没有办法解决?

Obs1:我的工作表包含5千行和40列。 前3行是标题(标识符),前3列总是有数据。

Obs2:工作表具有不同types的数据(数字,string,date)

码:

 Sub n_slasher() Dim i As Integer, j As Integer Dim LastRow As Long, LastColumn As Long Dim lRow As Long, lColumn As Long Dim ws As Worksheet Dim w As Workbook Set w = ThisWorkbook Application.ScreenUpdating = False Application.EnableEvents = False Application.Calculation = xlCalculationManual 'find date limits LastRow = Worksheets("Comp").Cells(Rows.Count, "A").End(xlUp).Row LastColumn = Worksheets("Comp").Cells(1, Columns.Count).End(xlToLeft).Column For lColumn = 4 To LastColumn For lRow = 3 To LastRow If Worksheets("Comp").Cells(lRow, lColumn) = "" Then Worksheets("Comp").Cells(lRow, lColumn) = "\N" End If Next lRow Next lColumn Application.ScreenUpdating = True Application.EnableEvents = True Application.Calculation = xlCalculationAutomatic End Sub 

这是一个使用数组的版本

 Sub n_slasher() Dim LastRow As Long, LastColumn As Long Dim lRow As Long, lColumn As Long Dim ws As Worksheet Dim w As Workbook Dim data Set w = ThisWorkbook With Application .ScreenUpdating = False .EnableEvents = False .Calculation = xlCalculationManual End With Set ws = Worksheets("Comp") 'find date limits With ws LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column data = .Range("D3", .Cells(LastRow, LastColumn)).Value2 For lRow = 1 To UBound(data) For lColumn = 1 To UBound(data, 2) If Not IsError(data(lRow, lColumn)) Then If Len(data(lRow, lColumn)) = 0 Then data(lRow, lColumn) = "\N" Next lColumn Next lRow .Range("D3", .Cells(LastRow, LastColumn)).Value2 = data End With With Application .EnableEvents = True .Calculation = xlCalculationAutomatic .ScreenUpdating = True End With End Sub