I'm trying to execute a code on VBA that fill a cell with "-" every time he encounters a blank cell in the used Range. I have two columns with formulas inside (And the formula result is "#Value" because the user let it blank) and when I run the code I get a run-time error on the line:
If pl.Range(Split(Cells(1, coluna).Address, "$")(1) & linha).Value = ""
I tried to use Error Handler but it doesn't work. It simply ignores the tag and keeps giving me a run-time error screen. What should I do?
The error I get:
Type mismatch (Error 13)
Thanks for the help!
Public Sub PreencheCaracterizacao()
Dim linha As Long
Dim coluna As Long
Set pl = ThisWorkbook.Worksheets("BD - Caracterização")
'Two loops: linha (line) and coluna (column)
For linha = 2 To pl.Cells.Find("*", pl.Cells(1, 1), xlFormulas, xlPart, xlByRows, lPrevious).Row
For coluna = 1 To pl.UsedRange.Columns.Count
On Error GoTo proximo
'Test if the cell is blank
If pl.Range(Split(Cells(1, coluna).Address, "$")(1) & linha).Value = "" Then
'Fill the cell with the string "-"
pl.Range(Split(Cells(1, coluna).Address, "$")(1) & linha).Value = "-"
End If
proximo:
Next coluna
Next linha
End Sub
Range.Replace method
(which is much more efficient) and the refNonEmpty
'helper' function.Worksheet.UsedRange property
which has its limitations.Option Explicit
Sub replaceBlanksSlow()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.Worksheets("BD - Caracterizaçao")
Dim lRow As Long
lRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
Dim lCol As Long
lCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column
Dim cValue As Variant
Dim linha As Long
Dim coluna As Long
For linha = 2 To lRow
For coluna = 1 To lCol
cValue = ws.Cells(linha, coluna).Value
If Not IsError(cValue) Then
If cValue = "" Then
ws.Cells(linha, coluna).Value = "-"
End If
End If
Next coluna
Next linha
End Sub
Sub replaceBlanksSlowConstants()
Const wsName As String = "BD - Caracterizaçao"
Const fRow As Long = 2
Const fCol As Long = 1
Const rString As String = "-"
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
Dim lRow As Long
lRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
Dim lCol As Long
lCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column
Dim cValue As Variant
Dim linha As Long
Dim coluna As Long
For linha = fRow To lRow
For coluna = fCol To lCol
cValue = ws.Cells(linha, coluna).Value
If Not IsError(cValue) Then
If cValue = "" Then
ws.Cells(linha, coluna).Value = rString
End If
End If
Next coluna
Next linha
End Sub
' This one uses the 'refNonEmpty' function to create a reference to the range.
Sub replaceBlanks()
Const wsName As String = "BD - Caracterizaçao"
Const First As String = "A2"
Const fCol As Long = 1
Const rString As String = "-"
Dim wb As Workbook: Set wb = ThisWorkbook
Dim fCell As Range: Set fCell = wb.Worksheets(wsName).Range(First)
Dim rg As Range: Set rg = refNonEmpty(fCell)
If Not rg Is Nothing Then
rg.Replace "", rString
End If
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose: Creates a reference to the range from a given cell (range)
' to the last non-empty cell in its worksheet.
' Remarks: It may fail if the worksheet is filtered.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function refNonEmpty( _
FirstCell As Range) _
As Range
If Not FirstCell Is Nothing Then
Dim rg As Range
With FirstCell.Cells(1)
Set rg = .Resize(.Worksheet.Rows.Count - .Row + 1, _
.Worksheet.Columns.Count - .Column + 1)
Dim lCell As Range
Set lCell = rg.Find("*", , xlFormulas, , xlByRows, xlPrevious)
If lCell Is Nothing Then Exit Function
Set rg = rg.Resize(lCell.Row - .Row + 1)
Set refNonEmpty = rg.Resize(, rg.Find("*", , xlFormulas, , _
xlByColumns, xlPrevious).Column - .Column + 1)
End With
End If
End Function
' Note that here you cannot control the first cell.
Sub replaceBlanksUsedRange()
ThisWorkbook.Worksheets("BD - Caracterizaçao").UsedRange.Replace "", "-"
End Sub