如何在 Application.Left 函数(需要范围或字符串)中使用拆分函数(变体)的结果?
Posted
技术标签:
【中文标题】如何在 Application.Left 函数(需要范围或字符串)中使用拆分函数(变体)的结果?【英文标题】:How can I use the result of a Split function (a Variant) in a Application.Left function (which needs a Range or String)? 【发布时间】:2021-10-15 12:20:42 【问题描述】:我想在包含多个用逗号分隔的十六进制代码的单元格上使用此 UDF。我目前面临的主要问题是 Split 函数创建的 String 数组不能在第二个 UDF 中的 Application.Left 函数中使用。像 Cstr 这样的类型转换函数似乎只适用于数字或字符串值。我该如何解决这个问题?
Function Gen_Col(Full_Hex As Range) As String
Dim Seperate1() As Variant
Dim code As Variant
Dim Dcode As Range
Dim Dec_Range As Range
Dim Color As Range
Set Dec_Range = Range("Table3[DEC]")
Seperate1 = Split(Full_Hex, ",")
Gen_Col = ""
For Each code In Seperate1
If Gen_Col <> "" Then
Set Dcode = Dec_Range.Find(DecCode(code), LookIn:=xlValues)
If Not Dcode Is Nothing Then
Color = Dcode.Offset(0, 3)
Gen_Col = Gen_Col & ", " & Color.Value
End If
Else
If Not Dcode Is Nothing Then
Color = Dcode.Offset(0, 3)
Gen_Col = Color.Value
End If
End If
Next code
End Sub
Function DecCode(code As Variant) As String
Dim L1
Dim H1
Dim MR1
Dim L2
Dim H2
Dim MR2
Dim L3
Dim H3
Dim MR13
Dim M
Dim I
'I'm translating the hexcode to the nearest decimal code from a list (hence the MRound with 51), and then using the color name of the respective decimal code selected from a pre-made list of names.
L1 = Application.Left(code, 2)
H1 = Application.Hex2Dec(L1)
MR1 = Application.MRound(H1, 51)
L2 = Application.Mid(code, 3, 2)
H2 = Application.Hex2Dec(L2)
MR2 = Application.MRound(H2, 51)
L3 = Application.Right(code, 2)
H3 = Application.Hex2Dec(L3)
MR3 = Application.MRound(H3, 51)
M = Application.Match(MR1 & MR2 & MR3, Range("Table3[DEC]"), 0)
'"Table3[DEC]" contains a list of decimal codes.
I = Application.Index(Range("Table3[Name]"), M, 1)
'"Table3[Name]" is a list of names for each color based on the decimal code from [DEC].
DecCode = I
End Sub
【问题讨论】:
Split
返回一个字符串数组。更改定义Seperate1() As String
和code As String
(在这两个地方)。这能解决您的问题吗?
另请注意,应该使用LBound
将数组循环到Ubound
,而不是For Each
循环:Dim i As Long
、For i = LBound(Seperate1) to UBound(Seperate1)
、code = Seperate1(i)
。
有了这些建议 + 一些额外的修复,它现在可以工作了!
【参考方案1】:
感谢@FunThomas、@BigBen 和一些额外的修复,让它工作。
需要将Separate1
和code
更改为String,停止将Application.
用于Left、Mid 和Right 函数,并更改大部分For Each
部分。
Function Gen_Col(Full_Hex As Range) As String
Dim Seperate1() As String
Dim code As String
Dim Dcode As String
Dim Dec_Range As Range
Dim Color As Range
Dim i As Long
Seperate1 = Split(Full_Hex, ",")
Gen_Col = ""
For i = LBound(Seperate1) To UBound(Seperate1)
code = Seperate1(i)
If Gen_Col <> "" Then
Dcode = DecCode(code)
Gen_Col = Gen_Col & ", " & Dcode
Else
Dcode = DecCode(code)
Gen_Col = Dcode
End If
Next i
End Function
Function DecCode(code As String) As String
Dim L1
Dim H1
Dim MR1
Dim L2
Dim H2
Dim MR2
Dim L3
Dim H3
Dim MR13
Dim M
Dim i
L1 = Left(code, 2)
H1 = Application.Hex2Dec(L1)
MR1 = Application.MRound(H1, 51)
L2 = Mid(code, 3, 2)
H2 = Application.Hex2Dec(L2)
MR2 = Application.MRound(H2, 51)
L3 = Right(code, 2)
H3 = Application.Hex2Dec(L3)
MR3 = Application.MRound(H3, 51)
M = Application.Match(MR1 & MR2 & MR3, Range("Table3[DEC]"), 0)
i = Application.Index(Range("Table3[Name]"), M, 1)
DecCode = i
End Function
【讨论】:
以上是关于如何在 Application.Left 函数(需要范围或字符串)中使用拆分函数(变体)的结果?的主要内容,如果未能解决你的问题,请参考以下文章