简体   繁体   中英

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. Sheet 1 has store information while Sheet 2 has product quantity by store information

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 "".

In Sheet 2, there is a table that lists the quantity of products by store. 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".

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM