[英]VBA Macro Loop Through Columns in Rows and Compare Values
預先感謝您花時間閱讀並幫助我。 我正在處理的宏的一部分應該 go 通過行並比較行的最后兩個單元格中的值(每行的列將不同,因此我不能只取特定兩列中的值進行比較)。
稍后,宏將根據發現更改其他單元格中的值。
我遇到的困難是分配行中最后一個單元格的值和最后一個單元格的值。
我的代碼是:
Sub compareValues()
Dim allInLastRow As Long
Dim allInLastCol As Long
Dim i As Integer
Dim x As Integer
Dim allInWs As Worksheet
Set allInWs = ThisWorkbook.Worksheets("All In")
allInLastRow = allInWs.Range("C" & Rows.Count).End(xlUp).Row
For i = 2 To allInLastRow 'scans through all the rows
allInLastCol = allInWs.Cells(i, Columns.Count).End(xlToLeft).Column
For x = 1 To allInLastCol 'scans through all the columns in each row
'in here I need to have the condition that compares lastcell value to 2nd last cell value and this is the one I have a problem with
Next x
Next i
End Sub
任何朝着正確方向輕推將不勝感激。 我很想從谷歌得到一個答案,但我不能真正以一種對搜索引擎有意義的方式來表述這個問題:-)
再次感謝您的幫助!
我可以想到要查找行中的最后一列,然后查找行中的倒數第二列。
Sub LstRw_Stuff()
Dim Rws As Long, r As Range, fRng As Range, col1 As Long, col2 As Long
Dim rng1 As Range, rng2 As Range
Set r = Range("A1")
Rws = Cells.Find(What:="*", After:=r, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For x = 2 To Rws
col1 = Cells(x, Columns.Count).End(xlToLeft).Column
Set rng1 = Cells(x, col1)
s = rng1.Value
rng1.ClearContents
col2 = Cells(x, Columns.Count).End(xlToLeft).Column
Set rng2 = Cells(x, col2)
rng1 = s
'do whatever need to be done for comparing
If rng1 > rng2 Then
MsgBox "yep" & rng1.Value & ">" & rng2.Value
Else
MsgBox "Nope" & rng1.Value & "<" & rng2.Value
End If
Next x
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.