当公共变量传递给选择语句时,Excel vba 返回编译错误

问题描述 投票:0回答:1

我编写了一个模块,将一些单元格转换为 RGB 值,然后在 With Selection 语句中使用。

当我在模块内进行计算时,选择语句识别了该变量。

如果我使用子程序执行相同的计算,则会出现 .color = HexToRGB(XXX) 的编译错误(即使子程序返回正确的值)。

编译错误:

Byref 参数类型不匹配

Public XXX, YY1, YY2, YY3 As String
public r,c as integer
Sub COLOUR_SEGMENT()
INITILIZE
R = ActiveCell.Row
C = ActiveCell.Column
If Cells(R, C).Value = "" Or _
   Cells(R, C + 1).Value = "" Or _
   Cells(R, C + 3).Value = "" Then
   answ1 = MsgBox("You did not a cell triplet containing a colour.", vbCritical)
   If answ1 = 1 Then
        End
    End If
End If

PICKER

Range(Cells(R, C), Cells(R, C + 2)).Select
With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = HexToRGB(XXX)
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With
End Sub

Sub PICKER()
    XXX = Right(Cells(R, C), 2) & Right(Cells(R, C + 1), 2) & Right(Cells(R, C + 2), 2)
End Sub
excel vba compiler-errors public
1个回答
0
投票

感谢 Brad 指出,当字符串变量列表被声明为公共时,只有列表中的最后一个变量被声明为字符串,其余变量都是变体。 HexToRGB 仅适用于字符串。因为XXX是不变的,所以编译失败

© www.soinside.com 2019 - 2024. All rights reserved.