+ Reply to Thread
Results 1 to 5 of 5

VBA, trying to hide rows on different worksheet.

  1. #1
    Forum Contributor
    Join Date
    06-21-2014
    Location
    Brighton
    MS-Off Ver
    2011
    Posts
    115

    VBA, trying to hide rows on different worksheet.

    Hi,

    I'm new to VBA so apologies for the below explanation.

    I have two work sheets.
    Let's call them;
    - Sheet1
    - Sheet2

    In each worksheet is a dropdown list.
    When the dropdown list is selected on either sheet I already have a macro which automatically runs to update the dropdown on the other sheet.

    The dropdown changes the list of data shown on each sheet.
    Sheet1 however ends up with lots of empty rows of data.

    I'm therefore trying to get a macro to automatically hide all rows with no data on Sheet1

    I've already managed to do this on Sheet1 however when I change the dropdown on the Sheet2 then Sheet1 updates the dropdown but the macro doesn't run to hide the rows.
    I'm therefore trying to get the VBA code to work on Sheet2 so that it not only updates the dropdown on Sheet1 but it also hides the rows on Sheet1 at the same time.

    The VBA code I have is as follows;

    Sheet1 (working correctly; when dropdown changed then the dropdown on Sheet2 also changes & the macro hides the blank rows on Sheet1)

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim targetSheet As Worksheet
    If Not Intersect(Target, Range("A3")) Is Nothing Then
    Set targetSheet = ActiveWorkbook.Worksheets("Customer Overview")
    On Error Resume Next
    Application.EnableEvents = False
    targetSheet.Range("A2") = Target.Value
    Application.EnableEvents = True
    Set targetSheet = ActiveWorkbook.Worksheets("Lifetime Account")
    On Error Resume Next
    Application.EnableEvents = False
    targetSheet.Range("A3") = Target.Value
    Application.EnableEvents = True
    End If

    Sheets("Aged Debtor").Select
    ActiveSheet.Unprotect
    'Updateby Extendoffice 20160913
    Dim xRg As Range
    Application.ScreenUpdating = False
    For Each xRg In Range("A10:A43")
    If xRg.Value = "" Then
    xRg.EntireRow.Hidden = True
    Else
    xRg.EntireRow.Hidden = False
    End If
    Next xRg
    Application.ScreenUpdating = True
    Sheets("Aged Debtor").Select
    ActiveSheet.protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    End Sub


    Sheet2 (not working; this changes the dropdown on Sheet1 correctly however doesn't hide the rows on Sheet1)

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim targetSheet As Worksheet
    If Not Intersect(Target, Range("A3")) Is Nothing Then
    Set targetSheet = ActiveWorkbook.Worksheets("Customer Overview")
    On Error Resume Next
    Application.EnableEvents = False
    targetSheet.Range("A2") = Target.Value
    Application.EnableEvents = True
    Set targetSheet = ActiveWorkbook.Worksheets("Aged Debtor")
    On Error Resume Next
    Application.EnableEvents = False
    targetSheet.Range("A3") = Target.Value
    Application.EnableEvents = True
    End If

    Sheets("Aged Debtor").Select
    ActiveSheet.Unprotect
    With Worksheets("Aged Debtor")
    'Updateby Extendoffice 20160913
    Dim xRg As Range
    Application.ScreenUpdating = False
    For Each xRg In Range("A10:A43")
    If xRg.Value = "" Then
    xRg.EntireRow.Hidden = True
    Else
    xRg.EntireRow.Hidden = False
    End If
    Next xRg
    Application.ScreenUpdating = True
    Sheets("Aged Debtor").Select
    ActiveSheet.protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    Sheets("Lifetime Account").Select
    End With
    End Sub


    Not sure what I have wrong in the 2nd sheet code?

    Any help very much aprpeciated.
    Last edited by matthew_salter; 07-05-2018 at 04:47 PM. Reason: Spelling mistake

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,355

    Re: VBA, trying to hide rows on different worksheet.

    Hi Matthew,

    The general rule is that macros have a range that they work on. If you have a macro behind a worksheet, it should only work on that sheet. If you want a macro to work on many different sheets you need to put it in a MODULE which looks over all sheets and userforms. The topic is called Scope.

    https://www.stl-training.co.uk/artic...ba-coding.html

    I'm not sure that is your problem but from what you suggest, it might be.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: VBA, trying to hide rows on different worksheet.

    In the meantime, try modifying the last part of your sheet 2 code.
    Replace this
    Please Login or Register  to view this content.
    With this

    Please Login or Register  to view this content.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  4. #4
    Forum Contributor
    Join Date
    06-21-2014
    Location
    Brighton
    MS-Off Ver
    2011
    Posts
    115

    Re: VBA, trying to hide rows on different worksheet.

    @JLGWhiz, that's worked like a dream!
    Wonderful, thank you.

    Thanks also to @MarvinP for looking into this as well.

  5. #5
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: VBA, trying to hide rows on different worksheet.

    Quote Originally Posted by matthew_salter View Post
    @JLGWhiz, that's worked like a dream!
    Wonderful, thank you.

    Thanks also to @MarvinP for looking into this as well.
    You're welcome, and don't forget to mark the thread as solved. (Thread tools)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Help! I cannot hide rows once I protect my worksheet.
    By ElaineMok in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-03-2016, 03:46 AM
  2. Delete & Hide Rows in a Worksheet based on the Value of a Cell in another Worksheet
    By lhickerson in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-12-2014, 09:53 AM
  3. Macro to hide rows on a worksheet, based on a cell's value on another worksheet.
    By buttercup116 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-29-2014, 10:47 AM
  4. Hide rows on another worksheet based on value in that row
    By ahtigers10 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-04-2014, 02:55 PM
  5. Hide rows on one worksheet by using a range on another worksheet
    By suzig in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-24-2012, 07:02 AM
  6. Hide/Unhide Rows in One Worksheet Based on Values in Cells in Another Worksheet
    By xponent_es in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-27-2011, 03:26 PM
  7. Three tables on one worksheet, need to hide rows
    By Chiku in forum Excel General
    Replies: 12
    Last Post: 12-06-2005, 06:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1