[英]Excel-VBA Comparing Column Data
我對 Excel VBA 完全陌生,我需要編寫代碼來完成一個簡單的任務。 我需要逐行比較兩個不同列中的值,看看差異是否超過 50。如果是,我需要突出顯示整行。
我用其他語言編程,所以我對它的工作原理有基本的了解,但我不知道如何導航單元格/查看單元格內的內容。 我試過了,但沒有成功(它只會突出顯示每一行)。 我簡化了它以比較值是否相等,但無濟於事(仍然突出顯示所有內容)有人可以幫我嗎?
Dim strF0_col As Integer, sF0_col As Integer
Dim myRow, counter As Integer
Dim rEnd As Integer
Sub compare_F0()
rEnd = 100
strF0_col = 307
sF0_col = 317
counter = 0
For myRow = 2 To rEnd Step 1
Application.StatusBar = counter & "rows highlighted."
If (Cells(myRow, strF0_col).Value = Cells(myRow, sF0_col).Value) Then
Cells(myRow, strF0_col).EntireRow.Interior.ColorIndex = 28
End If
Next myRow
End Sub
提前致謝
正如@Doug Glancy 所建議的那樣,是否有任何理由不使用條件格式?
它在這里對我來說工作得很好。
如果您想試一試,請執行以下操作...
=$KU2>$LE2+50
請注意行索引(在本例中為 2)不能有 $ 符號。
希望能幫助到你。
您可能不想突出顯示空白的行?
如果是這樣,請使用
If Cells(myRow, strF0_col).Value <> "" And _
Cells(myRow, strF0_col).Value = Cells(myRow, sF0_col).Value Then
順便說一句,像這樣訪問單元格值非常慢。 如果您只處理 100 行,那么它足夠快,但如果這個數字增長,那么您會發現它會減慢到痛苦的程度。
將范圍值復制到變體數組然后循環它要快得多。 搜索[Excel] "Variant Array"
有很多答案顯示如何執行此操作並解釋它為什么有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.