简体   繁体   English

比较VBA中的单元格以进行条件格式化

[英]Compare cells in VBA for conditional formatting

I have this chunk of code that works just fine: 我有这么多代码可以正常工作:

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

Now the code needs to scale, and instead of storing values inside the sshDevices array, these values need to reside in a column in another sheet, so I tried to replace 现在代码需要扩展,而不是在sshDevices数组中存储值,这些值需要驻留在另一个工作表的列中,所以我试图替换

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

with

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

at that point the conditional formatting stopped working. 此时条件格式化停止工作。 How may I pick up the values from a cell range and insert them into a variable for comparison? 我如何从单元格范围中获取值并将它们插入变量中进行比较?

I'm not 100% sure with what you are trying to achieve with your code, but to answer your question 我不是100%肯定你想用你的代码实现的,而是回答你的问题

How may I pick up the values from a cell range and insert them into a variable for comparison? 我如何从单元格范围中获取值并将它们插入变量中进行比较?

you can use a For Each loop, something akin to: 你可以使用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

If you want to keep the sshDevices array, you can do that, and use the For Each loop to add each item individually. 如果要保留sshDevices数组,可以执行此操作,并使用For Each循环单独添加每个项目。

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

use 采用

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

just be informed that this way you'll have a 1-based array regardless of any Option Base which however affects the returned array form Array() function 只是被告知这样你将拥有一个基于1的数组,无论任何Option Base如何影响返回的数组形式的Array()函数

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

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