我有一列包含充满HTML内容的单元格,其中包括需要转换为英寸的厘米单位。
要转换的数字始终像<p><strong>Medium - 6.3cm x 7cm</strong></p>
之前的“ cm”一样
我一直试图将整个单元格内容复制到相邻的单元格中,并通过在“ cm”和空格之间寻找来确定要转换的数字。
但是,数字和另一个字符之间并不总是有空格,因为可能会出现诸如<p>8cm x 5.3cm</p>
或blah-10.5cm x 5cm
之类的情况。在某些情况下,它是在单元格的开头出现的,在这种情况下,也将没有空格,即12cm x 8cm - blah blah blah
。
有人可以通过标识要转换为“ cm”子字符串前面的所有非数字字符的数字来建议这样做吗?
到目前为止,我们尝试通过用空格替换数字前出现的字符来解决此问题,但是发现替换所需的可能性太多,并且当然还会弄乱HTML。< / p>
在任何情况下,我都将其复制到下面以供参考。如果有人可以帮助我们解决这个问题,将不胜感激!
Sub ChangeCM2IN()
Dim X As Long, V As Double, Cell As Range, CM() As String
Application.ScreenUpdating = False
For Each Cell In Range("A1", Cells(Rows.Count, "A").End(xlUp))
CM = Split(Replace(Replace(" " & Cell.Value, "x", "x ", , , vbTextCompare), ">", " "), "cm", , vbTextCompare)
For X = 0 To UBound(CM) - 1
V = Format(Mid(CM(X), InStrRev(CM(X), " ")) * 0.393700787401575, "0.0")
CM(X) = Left(CM(X), InStrRev(CM(X), " ")) & V
Next
Cell.Offset(, 1).Value = Join(CM, "in")
Next
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:0)
找人协助解决这个问题,他们提出了一个非常好的解决方案。希望有一天对其他人有用!
Sub ChangeCM2IN()
Dim X As Long, Z As Long, FirstDigit As Long, V As Double, Cell As Range, CM() As String
Application.ScreenUpdating = False
For Each Cell In Range("A1", Cells(Rows.Count, "A").End(xlUp))
CM = Split(" " & Cell.Value, "cm", , vbTextCompare)
For X = 0 To UBound(CM) - 1
For Z = Len(CM(X)) To 1 Step -1
If Mid(CM(X), Z, 1) Like "[!0-9.]" Then
FirstDigit = Z + 1
V = Format(Mid(CM(X), FirstDigit) * 0.393700787401575, "0.0")
Exit For
End If
Next
CM(X) = Left(CM(X), FirstDigit - 1) & V
Next
Cell.Offset(, 1).Value = Trim(Join(CM, "in"))
Next
Application.ScreenUpdating = True
End Sub