我有一個超過 5 列的 excel 檔案,其中單元格值為 TRUE 和 FALSE。需要在列之間進行比較,如果每列中的所有單元格值都為 TRUE,則在第 6 列單元格中輸入“無更改”。
| 1 || 2 || 3 || 4 || 5 || 6 |
|:---- ||:---- ||:---- ||:---- ||:---- ||:---- |
| TRUE ||TRUE ||TRUE ||TRUE ||TRUE ||NO CHange |
如果這 5 列中的任何單元格具有值 FALSE 需要知道哪一列(第 2 或第 3 或第 5)
| 1 || 2 || 3 || 4 || 5 || 6 |
|:---- ||:---- ||:---- ||:---- ||:---- ||:------------- |
|TRUE ||FALSE ||TRUE ||TRUE ||TRUE ||Change in 2nd |
或者
| 1 || 2 || 3 || 4 || 5 || 6 |
|:---- ||:---- ||:---- ||:---- ||:---- ||:--------------- |
|TRUE ||FALSE ||TRUE ||FALSE ||TRUE ||Change in 2 and 4 |
這可能嗎?
不確定這是正確的!
Sub Checking()
Dim i as Integer
For i=2 to $lastrow
If (cell(i,1).value = cell(i,2).value = cell(i,3).value = cell(i,4).value = cell(i,5).value = "TRUE") Then
cell(i,"6").value = "No Change"
Else
If (cell(i,1).value = "FALSE") Then
cell(i,6).value = "1 Changed"
End If
If (cell(i,2).value = "FALSE") Then
cell(i,6).value = "2 Changed"
End If
If (cell(i,3).value = "FALSE") Then
cell(i,6).value = "3 changed"
End If
If (cell(i,4).value = "FALSE") Then
cell(i,6).value = "4 Changed"
End If
If (cell(i,5).value = "FALSE") Then
cell(i,6).value = "5 Changed"
End If
Next Cell
End Sub
uj5u.com熱心網友回復:
處理陣列中的值比 1 乘 1 訪問單元格更快
您應該將用于存盤行號的任何變數宣告為
Long. 作業表中的最大行數遠高于Integer可以容納的資料型別,并且可能會導致溢位錯誤。通常,您現在也沒有理由使用Integer資料型別,因此只需宣告為Long.宣告所有變數
Option Explicit并在模塊頂部插入以幫助您執行此操作是一種很好的做法。
由于各種原因,您的代碼甚至無法編譯,因此以下代碼不會從您的代碼中修改:
Option Explicit
Sub Checking()
Const firstRow As Long = 2
Const firstColumn As Long = 1
Const numToCompare As Long = 5
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1") 'Change name accordingly
'== Find the last row
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, firstColumn).End(xlUp).Row
'== Get the input values into an array to process faster vs accessing the cells 1 by 1
Dim inputArr As Variant
inputArr = ws.Range(ws.Cells(firstRow, firstColumn), ws.Cells(lastRow, firstColumn)).Resize(, numToCompare).Value
Dim outputArr() As String
ReDim outputArr(1 To UBound(inputArr, 1), 1 To 1) As String
Dim i As Long
Dim falseDict As Object
'== Loop through the array row by row..
For i = 1 To UBound(inputArr, 1)
Set falseDict = CreateObject("Scripting.Dictionary")
'== For each row, loop through each column to look for False value, add the column number to the dictionary if found
Dim n As Long
For n = 1 To UBound(inputArr, 2)
If inputArr(i, n) = False Then falseDict(firstColumn n - 1) = 1
Next n
'Determine the result string
Dim output As String
Select Case falseDict.Count
Case 0: output = "No Change"
Case Else
Dim dictKeys As Variant
dictKeys = falseDict.Keys
output = "Change in " & Join(dictKeys, ", ")
End Select
outputArr(i, 1) = output
Next i
Set falseDict = Nothing
'Output 1 column after the last column to compare
Dim outputColumn As Long
outputColumn = firstColumn numToCompare
ws.Cells(firstRow, outputColumn).Resize(UBound(outputArr, 1)).Value = outputArr
End Sub
uj5u.com熱心網友回復:
通過評估替代
假設在小區范圍的單元值A2:E2,我展示的方法如何能夠使用的結果公式評估 (獲得列編號的陣列明確地與數值細胞False)和加入這些元素到想要輸出字串。
該邏輯式后面"=Not(A2:E2)*Column(A2:E2)"可被描述為如下:
A2:E2...................... 獲取顯示在指定單元格中的布林值 (true=1|false=0)Not(A2:E2)..... 替換(每個值由其相反的值:False ~> True=1, True ~> False=0)*Column(A2:E2)...與單元格的列號相乘,因此僅對于具有原始False條目的單元格接收大于 0 的值(即,作為與因子 1 相乘的結果的相應列號)。
最終過濾允許排他地獲得想要的列號,即沒有零值。Join將找到的元素組合到結果輸出字串中,最終可以將其寫入第 6 列(部分 c)。
Option Explicit ' Force declaration of variables (head of code module)
Sub DetectFalseValues()
Dim ws As Worksheet
Set ws = Sheet1 ' << change to required sheet Code(Name)
'a) get column numbers if original cell value equals False (negated as Not True);
' (vice versa get zero for original True value after negation)
Dim allValues() As Variant
allValues = ws.Evaluate("Not(A2:E2)*Column(A2:E2)")
'b) filter out all zero values and combine elements to string
Dim output As String
output = Join(Filter(allValues, 0, False), " and ")
output = IIf(Len(output) = 0, "No Change", "Change in " & output)
'c) write to target
ws.Range("F2").Value = output
''optional display in VB Editor's immediate window
' Debug.Print output ' e.g. "Change in 2 and 4"
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/390969.html
