+ Reply to Thread
Results 1 to 6 of 6

Make code Faster

Hybrid View

zplugger Make code Faster 11-30-2013, 09:06 AM
HaHoBe Re: Make code Faster 11-30-2013, 09:29 AM
zplugger Re: Make code Faster 11-30-2013, 09:51 AM
HaHoBe Re: Make code Faster 11-30-2013, 09:54 AM
zplugger Re: Make code Faster 11-30-2013, 10:00 AM
Marc L Re: Make code Faster 11-30-2013, 10:08 AM
  1. #1
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Make code Faster

    Can this be made faster? Works but must be cleaner way.
    Thanks Z

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If CheckBox1.Value = True Then Range("A3").Value = Range("E34").Value
     If CheckBox1.Value = False Then Range("A3").Value = 0
     If CheckBox2.Value = True Then Range("A4").Value = Range("f34").Value
     If CheckBox2.Value = False Then Range("A4").Value = 0
    If CheckBox3.Value = True Then Range("A5").Value = Range("g34").Value
     If CheckBox3.Value = False Then Range("A5").Value = 0
     If CheckBox4.Value = True Then Range("A6").Value = Range("h34").Value
     If CheckBox4.Value = False Then Range("A6").Value = 0
     If CheckBox5.Value = True Then Range("A7").Value = Range("i34").Value
     If CheckBox5.Value = False Then Range("A7").Value = 0
     If Target.Row = 7 And (Target.Row >= 7 And Target.Row <= 7) Then
            Range("c7").Font.ColorIndex = 11
             Range("c7").Font.Underline = True
       Else: Range("c7").Font.ColorIndex = 0
       Range("c7").Font.Underline = False
       End If
       If Target.Row = 8 And (Target.Row >= 8 And Target.Row <= 8) Then
            Range("c8").Font.ColorIndex = 11
             Range("c8").Font.Underline = True
       Else: Range("c8").Font.ColorIndex = 0
       Range("c8").Font.Underline = False
       End If
       If Target.Row = 9 And (Target.Row >= 9 And Target.Row <= 9) Then
            Range("c9").Font.ColorIndex = 11
             Range("c9").Font.Underline = True
       Else: Range("c9").Font.ColorIndex = 0
       Range("c9").Font.Underline = False
       End If
       If Target.Row = 10 And (Target.Row >= 10 And Target.Row <= 10) Then
            Range("c10").Font.ColorIndex = 11
             Range("c10").Font.Underline = True
       Else: Range("c10").Font.ColorIndex = 0
       Range("c10").Font.Underline = False
       End If
       
       If Target.Row = 11 And (Target.Row >= 11 And Target.Row <= 11) Then
            Range("c11").Font.ColorIndex = 11
             Range("c11").Font.Underline = True
       Else: Range("c11").Font.ColorIndex = 0
       Range("c11").Font.Underline = False
       End If
       If Target.Row = 12 And (Target.Row >= 12 And Target.Row <= 12) Then
            Range("c12").Font.ColorIndex = 11
             Range("c12").Font.Underline = True
       Else: Range("c12").Font.ColorIndex = 0
       Range("c12").Font.Underline = False
       End If
       If Target.Row = 13 And (Target.Row >= 13 And Target.Row <= 13) Then
            Range("c13").Font.ColorIndex = 11
             Range("c13").Font.Underline = True
       Else: Range("c13").Font.ColorIndex = 0
       Range("c13").Font.Underline = False
       End If
       If Target.Row = 14 And (Target.Row >= 14 And Target.Row <= 14) Then
            Range("c14").Font.ColorIndex = 11
             Range("c14").Font.Underline = True
       Else: Range("c14").Font.ColorIndex = 0
       Range("c14").Font.Underline = False
       End If
       
       If Target.Row = 15 And (Target.Row >= 15 And Target.Row <= 15) Then
            Range("c15").Font.ColorIndex = 11
             Range("c15").Font.Underline = True
       Else: Range("c15").Font.ColorIndex = 0
       Range("c15").Font.Underline = False
       End If
       If Target.Row = 16 And (Target.Row >= 16 And Target.Row <= 16) Then
            Range("c16").Font.ColorIndex = 11
             Range("c16").Font.Underline = True
       Else: Range("c16").Font.ColorIndex = 0
       Range("c16").Font.Underline = False
       End If
       
       If Target.Row = 17 And (Target.Row >= 17 And Target.Row <= 17) Then
            Range("c17").Font.ColorIndex = 11
             Range("c17").Font.Underline = True
       Else: Range("c17").Font.ColorIndex = 0
       Range("c17").Font.Underline = False
       End If
       If Target.Row = 18 And (Target.Row >= 18 And Target.Row <= 18) Then
            Range("c18").Font.ColorIndex = 11
             Range("c18").Font.Underline = True
       Else: Range("c18").Font.ColorIndex = 0
       Range("c18").Font.Underline = False
       End If
       If Target.Row = 21 And (Target.Row >= 21 And Target.Row <= 21) Then
            Range("c21").Font.ColorIndex = 11
             Range("c21").Font.Underline = True
       Else: Range("c21").Font.ColorIndex = 0
       Range("c21").Font.Underline = False
       End If
       If Target.Row = 22 And (Target.Row >= 22 And Target.Row <= 22) Then
            Range("c22").Font.ColorIndex = 11
             Range("c22").Font.Underline = True
       Else: Range("c22").Font.ColorIndex = 0
       Range("c22").Font.Underline = False
       End If
       If Target.Row = 25 And (Target.Row >= 25 And Target.Row <= 25) Then
            Range("c25").Font.ColorIndex = 11
             Range("c25").Font.Underline = True
       Else: Range("c25").Font.ColorIndex = 0
       Range("c25").Font.Underline = False
       End If
       
       If Target.Row = 26 And (Target.Row >= 26 And Target.Row <= 26) Then
            Range("c26").Font.ColorIndex = 11
             Range("c26").Font.Underline = True
       Else: Range("c26").Font.ColorIndex = 0
       Range("c26").Font.Underline = False
       End If
       If Target.Row = 27 And (Target.Row >= 27 And Target.Row <= 27) Then
            Range("c27").Font.ColorIndex = 11
             Range("c27").Font.Underline = True
       Else: Range("c27").Font.ColorIndex = 0
       Range("c27").Font.Underline = False
       End If
       If Target.Row = 28 And (Target.Row >= 28 And Target.Row <= 28) Then
            Range("c28").Font.ColorIndex = 11
             Range("c28").Font.Underline = True
       Else: Range("c28").Font.ColorIndex = 0
       Range("c28").Font.Underline = False
       End If
       If Target.Row = 29 And (Target.Row >= 29 And Target.Row <= 29) Then
            Range("c29").Font.ColorIndex = 11
             Range("c29").Font.Underline = True
       Else: Range("c29").Font.ColorIndex = 0
       Range("c29").Font.Underline = False
       End If
       If Target.Row = 30 And (Target.Row >= 30 And Target.Row <= 30) Then
            Range("c30").Font.ColorIndex = 11
             Range("c30").Font.Underline = True
       Else: Range("c30").Font.ColorIndex = 0
       Range("c30").Font.Underline = False
       End If
       If Target.Row = 31 And (Target.Row >= 31 And Target.Row <= 31) Then
            Range("c31").Font.ColorIndex = 11
             Range("c31").Font.Underline = True
       Else: Range("c31").Font.ColorIndex = 0
       Range("c31").Font.Underline = False
       End If
       If Target.Row = 32 And (Target.Row >= 32 And Target.Row <= 32) Then
            Range("c32").Font.ColorIndex = 11
             Range("c32").Font.Underline = True
       Else: Range("c32").Font.ColorIndex = 0
       Range("c32").Font.Underline = False
       End If
       If Target.Row = 34 And (Target.Row >= 34 And Target.Row <= 34) Then
            Range("c34").Font.ColorIndex = 11
             Range("c34").Font.Underline = True
       Else: Range("c34").Font.ColorIndex = 0
       Range("c34").Font.Underline = False
       
       End If
     End Sub
    Last edited by zplugger; 11-30-2013 at 10:05 AM.

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

    Re: Make code Faster

    Hi, zplugger,

    like this?
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    If CheckBox1.Value Then Range("A3").Value = Range("E34").Value Else Range("A3").Value = 0
    If CheckBox2.Value Then Range("A4").Value = Range("f34").Value Else Range("A4").Value = 0
    If CheckBox3.Value Then Range("A5").Value = Range("g34").Value Else Range("A5").Value = 0
    If CheckBox4.Value Then Range("A6").Value = Range("h34").Value Else Range("A6").Value = 0
    If CheckBox5.Value Then Range("A7").Value = Range("i34").Value Else Range("A7").Value = 0
    
    With Range("C7:C18, C21:C22, C25:C32, C34:C34")
      .ColorIndex = 0
      .Font.Underline = False
    End With
    
    Select Case Target.Row
      Case 7 To 18, 21 To 22, 25 To 32, 34
        With Range("C" & Target.Row).Font
          .ColorIndex = 11
          .Font.Underline = True
        End With
      Case Else
    End Select
    
    End Sub
    Maybe get the Range clearance into the Select Case as well.

    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
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Make code Faster

    Wow that small

    Getting error
    object doesn't support run time 438
    colorindex=0 is yellow

    I'm using office 2013

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

    Re: Make code Faster

    Hi, zplugger,

    my fault, wasn´t consequent there regarding the object to work on:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    If CheckBox1.Value Then Range("A3").Value = Range("E34").Value Else Range("A3").Value = 0
    If CheckBox2.Value Then Range("A4").Value = Range("f34").Value Else Range("A4").Value = 0
    If CheckBox3.Value Then Range("A5").Value = Range("g34").Value Else Range("A5").Value = 0
    If CheckBox4.Value Then Range("A6").Value = Range("h34").Value Else Range("A6").Value = 0
    If CheckBox5.Value Then Range("A7").Value = Range("i34").Value Else Range("A7").Value = 0
    
    Select Case Target.Row
      Case 7 To 18, 21 To 22, 25 To 32, 34
        With Range("C7:C18, C21:C22, C25:C32, C34:C34").Font
          .ColorIndex = 0
          .Underline = False
        End With
        With Range("C" & Target.Row).Font
          .ColorIndex = 11
          .Underline = True
        End With
      Case Else
    End Select
    
    End Sub
    Ciao,
    Holger

  5. #5
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Make code Faster

    OMG its like the spread sheet is in Turbo

    Thank you so much,will study code to understand better,your a saint.
    Z

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Re: Make code Faster


    Hi zplugger !

    Desactivating the screen refresh (see Application.ScreenUpdating) makes your code faster …

+ 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. Make VBA code faster
    By Danielle22 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-28-2013, 09:01 AM
  2. Make the below code work faster!!!
    By devpp in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-04-2013, 06:13 AM
  3. [SOLVED] Ways to make my code run faster
    By JazzyBear in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-03-2013, 03:59 PM
  4. [SOLVED] Make code run faster
    By ozhunter in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-20-2013, 04:26 AM
  5. Make code faster/better best practices
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 12-29-2008, 10:25 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