简体   繁体   English

根据单元格值有条件清除一行中单元格范围的内容

[英]Conditionally clear contents of range of cells in a row based on cell value

I have an excel file with two sheets: Sheet1 and Sheet2. 我有一个包含两张纸的Excel文件:Sheet1和Sheet2。 Sheet 1 has store information while Sheet 2 has product quantity by store information 工作表1具有商店信息,工作表2具有按商店信息的产品数量

In Sheet 1, Range "A1:A5" lists 5 store names, Store A through Store E, while range "B1:B5" in Sheet 1 is a drop down box that lets the user choose from the following: "Open", "Closed", or "". 在工作表1中,范围“ A1:A5”列出了5个商店名称,即商店A到商店E,而工作表1中的范围“ B1:B5”是一个下拉框,允许用户从以下选项中进行选择:“打开”,“已关闭”或“”。

In Sheet 2, there is a table that lists the quantity of products by store. 在工作表2中,有一个表,按商店列出了产品数量。 Something like: 就像是:

在此处输入图片说明

I am looking for a way to clear the contents of range B:F in Sheet2 when the respective store in Sheet1 is not set to "Open". 我正在寻找一种方法来清除Sheet1中相应存储区未设置为“打开”时Sheet2中范围B:F的内容。

Currently, I am linking the values in Column B from Sheet1 to Column G in Sheet2, and have set the code to the following in Sheet2: 当前,我正在将Sheet1的B列中的值链接到Sheet2的G列中,并在Sheet2中将代码设置为以下代码:

Private Sub Worksheet_Activate() 
If Range("G2") <> "Open" Then
Range("B2:F2").ClearContents
End If

How can I perform this in a loop rather than copying this code five separate times? 我该如何在循环中执行此操作,而不是分别复制此代码五次?

Something like this? 像这样吗

For i = 2 To 6 'This is the starting row (2) to the ending row (6)
    If Range("G" & i) <> "Open" Then 'We use i to dynamically build the range as a string
        Range("B" & i & ":F" & i).ClearContents
    End If
Next i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用基于 Interior.Color 的合并单元格清除单元格范围的内容 - Clear contents of cell range with merged cells based on Interior.Color 根据VBA中其他单元格的值,有条件地格式化单元格的循环范围 - Conditionally formatting a looped range of cells based on value in other cell in VBA 根据字符串/空单元格清除单元格中的内容 - Clear contents in Cells based on String/empty cell 根据第一个单元格有条件地对单元格进行格式化 - Format cells in row conditionally based on the first cell 根据其他单元格中的值清除单元格内容 - Clear contents of cell based on value in other cell 根据相邻像元值锁定行中像元的范围 - Lock range of Cells in row based on adjacent cell value 基于相邻单元格值可编辑/不可编辑的行中的单元格范围 - Range of cells in a row being editable/not editable based on an adjacent cell value VBA 如何根据该范围内一个单元格的值有条件地格式化一系列单元格? - VBA How do I conditionally format a range of cells based on the value of one cell in that range? 如果在vba中更改了单元格范围,则清除单元格内容 - Clear cell contents if another is changed for a range of cells in vba 循环遍历范围并清除正在评估的单元格下方 8 个单元格的内容 - Loop through range and clear contents of 8 cells below the cell being evaluated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM