简体   繁体   English

使用VBA更改Excel单元格中文本字体的一部分

[英]Change Part of text font in Excel cell using vba

Hi I am trying to create a function to calculate milliohms (mOhms) my function is 嗨,我正在尝试创建一个计算毫欧(mOhms)的函数,我的函数是

Function mOhms(Current, Voltage)
mOhms = Format((Voltage / Current) * 1000, "00.00 m") & Chr(87)
End Function

with results being 结果是

40.00 mW (if cell values are 24 and 1 respectivly) 40.00 mW(如果单元格值分别为24和1)

How do i get the W as (Ω) ohms symbol if i change the cell font style to Symbol m changes to micro (μ) symbol 如果将单元字体样式更改为Symbol,将符号m更改为micro(μ),如何获得W as(Ω)欧姆符号

i have tried paying with 我曾尝试用

With ActiveCell.Characters(Start:=Len(ActiveCell) - 1, Length:=1).Font
.FontStyle = "Symbol"
End With

Which results in "Circular reference error"s 这导致“循环参考误差”

Need some help to resolve this 需要一些帮助来解决这个问题

Try using Unicode in place of the Chr(87) 尝试使用Unicode代替Chr(87)

Function mOhms(Current, Voltage)
mOhms = Format((Voltage / Current) * 1000, "00.00 m") & ChrW(&H2126)
End Function 

should you want to stick with Characters object you have to: 您是否要坚持使用Characters对象,您必须:

  • use Name property, instead of FontStyle one 使用Name属性,而不是FontStyle之一

  • set its Start parameter to the last character of the range text, instead of the second to last one 将其Start参数设置为范围文本的最后一个字符,而不是倒数第二个

so you may want to code like follows: 因此,您可能需要编写如下代码:

Sub main()
    With Range("G1") '<--| change this to any valid Range reference
        .Value = mOhms(24, 1) '<--| set the referenced range value
        FormatOhm .Cells '<--| format the referenced range value last character
    End With
End Sub

Function mOhms(Current, Voltage)
    mOhms = Format((Voltage / Current) * 1000, "00.00 m") & Chr(87)
End Function

Sub FormatOhm(rng As Range)
    With rng
        .Characters(Start:=Len(.Value), Length:=1).Font.name = "Symbol"
    End With
End Sub

a possible enhancement of which could be the handling of "W" character actual position in the string, should it not always be the last character 可能的增强可能是字符串中“ W”字符实际位置的处理,如果它不一定总是最后一个字符

then you could add the following function: 那么您可以添加以下功能:

Function GetCharacter(rng As Range, char As String) As Long
    Dim i As Long

    With rng
        For i = 1 To .Characters.Count
            If .Characters(i, 1).Text = char Then
                GetCharacter = i
                Exit For
            End If
        Next i
    End With
End Function

that returns a Long with the passed character position inside the passed range value or 0 if no match occurred 返回带有传递的字符位置在传递的范围值内的Long或如果没有匹配发生则返回0

in this case you'd have to slightly change FormatOhm() function to have it handle the actual character position: 在这种情况下,您必须稍微更改FormatOhm()函数以使其处理实际的字符位置:

Sub FormatOhm(rng As Range, iChar As Long)
    If iChar = 0 Then Exit Sub '<--| exit if no character matching occurred
    With rng
        .Characters(Start:=iChar, Length:=1).Font.name = "Symbol"
    End With
End Sub

and your "main" code would then get to: 然后您的“主要”代码将到达:

Sub main()
    With Range("G1") '<--| change this to any valid Range reference
        .Value = mOhms(24, 1) '<--| set the referenced range value
        FormatOhm .Cells, GetCharacter(.Cells, "W") '<--| format the referenced range value character corresponding to "W", if any
    End With
End Sub

of course what above could be further both improved and made more robust, for instance handling char parameter length in GetCharacter() and correspondingly in FormatOhm() 当然,可以对上述内容进行进一步的改进和增强,例如在GetCharacter()和相应的FormatOhm()处理char参数长度

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

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