[英]Excel - VBA code to check column A (person's name) against Columns F(6) to K(11), if any contain "Yes", then add "Yes" to the blank rows
@FaneDuru kindly created the code below: @FaneDuru 友好地创建了以下代码:
Sub fillYes()
Dim sh As Worksheet, lastR As Long, arr, arrFin, i As Long, dict As Object
Set sh = ActiveSheet
lastR = sh.Range("A" & sh.rows.count).End(xlUp).row
arr = sh.Range("A2:I" & lastR).Value2 'place the range in an array, for faster iteration
'place the names having yes/yes in a dictionary:
Set dict = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(arr)
If arr(i, 8) = "yes" And arr(i, 9) = "yes" Then
dict(arr(i, 1)) = 1
End If
Next i
'place the columns to be adapted in an array:
arrFin = sh.Range("H2:I" & lastR).Value2
For i = 1 To UBound(arr)
If dict.Exists(arr(i, 1)) Then arrFin(i, 1) = "yes": arrFin(i, 2) = "yes"
Next i
'drop the final array content at once:
sh.Range("H2").Resize(UBound(arrFin), 2).value = arrFin
End Sub
Which would check Columns H and I. I'm just looking to amend this slightly so it also applies to Columns 6,7,8,9,10,11 (F to K).这将检查 H 列和 I 列。我只是想稍微修改一下,因此它也适用于 6、7、8、9、10、11 列(F 到 K)。 NOTE - Each column should be treated separately, so for example, if Column F contained a "Yes", that doesn't then automatically mean that all the columns for that person should contain a "Yes"
注意 - 每一列都应单独处理,例如,如果 F 列包含“是”,这并不意味着该人的所有列都应包含“是”
Any help would be greatly appreciated.任何帮助将不胜感激。
Please, use the next adapted answer.请使用下一个改编的答案。 To avoid an imbricated range of many
If - End If
statements, I created boolYes
variable which will become True
of the first "yes" occurrence in the mentioned columns.为了避免许多
If - End If
语句的重叠范围,我创建了boolYes
变量,该变量将在提到的列中出现第一个“是”时变为True
。 The code also is able to deal with "Yes" or "YES" instead of only "yes":该代码还能够处理“是”或“是”,而不仅仅是“是”:
Sub fillYes_Extended()
Dim sh As Worksheet, lastR As Long, arr, arrFin, i As Long, dict As Object
Dim j As Long, boolYes As Boolean
Set sh = ActiveSheet
lastR = sh.Range("A" & sh.rows.count).End(xlUp).row
arr = sh.Range("A2:K" & lastR).Value2 'place the range in an array, for faster iteration
'place the names having yes/yes in the required columns, in a dictionary:
Set dict = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(arr)
For j = 6 To 11
If LCase(arr(i, j)) = "yes" Then boolYes = True: Exit For
Next j
If boolYes Then
dict(arr(i, 1)) = 1: boolYes = False 'add the dictionary key and reinitialize boolYes variable
End If
Next i
'place the columns to be adapted in an array:
arrFin = sh.Range("F2:K" & lastR).Value2
For i = 1 To UBound(arr)
If dict.Exists(arr(i, 1)) Then
For j = 1 To 6
arrFin(i, j) = "yes"
Next j
End If
Next i
'drop the final array content at once:
sh.Range("F2").Resize(UBound(arrFin), UBound(arrFin, 2)).Value = arrFin
End Sub
Please, test it and send some feedback.请测试它并发送一些反馈。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.