+ Reply to Thread
Results 1 to 6 of 6

VBA- Combining two Worksheet change scripts

  1. #1
    Registered User
    Join Date
    09-06-2022
    Location
    Redhill, England
    MS-Off Ver
    365
    Posts
    5

    Post VBA- Combining two Worksheet change scripts

    Hi all,

    I'm struggling to combine two Worksheet_Change(ByVal Target As Range) scripts in VBA.

    Any assistance would be most welcome!

    EnablingCharging Doc V7 TEST.xlsm
    Last edited by VCarver; 09-08-2022 at 03:11 AM. Reason: Solved

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,673

    Re: VBA- Combining two Worksheet change scripts

    It is not clear what you are trying to do:

    Your Case Select always selects R6 (=1) with "Case is =" to T12 which is empty ... then R7:R10 which never equal R6

    ????
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  3. #3
    Registered User
    Join Date
    09-06-2022
    Location
    Redhill, England
    MS-Off Ver
    365
    Posts
    5

    Re: VBA- Combining two Worksheet change scripts

    You've hit on half of my problem alone! Wrong cell references... update below.

    I'm trying to hide different rows based on a value in cell G7 for the segment highlighted in Red.
    The top segment works fine, but can't combine the two.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rRow As Long
    If Not Intersect(Target, Range("F15,F53,F91,F129,F167,F205,F243,F281,F319,F357")) Is Nothing Then
    If Target.Address = "$F$15" Then
    Rows("17:45").Hidden = True
    rRow = Range("C17:C45").Find(Target.Value).Row
    Rows(rRow & ":" & rRow + 3).Hidden = False
    ElseIf Target.Address = "$F$53" Then
    Rows("55:83").Hidden = True
    rRow = Range("C55:C83").Find(Target.Value).Row
    Rows(rRow & ":" & rRow + 3).Hidden = False
    ElseIf Target.Address = "$F$91" Then
    Rows("93:121").Hidden = True
    rRow = Range("C93:C121").Find(Target.Value).Row
    Rows(rRow & ":" & rRow + 3).Hidden = False
    ElseIf Target.Address = "$F$129" Then
    Rows("131:159").Hidden = True
    rRow = Range("C131:C159").Find(Target.Value).Row
    Rows(rRow & ":" & rRow + 3).Hidden = False
    ElseIf Target.Address = "$F$167" Then
    Rows("169:197").Hidden = True
    rRow = Range("C169:C197").Find(Target.Value).Row
    Rows(rRow & ":" & rRow + 3).Hidden = False
    ElseIf Target.Address = "$F$205" Then
    Rows("207:235").Hidden = True
    rRow = Range("C207:C235").Find(Target.Value).Row
    Rows(rRow & ":" & rRow + 3).Hidden = False
    ElseIf Target.Address = "$F$243" Then
    Rows("245:273").Hidden = True
    rRow = Range("C245:C273").Find(Target.Value).Row
    Rows(rRow & ":" & rRow + 3).Hidden = False
    ElseIf Target.Address = "$F$281" Then
    Rows("283:311").Hidden = True
    rRow = Range("C283:C311").Find(Target.Value).Row
    Rows(rRow & ":" & rRow + 3).Hidden = False
    ElseIf Target.Address = "$F$319" Then
    Rows("321:349").Hidden = True
    rRow = Range("C321:C349").Find(Target.Value).Row
    Rows(rRow & ":" & rRow + 3).Hidden = False
    ElseIf Target.Address = "$F$357" Then
    Rows("359:387").Hidden = True
    rRow = Range("C359:C387").Find(Target.Value).Row
    Rows(rRow & ":" & rRow + 3).Hidden = False

    Select Case Range("G7").Value
    Case Is = Range("R6"):
    Rows("9:390").EntireRow.Hidden = True
    Rows("11:16").EntireRow.Hidden = False
    Case Is = Range("R7"):
    Rows("9:390").EntireRow.Hidden = True
    Rows("49:54").EntireRow.Hidden = False
    Case Is = Range("R8"):
    Rows("9:390").EntireRow.Hidden = True
    Rows("87:92").EntireRow.Hidden = False
    Case Is = Range("R9"):
    Rows("9:390").EntireRow.Hidden = True
    Rows("125:130").EntireRow.Hidden = False
    Case Is = Range("R10"):
    Rows("9:390").EntireRow.Hidden = True
    Rows("163:168").EntireRow.Hidden = False
    Case Else:
    Rows("9:390").EntireRow.Hidden = False
    End Select

    End If
    End If
    End Sub

  4. #4
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,673

    Re: VBA- Combining two Worksheet change scripts

    Please Login or Register  to view this content.
    Last edited by JohnTopley; 09-07-2022 at 03:36 PM.

  5. #5
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,673

    Re: VBA- Combining two Worksheet change scripts

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    an alternative for the latter code

  6. #6
    Registered User
    Join Date
    09-06-2022
    Location
    Redhill, England
    MS-Off Ver
    365
    Posts
    5

    Re: VBA- Combining two Worksheet change scripts

    An excel god amongst mortals, thank you very much John!

    I couldn't see the wood through the trees.

+ 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. amalgamate 2 worksheet change vba scripts
    By paynod in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-23-2015, 05:23 AM
  2. Macro validation and combining scripts
    By CanterburyAllen in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-16-2013, 03:43 AM
  3. Combining two macro scripts (html information to excel)
    By hfrankl1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-19-2012, 03:27 PM
  4. Combining Two Scripts
    By BazzaBoy in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 09-13-2010, 04:12 AM
  5. Running Two Worksheet Change Scripts Gives Error
    By BazzaBoy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-12-2009, 04:24 AM
  6. For a website, are Excel scripts better than other programming language scripts?
    By Advice Pro in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-01-2009, 02:07 PM
  7. combining two short VBA scripts from two buttons
    By sloan.fader@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-24-2006, 03:15 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