简体   繁体   English

Excel - VBA 代码检查列 A(人名)与列 F(6) 到 K(11),如果有包含“是”,则在空白行中添加“是”

[英]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.

相关问题 Excel - VBA 代码检查列 A(人名)与列 H 和 I,如果任何行包含“是”,则将“是”添加到空白行 - Excel - VBA code to check column A (person's name) against Column H and I, if any of the rows contain "Yes", then add "Yes" to the blank rows Excel VBA检查工作表是否存在,如果是,则在工作表名称中添加数字 - Excel VBA check if sheet exists and if yes add numeric to sheet name MsgBox是/否Excel VBA - MsgBox Yes/No Excel VBA Excel - 如果多个非连续列包含值是,我想返回真值。 但我也希望它忽略空白单元格 - Excel - I want to return the value of true if multiple non consecutive columns contain the value yes. But I also want it to ignore blank cells Excel:当 column = yes 时添加行名称和列标题到行的宏 - Excel: Macro to add Row name and column headers to row when column = yes Excel VBA根据单元格颜色显示“隐藏行”,如果命令显示“是” - Excel VBA Hide Rows based on cell color and if command says “Yes” 如何使用 VBA 在 Excel 的 5 列中获得“是”和“否”的排列? - How to use VBA to get permutations of “yes” and “No” across 5 columns in Excel? 如何为特定列中有“是”的行复制特定列 - How to copy specific columns for rows where there is a “yes” in a particular column Excel:检查一个值是否在另一列中,如果是,则属性另一个值 - Excel: Check if a value is in another column and if yes attribute another value Excel:如何在工作表中查找名称并在另一列上标记“是” - Excel: How to find name in sheet and mark “yes” on another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM