Excel VBA - 转换某个子字符串之前的数字
Excel VBA - Convert numbers that come before a certain substring
我有一个包含充满 HTML 内容的单元格的列,其中包括我们需要转换为英寸的以厘米为单位的测量值。
要转换的数字始终在 "cm" 之前,如 <p><strong>Medium - 6.3cm x 7cm</strong></p>
我一直在尝试将整个单元格内容复制到相邻单元格中,并通过在 "cm" 和 space 之间查找来确定要转换的数字。
但是,数字和另一个字符之间并不总是有 space,因为可能存在 <p>8cm x 5.3cm</p>
或 blah-10.5cm x 5cm
等情况。在某些情况下,它出现在单元格的开头,在这种情况下也不会有 space,即 12cm x 8cm - blah blah blah
.
任何人都可以通过将要转换的数字识别为 "cm" 子字符串之前的所有非数字字符来建议任何方法吗?
到目前为止我们已经尝试通过用 space 替换数字之前出现的字符来解决这个问题......但是发现替换所需的可能性太多而且它也搞砸了HTML当然。
我已经复制在下面以供参考。如果有人能帮助我们解决这个问题,我们将不胜感激!
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
找人帮忙解决这个问题,他们提出了这个非常好的解决方案。希望有一天对其他人有用!
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
我有一个包含充满 HTML 内容的单元格的列,其中包括我们需要转换为英寸的以厘米为单位的测量值。
要转换的数字始终在 "cm" 之前,如 <p><strong>Medium - 6.3cm x 7cm</strong></p>
我一直在尝试将整个单元格内容复制到相邻单元格中,并通过在 "cm" 和 space 之间查找来确定要转换的数字。
但是,数字和另一个字符之间并不总是有 space,因为可能存在 <p>8cm x 5.3cm</p>
或 blah-10.5cm x 5cm
等情况。在某些情况下,它出现在单元格的开头,在这种情况下也不会有 space,即 12cm x 8cm - blah blah blah
.
任何人都可以通过将要转换的数字识别为 "cm" 子字符串之前的所有非数字字符来建议任何方法吗?
到目前为止我们已经尝试通过用 space 替换数字之前出现的字符来解决这个问题......但是发现替换所需的可能性太多而且它也搞砸了HTML当然。
我已经复制在下面以供参考。如果有人能帮助我们解决这个问题,我们将不胜感激!
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
找人帮忙解决这个问题,他们提出了这个非常好的解决方案。希望有一天对其他人有用!
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