Excel 上的错误处理错误类型不匹配错误 13

如何解决Excel 上的错误处理错误类型不匹配错误 13

我试图在 VBA 上执行代码,每次他在使用的范围中遇到空白单元格时,都会用“-”填充单元格。我有两列里面有公式(公式结果是“#Value”,因为用户让它为空),当我运行代码时,我得到了一个运行时错误

If pl.Range(Split(Cells(1,coluna).Address,"$")(1) & linha).Value = ""

我尝试使用错误处理程序,但它不起作用。它只是忽略标签并不断给我一个运行时错误屏幕。我该怎么办?

我得到的错误

类型不匹配(错误 13)

感谢您的帮助!

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,"$")(1) & linha).Value = "" Then
            
            'Fill the cell with the string "-"
            pl.Range(Split(Cells(1,"$")(1) & linha).Value = "-"
            
        End If 

proximo:
    Next coluna
Next linha 

End Sub

解决方法

在非空范围内替换

  • 在遍历工作表的单元格时,您必须考虑可能导致“类型不匹配错误”发生的错误值。前两种解决方案说明了一种方法。
  • 第三个(最准确的)解决方案使用 Range.Replace method(效率更高)和 refNonEmpty 'helper' 函数。
  • 最后一个解决方案是使用具有其局限性的 Worksheet.UsedRange property 的单行。
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("*",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("*",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 = 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("*",xlPrevious)
            If lCell Is Nothing Then Exit Function
            Set rg = rg.Resize(lCell.Row - .Row + 1)
            Set refNonEmpty = rg.Resize(,rg.Find("*",_
                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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?