当代码运行且单元格为空或无值时,VBA类型不匹配

当代码运行且单元格为空或无值时,VBA类型不匹配,vba,excel,Vba,Excel,我有以下的IF声明: If Cells(i, 4).NumberFormat <> "0.0%" Or IsEmpty(Cells(i, 4)) Or Cells(i, 4).Value2 = "" Then Cells(i, 4).NumberFormat = "0.0%" Cells(i, 4).Value = Cells(i, 4).Value / 100 'Else 'Cells(i, 4).Value = Cells(i, 4).Value

我有以下的IF声明:

If Cells(i, 4).NumberFormat <> "0.0%" Or IsEmpty(Cells(i, 4)) Or Cells(i, 4).Value2 = "" Then
    Cells(i, 4).NumberFormat = "0.0%"
    Cells(i, 4).Value = Cells(i, 4).Value / 100
'Else
    'Cells(i, 4).Value = Cells(i, 4).Value
    'Cells(i, 4).Value = Cells(i, 4).Value
End If
如果单元格(i,4).NumberFormat“0.0%”或IsEmpty(单元格(i,4))或单元格(i,4).Value2=“”,则
单元格(i,4)。NumberFormat=“0.0%”
单元格(i,4)。数值=单元格(i,4)。数值/100
”“否则呢
'单元格(i,4).值=单元格(i,4).值
'单元格(i,4).值=单元格(i,4).值
如果结束
当我启动代码时,它会为每个包含数据的单元格运行,但是

如果单元格为空,它将不运行,并显示一个错误,显示“类型不匹配”


以下是全部代码:

Public Sub SortMyData()

Dim i As Integer
Dim N_Values As Integer

N_Values = Cells(Rows.Count, 2).End(xlUp).Row

For i = 6 To N_Values
    'Cells(i, 3).NumberFormat = "0"

    If Cells(i, 2).NumberFormat <> "0.0%" Then
        Cells(i, 2).NumberFormat = "0.0%"
        Cells(i, 2).Value = Cells(i, 2).Value / 100
    'Else
        'Cells(i, 2).Value = Cells(i, 2).Value
        'Cells(i, 3).Value = Cells(i, 3).Value
    End If

        If (Cells(i, 3).Value) > 1000000 Then
           Cells(i, 3).Value = Cells(i, 3).Value / 1000000 & "Mb"
           Cells(i, 3).HorizontalAlignment = xlRight

            ElseIf (Cells(i, 3).Value) > 1000 Then
                Cells(i, 3).Value = Cells(i, 3).Value / 1000 & "kb"
                Cells(i, 3).HorizontalAlignment = xlRight

            ElseIf Cells(i, 3).Value = Null Or Cells(i, 3).Text = Null Or Cells(i, 3).Value = "" Or Cells(i, 3).Text = "" Then
                Cells(i, 3).Value = 0
                Cells(i, 3).HorizontalAlignment = xlRight
        End If

            If Cells(i, 4).NumberFormat <> "0.0%" Or IsEmpty(Cells(i, 4)) Or Cells(i, 4).Value2 = "" Then
                Cells(i, 4).NumberFormat = "0.0%"
                Cells(i, 4).Value = Cells(i, 4).Value / 100
            'Else
                'Cells(i, 4).Value = Cells(i, 4).Value
                'Cells(i, 4).Value = Cells(i, 4).Value
            End If
Next i

End Sub
公共子排序码()
作为整数的Dim i
将N_值设置为整数
N_值=单元格(Rows.Count,2).End(xlUp).Row
对于i=6到N_值
'单元格(i,3).NumberFormat=“0”
如果单元格(i,2).NumberFormat“0.0%”,则
单元格(i,2)。NumberFormat=“0.0%”
单元格(i,2)。数值=单元格(i,2)。数值/100
”“否则呢
'单元格(i,2).值=单元格(i,2).值
'单元格(i,3).值=单元格(i,3).值
如果结束
如果(单元格(i,3).值)>1000000,则
单元格(i,3).值=单元格(i,3).值/1000000&“Mb”
单元格(i,3)。水平对齐=xlRight
ElseIf(单元格(i,3).值)>1000
单元格(i,3).值=单元格(i,3).值/1000&“kb”
单元格(i,3)。水平对齐=xlRight
ElseIf Cells(i,3)。Value=Null或Cells(i,3)。Text=Null或Cells(i,3)。Value=”“或Cells(i,3)。Text=”“然后
单元格(i,3)。值=0
单元格(i,3)。水平对齐=xlRight
如果结束
如果单元格(i,4).NumberFormat“0.0%”或IsEmpty(单元格(i,4))或单元格(i,4).Value2=“”,则
单元格(i,4)。NumberFormat=“0.0%”
单元格(i,4)。数值=单元格(i,4)。数值/100
”“否则呢
'单元格(i,4).值=单元格(i,4).值
'单元格(i,4).值=单元格(i,4).值
如果结束
接下来我
端接头

为了更好的可读性,我用添加了一些
,并在分割之前测试了值:

Public Sub SortMyData()
Dim wS As Worksheet
Dim i As Long
Dim N_Values As Long

Set wS = ThisWorkbook.Sheets("Sheet1")

N_Values = wS.Cells(wS.Rows.Count, 2).End(xlUp).Row


With wS
    For i = 6 To N_Values
        With .Cells(i, 2)
            If .NumberFormat <> "0.0%" Then
                .NumberFormat = "0.0%"
                If .Value2 <> vbNullString And IsNumeric(.Value2) Then .Value = .Value / 100
            Else
            End If
        End With

        With .Cells(i, 3)
            .HorizontalAlignment = xlRight
            Select Case .Value
                Case Is > 1000000
                    .Value = .Value / 1000000 & "Mb"
                Case Is > 1000
                    .Value = .Value / 1000 & "kb"
                Case Is > 1
                    .Value = .Value & "b"
                Case Else
                    .Value = 0
            End Select
'            If (.Value) > 1000000 Then
'               .Value = .Value / 1000000 & "Mb"
'            ElseIf (.Value) > 1000 Then
'                .Value = .Value / 1000 & "kb"
'            ElseIf .Value = Null Or .Text = Null Or .Value = "" Or .Text = "" Then
'                .Value = 0
'            End If
        End With

        With .Cells(i, 4)
            If .NumberFormat <> "0.0%" Then
                .NumberFormat = "0.0%"
                If .Value2 <> vbNullString And IsNumeric(.Value2) Then .Value = .Value / 100
            Else
            End If
        End With

    Next i
End With

End Sub
公共子排序码()
将wS设置为工作表
我想我会坚持多久
将N_值变长
设置wS=ThisWorkbook.Sheets(“Sheet1”)
N_值=wS.Cells(wS.Rows.Count,2).End(xlUp).Row
与wS
对于i=6到N_值
带.单元(i,2)
如果.NumberFormat“0.0%”,则
.NumberFormat=“0.0%”
如果.Value2 vbNullString和IsNumeric(.Value2),则.Value=.Value/100
其他的
如果结束
以
带.单元(i,3)
.HorizontalAlignment=xlRight
选择Case.Value
病例>1000000例
.Value=.Value/1000000&“Mb”
案例>1000
.Value=.Value/1000&“kb”
病例>1
.Value=.Value&“b”
其他情况
.Value=0
结束选择
'如果(.Value)>1000000,则
'.Value=.Value/1000000&“Mb”
'ElseIf(.Value)>1000则
'.Value=.Value/1000&“kb”
'ElseIf.Value=Null或.Text=Null或.Value=”“或.Text=”“然后
“。值=0”
"完"
以
带.单元(i,4)
如果.NumberFormat“0.0%”,则
.NumberFormat=“0.0%”
如果.Value2 vbNullString和IsNumeric(.Value2),则.Value=.Value/100
其他的
如果结束
以
接下来我
以
端接头

单元格中可能有隐藏符号。您应该运行
Isnumeric(…)
首先检查单元格(空单元格返回true)。唯一可能出现错误的点是
单元格(i,4)。Value/100
(至少在我看来)…请尽可能指定错误发生在哪一行,知道错误在哪里非常有帮助,不必猜测!;)哇!你只是重新编写了我所有的代码,使它更简单!非常感谢。我能问一下为什么With语句中只有1个EndIF而没有2个EndIF吗?我是VBA新手,我需要学习@劳尔冈萨雷斯:不客气
End If
的数量仍然相同,我只是添加了两行
If-Then
(没有
End If
,因为它只有一行)来测试潜水前的值!VBA中最广泛的改进参考:;)谢谢你的朋友,上周你也帮了我!我对VBA很陌生。也。。。你认为在将来学习Python是否值得,因为VBA可能很快就会消亡?VBA应该有一天会消亡,但很容易开始学习。当然,学习Javascript、Python、C#、…)