首页 文章

在Excel VBA中从十六进制转换为十进制时忽略空单元格

提问于
浏览
0

我是VBA的新手并且有一点疑问 . 我试图将特定单元格中的某些值从十六进制转换为十进制,我有一个小难度 . 在那个细胞中有很多空白细胞 . 例如前5行是空白然后我再次有一个十六进制值3个空白行和一个十六进制值 . 由于空白单元格,我无法循环 . 请有人帮忙 . 以下是我写的代码 .

Sub Conversion()

Dim j As Integer
Dim LR As Integer

LR = Range("B" & Rows.Count).End(xlUp).Row

For j = 3 To LR
If Cells(j, 2).value = "" Then Cells(j, 3).value = "#N/A" Else
Cells(j, 3).value = CLng("&H" & Cells(j, 2).value)


Next
End Sub

我在这段代码中遇到Mismatch错误

4 回答

  • 0

    试试这个:

    For j = 3 To LR
    If Cells(j, 2).Value = "" Then
      Cells(j, 3).Value = "#N/A"
    Else
      Cells(j, 3).Value = CLng("&H" & Cells(j, 2).Value)
    End If
    

    为了忽略错误:

    On Error Resume Next
    
  • 0

    你最后忘记了 End If . 我已经组织了你的代码并添加了 End If ,似乎它的工作原理 .

    Sub Conversion()
    
    Dim j As Integer
    Dim LR As Integer
    
    LR = Range("B" & Rows.Count).End(xlUp).Row
    
    MsgBox Range("B" & Rows.Count).End(xlUp).Row
    
    For j = 3 To LR
    If Cells(j, 2).Value = "" Then
        Cells(j, 3).Value = "#N/A"
    Else
        Cells(j, 3).Value = CLng("&H" & Cells(j, 2).Value)
    End If
    
    Next
    End Sub
    
  • 0

    CLng不起作用,因为它给出了错误,因为我阅读并理解你可以使用下面的代码,你可以在你的代码中使用Format而不是CLNG命令

    Sub Conversion()
    
    Dim j As Integer
    Dim LR As Integer
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
    For j = 1 To LR
    If Cells(j, 1).Value = "" Then
        Cells(j, 3).Value = "#N/A"
    Else
        Cells(j, 3).Value = "&H" & Format(Cells(j, 1).Value, "0")
    End If
    Next
    End Sub
    
  • 1

    这是一个使用三元函数的人

    Sub Conversion()
    
        Dim sht As Worksheet
        Set sht = ActiveWorkbook.Sheets("Sheet3")
    
        Dim LR As Range
        Set LR = sht.Range("B1", sht.Range("B" & sht.Rows.Count).End(xlUp))
    
        Dim cel As Range
        For Each cel In LR
    
            cel.Offset(0, 1).Value = IIf(cel.Value = "", "#N/A", CLng("&H" & cel.Value))
    
        Next cel
    End Sub
    

相关问题