+ Reply to Thread
Results 1 to 3 of 3

Referencing a cell from another worksheet in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    12-17-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    39

    Referencing a cell from another worksheet in VBA

    Hi,

    I have a drop down list in sheet 'Reinforcement' located in cell G4. When this cell selection changes, VBA code hides/unhides certain rows.

    In sheet 'summary', i would like to reference the drop down list in 'Reinforcement' in cell G4 to hide/unhide rows. Below is the successful code used when hiding/unhiding rows in 'Reinforcement':

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Me.Range("G4")) Is Nothing Then Exit Sub
    
    Application.EnableEvents = False 'to prevent endless loop
    Sheets("1. Reinforcement Calculation").Unprotect Password:="xxxxxxx"
    Select Case [g4].Value
      Case "Standard Calculation"
        Rows("128:138").EntireRow.Hidden = True
        Rows("101:127").EntireRow.Hidden = False
      Case "Select"
        Rows("101:127").EntireRow.Hidden = False
      Case "Fancy Tap"
        Rows("104:127").EntireRow.Hidden = True
        Rows("128:138").EntireRow.Hidden = False
    End Select
    Sheets("1. Reinforcement Calculation").Protect Password:="xxxxxxx"
    
    Application.EnableEvents = True
    End Sub
    How do i change this code so that it refers to G4 in the 'Reinforcement' sheet when the code is running in the 'summary' sheet?

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 365 on Win11 (365 on Win11 (desktop), 365 on Win11 (notebook)
    Posts
    8,207

    Re: Referencing a cell from another worksheet in VBA

    Hi, a-man,

    should only be Sheet Reinforcement be monitored? If so you could add thee same lines to hide/unhoide but add a qualifiers for sheet Summary there like
    '....
      Case "Standard Calculation"
        Rows("128:138").EntireRow.Hidden = True
        Sheets("Summary").Rows("128:138").EntireRow.Hidden = True
        Rows("101:127").EntireRow.Hidden = False
        Sheets("Summary").Rows("101:127").EntireRow.Hidden = False
    If the code should run on either shee I would set a Variable for teh worksheet on which it shuould be hgandled like
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Me.Range("G4")) Is Nothing Then Exit Sub
    Dim ws As Worksheet
    Application.EnableEvents = False 'to prevent endless loop
    If Target.Parent.Name = "1. Reinforcement Calculation" Then
      Set ws = Sheets("Summary")
    Else
      Set ws = Sheets("1. Reinforcement Calculation")
    End If
    ActiveSheet.Unprotect Password:="xxxxxxx"
    ws.Unprotect Password:="xxxxxxx"
    Select Case [g4].Value
      Case "Standard Calculation"
        Rows("128:138").EntireRow.Hidden = True
        Rows("101:127").EntireRow.Hidden = False
        ws.Rows("128:138").EntireRow.Hidden = True
        ws.Rows("101:127").EntireRow.Hidden = False
      Case "Select"
        Rows("101:127").EntireRow.Hidden = False
        ws.Rows("101:127").EntireRow.Hidden = False
      Case "Fancy Tap"
        Rows("104:127").EntireRow.Hidden = True
        Rows("128:138").EntireRow.Hidden = False
        ws.Rows("104:127").EntireRow.Hidden = True
        ws.Rows("128:138").EntireRow.Hidden = False
    End Select
    ActiveSheet.Protect Password:="xxxxxxx"
    ws.Protect Password:="xxxxxxx"
    Set ws = Nothing
    Application.EnableEvents = True
    End Sub
    Assuming that both sheets are protected with the same password (otherwise use a variable/array to handle the indivdual passwords).

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    12-17-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Referencing a cell from another worksheet in VBA

    Holger - Once again, thank you!

+ 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. Referencing a cell on another worksheet in VBA
    By kthoma85 in forum Excel General
    Replies: 5
    Last Post: 11-30-2013, 03:49 AM
  2. Issue - Referencing every 7th cell from another worksheet
    By alexmblog in forum Excel General
    Replies: 2
    Last Post: 09-28-2011, 09:16 AM
  3. Cell Formatting when referencing worksheet name
    By mlandrie in forum Excel General
    Replies: 2
    Last Post: 09-14-2011, 04:43 PM
  4. Referencing same cell on different worksheet
    By archipelago in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-05-2008, 10:10 AM
  5. [SOLVED] Referencing a cell from another worksheet
    By Mark in forum Excel General
    Replies: 3
    Last Post: 01-27-2006, 05:55 PM

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