title: Type mismatch error with VBA
body: <p>I have a set of data that is in varying formats such as, B, KB, MB, GB, and TB. I have written code to remove the units and convert to GB. My issue is that The last section 'b to kb' causes a type mismatch error. I cant understand why as I have applied the same code to each conversion? Thanks in advance.</p>
  
  <p>Here is my code:</p>
  
  <pre><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") &gt; 0 Then
          r = Left$(r, Len(r) - 2)
          Debug.Print (r.Value)
      'No conversion required
  'TB
      ElseIf InStr(w, "tb") &gt; 0 Then
          r = Left$(r, Len(r) - 2)
          r = r * 1024
          Debug.Print (r.Value)
     'Convert tb to gb
  'MB
      ElseIf InStr(w, "mb") &gt; 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") &gt; 0 Then
        r = Left$(r, Len(r) - 2)
        r = (1 / 1048576) * r
        Debug.Print (r.Value)
  'B to KB
      ElseIf InStr(w, "b") &gt; 0 Then
        r = Left$(r, Len(r) - 1)
        r = (1 / 1024) * r
        debung.Print (r.Value)
  
  
  
  
      End If
  Next r
  End Sub
  </code></pre>