比较VBA中的单元格作为条件格式

我有这个代码工作得很好:

Function sshProblem(rng As Range) As Boolean Dim portStatus As String portStatus = rng.Value Dim deviceType As String deviceType = Cells(Application.Caller.Row, 3).Value Dim sshDevices As Variant sshDevices = Array("linux", "vmw", "docker", "unix") If StrComp(portStatus, "No") = 0 Then sshProblem = Not IsError(Application.Match(deviceType, sshDevices, 0)) End If End Function 

现在代码需要扩展,而不是将值存储在sshDevices数组中,这些值需要驻留在另一个表的列中,所以我试图replace

 sshDevices = Array("linux", "vmw", "docker", "unix") 

 sshDevices = Worksheets("Config sheet").Range("I2:I11").Value 

此时条件格式停止工作。 我怎样才能从单元格区域获取值并将其插入到一个variables中进行比较?

我不是100%确定你想用你的代码实现什么,而是回答你的问题

我怎样才能从单元格区域获取值并将其插入到一个variables中进行比较?

你可以使用For Each循环,类似于:

 dim myCell as range, myRange as range set myRange = Worksheets("Config sheet").Range("I2:I11") For Each myCell in myRange sshDevice = myCell.Value 'do stuff you need with sshDevice Next myCell 

如果你想保留sshDevices数组,你可以做到这一点,并使用For Each循环分别添加每个项目。

 dim counter as int counter = 0 For Each myCell in myRange sshDevice(counter) = myCell.Value counter = counter + 1 next myCell 

使用

 sshDevices = Application.Transpose(Worksheets("Config sheet").Range("I2:I11").Value) 

只是被告知这样你将有一个基于1的数组,无论任何Option Base ,但是影响返回的数组formsArray()函数