[英]Excel - Get value of a column in a row where match found in another column
[英]How to find a value in a column and get the value of another column on that row and format that row in Excel?
所以我的問題是
我有4列(A,B,C,D),我需要找出A列中是否存在C列中的值,如果需要,我需要在D列中查找值(在我的初始值的行上)並檢查它是否與B列中的值相同(在我找到匹配項的行上),然后對C列中的每個值執行此操作。
我從未在Visual Basic中編寫過代碼,並且幾乎沒有Excel經驗。 我認為代碼應該是這樣的:(當然是在VB中)
for(int i=0;i<numOfRecordsInC;i++){
for(int m=0;m<numOfRecordsInA;m++){
if(C[i]==A[m] && D[i]==B[m]){//ith,mth element of the columns
//Highlight mth row
//or just print "1" to any column on mth row if that's easier
}
}
}
如何在Excel中的VBA中編寫類似的內容,或者可以在Excel中使用其他工具執行此操作? 有條件的格式用於重復項是沒有用的,因為沒有保證列內部不會有重復的值。
好吧,經過一些研究,我想出了一個這樣的公式:
=IF(COUNTIF(E$5:E$1605;P5);IF(VLOOKUP(P5;E$5:J$1605;2;0)=R5;"Matched";"Not Matched");"Not Found")
但是我得到錯誤的結果,或者我的公式有問題。
你想要這樣的東西嗎?
Sub highlightCells()
With Worksheets("sheet1")
'calculate last row for colA & colB
lastRowLeft = WorksheetFunction.Max( _
.Cells(.Rows.Count, "A").End(xlUp).Row, _
.Cells(.Rows.Count, "B").End(xlUp).Row)
'calculate last row for colC & colD
lastRowRight = WorksheetFunction.Max( _
.Cells(.Rows.Count, "C").End(xlUp).Row, _
.Cells(.Rows.Count, "D").End(xlUp).Row)
'match
For i = 1 To lastRowRight
For m = 1 To lastRowLeft
If .Cells(i, "C").Value = .Cells(m, "A").Value _
And .Cells(i, "D").Value = .Cells(m, "B").Value Then
'do sth
Debug.Print "(m,i)=(" & m & "," & i & ")"
End If
Next
Next
End With
End Sub
示例數據
| A B C D
--------------
1| 1 A
2| 5 2 B
3| 2 B 3 C
4| 4 D
5| 5 E
6| 4 D
結果示例
(m,i)=(3,2)
(m,i)=(6,4)
找到了適用於我的情況的公式(即使列重復,仍然可以提供正確的結果)
=IF(COUNTIF(E$5:E$1605;P5);IF(COUNTIFS(E$5:E$1605;P5;J$5:J$1605;R5);"Matched";"Not Matched");"Not Found")
Excel不會讓我用,
以單獨的參數和建議;
。 也許是因為Windows的語言設置所致,所以如果您最終使用此公式,請記住這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.