简体   繁体   English

VBA类型不匹配错误

[英]Type mismatch error with VBA

I have a set of data that is in varying formats such as, B, KB, MB, GB, and TB. 我有一组不同格式的数据,例如B,KB,MB,GB和TB。 I have written code to remove the units and convert to GB. 我已经编写了删除单元并将其转换为GB的代码。 My issue is that The last section 'b to kb' causes a type mismatch error. 我的问题是,最后一个“ b到kb”部分会导致类型不匹配错误。 I cant understand why as I have applied the same code to each conversion? 我无法理解为什么将相同的代码应用于每次转化? Thanks in advance. 提前致谢。

Here is my code: 这是我的代码:

Sub RemoveUnits()
Dim r As Range
Dim x As Long
For Each r In ActiveSheet.UsedRange
w = r.Value

'GB
     If InStr(w, "gb") > 0 Then
        r = Left$(r, Len(r) - 2)
        Debug.Print (r.Value)
    'No conversion required
'TB
    ElseIf InStr(w, "tb") > 0 Then
        r = Left$(r, Len(r) - 2)
        r = r * 1024
        Debug.Print (r.Value)
   'Convert tb to gb
'MB
    ElseIf InStr(w, "mb") > 0 Then
      r = Left$(r, Len(r) - 2)
      r = (1 / 1024) * r
      Debug.Print (r.Value)
'Convert mb to gb
 'KB
    ElseIf InStr(w, "kb") > 0 Then
      r = Left$(r, Len(r) - 2)
      r = (1 / 1048576) * r
      Debug.Print (r.Value)
'B to KB
    ElseIf InStr(w, "b") > 0 Then
      r = Left$(r, Len(r) - 1)
      r = (1 / 1024) * r
      debung.Print (r.Value)




    End If
Next r
End Sub

I see two possible problems. 我看到两个可能的问题。

  1. You have set r as range. 您已将r设置为范围。
  2. You are trying to multiply a numeric value with a string value. 您正在尝试将数字值与字符串值相乘。 Convert the string to a numeric value and then perform the necessary action. 将字符串转换为数值,然后执行必要的操作。

For example 例如

r = Left$(r, Len(r) - 2)
r = r * 1024

Try this 尝试这个

r = Val(Trim(r)) * 1024

Similarly change r = (1 / 1024) * r to r = (1 / 1024) * Val(Trim(r)) and so on... 类似地,将r = (1 / 1024) * r更改为r = (1 / 1024) * Val(Trim(r)) ,依此类推...

Note: I am assuming that r stores a numeric value as string. 注意:我假设r将数字值存储为字符串。

Cause: Try use the common Debug.Print instead of the esoteric debung.Print in your last ElseIf . 原因:尝试使用公共的Debug.Print而不是最后一个ElseIf中的深奥的debung.Print Usually code highlighting is a good friend. 通常,突出显示代码是一个好朋友。 And also, supplemental meta-irony points for the fact that the code that helps you debug causes the exceptions in the first place. 此外,补充性的元讽刺意味在于事实是,可以帮助您调试的代码首先会导致异常。 :-) :-)

Suggestion: Left$ expects a String as first parameter, while you pass an Excel.Range object. 建议:当您传递Excel.Range对象时, Left$期望将String作为第一个参数。 Sometimes is not okay... 有时候不好

Try (and fix this for all your code): 尝试(并针对所有代码修复此问题):

Sub RemoveUnits()
        '... Local declarations'
        Dim w  As String

        For Each r In ActiveSheet.UsedRange
                Let w = CStr(r.Value)

                ' ... The other cases'

                ElseIf InStr(w, "b") > 0 Then
                        Let w = Left$(w, Len(w) - 1)
                        Let r.Value = (1 / 1024) * CDbl(w)
                        Debug.Print r.Value
                End If
        Next r
End Sub

The suggestion part is similar to the answer of Siddharth Rout , with the difference that it uses for calculations the String variable as an intermediate, not the Range one. 建议部分类似于Siddharth Rout的答案,区别在于它用于计算String变量作为中间变量,而不是Range变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM