簡體   English   中英

比較VBA中的單元格以進行條件格式化

[英]Compare cells in VBA for conditional formatting

我有這么多代碼可以正常工作:

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數組中存儲值,這些值需要駐留在另一個工作表的列中,所以我試圖替換

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

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

此時條件格式化停止工作。 我如何從單元格范圍中獲取值並將它們插入變量中進行比較?

我不是100%肯定你想用你的代碼實現的,而是回答你的問題

我如何從單元格范圍中獲取值並將它們插入變量中進行比較?

你可以使用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如何影響返回的數組形式的Array()函數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM