简体   繁体   English

在VBA Excel中为多个单元格使用udf

[英]use a udf in vba excel for multiple cells

a user defined function I use in excel 我在excel中使用的用户定义函数

Function Hex2RGB(hex As String) As String

    Red = Val("&H" & Mid(hex, 1, 2))
    Green = Val("&H" & Mid(hex, 3, 2))
    Blue = Val("&H" & Mid(hex, 5, 2))

  Debug.Print RGB(Red, Green, Blue)
    Hex2RGB = RGB(Red, Green, Blue)

End Function

I have a worksheet with a 10 times 10 matrix for which this function would need to be used for every cell in the range. 我有一个工作表,其中包含10乘10的矩阵,为此范围内的每个单元格都需要使用此函数。 There has to be some way that I don't have to click on each cell and enter =Hex2RGB("value") manually? 必须有某种方式,我不必单击每个单元格并手动输入= Hex2RGB(“ value”)吗? when I write value I am referring to something like OD62A2....which is the colour than to be transformed in excel format 当我写值时,我指的是诸如OD62A2之类的东西。

Use a range parameter instead of value as below. 使用范围参数而不是如下所示的值。 Set the function manually to the first cell (eg left upper cell in the matrix) then copy and paste it across the matrix - the range will change 手动将功能设置到第一个单元格(例如,矩阵的左上单元格),然后将其复制并粘贴到矩阵中-范围将更改

Public Function Hex2RGB(r As Range) As String
    Dim hex As String: r.Value
    Red = Val("&H" & Mid(hex, 1, 2))
    Green = Val("&H" & Mid(hex, 3, 2))
    Blue = Val("&H" & Mid(hex, 5, 2))

  Debug.Print RGB(Red, Green, Blue)
    Hex2RGB = RGB(Red, Green, Blue)

End Function

Step 1: Add the following macro. 步骤1:添加以下宏。 This macro will iterate over the cells in the selected range, and replace them with a formula based on its own value, which I think is what you are trying to do. 该宏将在选定范围内的单元格上进行迭代,并使用基于其自身值的公式替换它们,我认为这是您要尝试执行的操作。

Sub GetHexValues()
    Dim rng as Range
    Dim cl as Range
    Set rng = Range(Selection.Address)
    For each cl in rng.Cells
        cl.Formula = "=Hex2RGB(" & Chr(34) & cl.Value & Chr(34) & ")"
    Next
End Sub

Step 2 Make sure your function is: 步骤2确保您的功能是:

Public Function Hex2RGB(hex) As String
    Red = Val("&H" & Mid(hex, 1, 2))
    Green = Val("&H" & Mid(hex, 3, 2))
    Blue = Val("&H" & Mid(hex, 5, 2))

    Debug.Print RGB(Red, Green, Blue)
    Hex2RGB = RGB(Red, Green, Blue)

End Function

Step 3: Select the matrix range. 步骤3: 选择矩阵范围。 (NB: Normally I recommend not using Select but as a means of user-input it is OK to do that here, I think.) (注意:通常,我建议不要使用“ Select但我认为可以将其用作用户输入的一种方法。)

There is generally speaking, no way to use a UDF worksheet function to manipulate a range of cells in the manner you're describing. 通常来说,无法使用UDF工作表函数以您所描述的方式来操纵一系列单元格。 UDF's can generally not alter many properties and values of cells/ranges except the cell that is calling the function. 除了调用该函数的单元格外,UDF通常不能更改单元格/范围的许多属性和值。 This is to avoid circular reference/etc. 这是为了避免循环引用等。 (there are workarounds to this limitation, but generally accepted rule is to use Subroutines to manipulate the worksheet objects). (有解决此限制的方法,但是通常公认的规则是使用子例程来处理工作表对象)。

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

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