这是一个相当简单但强大而智能的宏,可将微克规格化为毫克:
'============================================================================================
' Module : <any standard module>
' Version : 0.1.0
' Part : 1 of 1
' References : N/A
' Source : https://superuser.com/a/1333314/763880
'============================================================================================
Option Explicit
Public Sub NormaliseUnits()
Dim ¡ As Long
Dim rngTarget As Range
For Each rngTarget In Selection.Areas
'Minimise the number of cells to be processed
Set rngTarget = Intersect(rngTarget, rngTarget.Parent.UsedRange)
If rngTarget Is Nothing Then Exit For 'Nothing to do as the mimimised Area doesn't contain any data
' Expand the minimised target to include the previous column:
If rngTarget.Column > 1 Then
Set rngTarget = rngTarget.Offset(ColumnOffset:=-1).Resize(ColumnSize:=rngTarget.Columns.Count + 1)
End If
' Expand the minimised target to include the next column:
If rngTarget.Column + rngTarget.Columns.Count - 1 < Columns.Count Then
Set rngTarget = rngTarget.Resize(ColumnSize:=rngTarget.Columns.Count + 1)
End If
' Loop through all cells (skipping the first column) looking for a "ug" to fix
Dim rngRow As Range
For Each rngRow In rngTarget.Rows
For ¡ = 2 To rngRow.Columns.Count
If rngRow.Cells(¡) = "ug" _
And rngRow.Cells(¡ - 1) <> vbNullString _
Then
Dim strValue As String: strValue = CStr(rngRow.Cells(¡ - 1).Value2)
Dim strLessThan As String: strLessThan = vbNullString
If InStr("<>", Left$(strValue, 1)) Then
strLessThan = Left$(strValue, 1)
strValue = Mid$(strValue, 2)
End If
If IsNumeric(strValue) Then
rngRow.Cells(¡ - 1).Value2 = strLessThan & CStr(CDbl(strValue) / 1000)
rngRow.Cells(¡) = "mg"
End If
End If
Next ¡
Next rngRow
Next rngTarget
End Sub
它实际上非常聪明,您可以选择任何内容,整行,整列,单个单元格,甚至是不连续的范围,它将查找并标准化所有适当的值/单位。
笔记:
- 值以a开头
<
要么 >
正确归一化
- 如果值为空或不是数字,则它和单位保持不变