简体   繁体   中英

Using VBA, hide multiple rows based on a single cell that is empty but has a link to another worksheet code

I have a workbook that uses data from one worksheet to fill in cells of another worksheet.

I am having problems finding or building a VBA that will automatically hide several rows base on a cell that has a "link" to a cell in another worksheet. Need for several sections.

Worksheet 1 - is a Data page Worksheet 2 - is a Display page

For example:
Worksheet "Program Data" Program Data

Worksheet 2 is the "Production Schedule" Production Schedule

On worksheet 2 "Production Schedule"

I want to automatically hide rows 13:20 if Cell E15 is blank (but it has the "='Program Data'!E16" reference in it.)

and additionally

I want to automatically hide rows 21:28 if Cell E21 is blank (but it has the "='Program Data'!E26" reference in it.)

and so on...... for multiple sections of the worksheet "Production Schedule"... Not all sections have the same number of rows to hide if the indicated cell is blank or has the "link" to Program Data worksheet.

I also would like for it to unhide rows if data is entered into the "reference cell" on the data sheet at a later time..

I know this is asking a lot, so any advice would be greatly appreciated.

Here's a simple VBA script that worked fine for me:

I created two Excel sheets: "Sheet1" & "Sheet2"

In Sheet2, cell:A1, I made a reference to a cell on Sheet1

在此处输入图像描述

By default, Excel cell reference formulas (like the one above) will return 0 if the referenced cell is blank . You can change this easily with an =if statement if desired, eg:

=IF(Sheet1!A1<>"",Sheet1!A1,"")

For the VBA script, I let the reference cell return the default value "0"

在此处输入图像描述

Now, if I input "bob" into the referenced cell and run the VBA script again, x returns "bob"

在此处输入图像描述

Okay, now here is the simple script which worked fine when I tested it:

在此处输入图像描述

Note that I added "Calculate" which will calculate the formulas in the workbook (including our formula reference cell) to ensure the returned value is correct.

Of course, you can modify this to check other cells, hide other rows, etc. Including the Else portion in your VBA IF statement will also "unhide" rows if the reference value is populated or said another way, doesn't equal "0" by setting the hidden property to false .

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