+ Reply to Thread
Results 1 to 3 of 3

Shorten VBA code

Hybrid View

  1. #1
    Registered User
    Join Date
    01-26-2012
    Location
    Slovenia; Kranj
    MS-Off Ver
    Office 365
    Posts
    67

    Shorten VBA code

    Hi,

    I have two sheets... on first one there is drop-down menu with YES/NO and on the second I have hidden rows. If user select YES on first, row (with specific number.) un-hide on second sheet. NO or "" (empty) hide row. What I made works, but already when putting together I said that I'll ask you, for some other solution due to personal learning. But, always that but, now that I want to add solution to other column on a fist sheet I got error message that code is to long (Procedure too Large Error). So... part of mine code...

    Private Sub Worksheet_Change(ByVal Target As Range)
    ...
        If Not Intersect(Target, Me.Range("$D$39:$E$39")) Is Nothing Then
           If Range("$D$39").Value = "NO" Or Range("$D$39").Value = "" Then
                Sheets("xxxx").Rows("57:57").EntireRow.Hidden = True
            ElseIf Range("$D$39").Value = "YES" Then
                Sheets("xxxx").Rows("57:57").EntireRow.Hidden = False
            End If
        End If
        If Not Intersect(Target, Me.Range("$D$40:$E$40")) Is Nothing Then
           If Range("$D$40").Value = "NO" Or Range("$D$40").Value = "" Then
                Sheets("xxxx").Rows("58:58").EntireRow.Hidden = True
            ElseIf Range("$D$40").Value = "YES" Then
                Sheets("xxxx").Rows("58:58").EntireRow.Hidden = False
            End If
        End If
        If Not Intersect(Target, Me.Range("$D$41:$E$41")) Is Nothing Then
           If Range("$D$41").Value = "NO" Or Range("$D$41").Value = "" Then
                Sheets("xxxx").Rows("59:59").EntireRow.Hidden = True
            ElseIf Range("$D$41").Value = "YES" Then
                Sheets("xxxx").Rows("59:59").EntireRow.Hidden = False
            End If
        End If
    ...
    End Sub
    As you can see 7 lines are repeating from row 39 to 118. At the moment just in D column, but needed also in E column. I believe that there can be done some "loop" or "for" or I don't know what... but... any ideas?

    Thx and wish you all the best in 2018!


    Cheers, Marko
    Last edited by DiCaver; 01-09-2018 at 05:41 AM.
    IF a = b THEN
    GO "back to school"

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: Shorten VBA code

    It looks to be just 18 rows below your target.row, I also assume that sheets("xxx") is not the sheet with the code.

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Count > 1 Then Exit Sub
        
        If Not Intersect(Target, Me.Range("$D$39:$E$41")) Is Nothing Then
        
            If Target.Value = "NO" Or Target.Value = "" Then
            
                Sheets("xxxx").Range("A" & Target.Row + 18).EntireRow.Hidden = True
                
            ElseIf Target.Value = "YES" Then
            
                Sheets("xxxx").Range("A" & Target.Row + 18).EntireRow.Hidden = False
                
            End If
            
        End If
    
    End Sub

  3. #3
    Registered User
    Join Date
    01-26-2012
    Location
    Slovenia; Kranj
    MS-Off Ver
    Office 365
    Posts
    67

    Re: Shorten VBA code

    ... sometimes I feel so stupid! :-(

    Thx for this... works perfect. And yes, simple and the best solution.

    Cheers, Marko

+ 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. shorten code vba
    By wildonln in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-07-2016, 03:50 PM
  2. [SOLVED] Represent (define?) a section of code with a variable (shorten long code lines)?
    By Gene@action in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-01-2016, 03:59 PM
  3. Hi all, can you please help me to shorten this code as much as possible
    By boddulus in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 08-31-2014, 07:31 AM
  4. [SOLVED] Is it possible to shorten this code?
    By aaaaaaiden in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 02-12-2014, 11:05 AM
  5. Looking to shorten VBA code
    By swhite7 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-02-2013, 06:31 PM
  6. [SOLVED] Very inefficient code because of different sizes of arrays, how do i shorten my code?
    By Brammer88 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-28-2012, 04:49 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