+ Reply to Thread
Results 1 to 4 of 4

Need help cleaning up my code...

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-09-2013
    Location
    California, USA
    MS-Off Ver
    MS 365 Subscription
    Posts
    130

    Need help cleaning up my code...

    I have a repetitive code that skips every 20 lines and starts on C21 and goes goes to C2921. I'm sure there is a way to shorten this code, I just do not know how.

    I appreciate the help.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("A:A")) Is Nothing Then
        If Target.Row Mod 22 = 0 Then
            If Target.Value <> "" Then
                Select Case Target.Value
                    Case "Blank Sheet": Msgbox_Blank_Sheet
                    Case "Place Concrete": Msgbox_Concrete
                    Case "Asphalt": Msgbox_Asphalt
                    Case "Silicone Joints": Msgbox_Silicone_Joints
                    Case "Compression Seal": Msgbox_Compression_Seal
                    Case "CTB": Msgbox_CTB
                    Case "CTS": Msgbox_CTS
                    Case "Aggregate Base": Msgbox_Aggregate_Base
    
                End Select
            End If
        End If
    End If
        
    If Not Intersect(Target, Range("A:A")) Is Nothing Then
        If Target.Row Mod 42 = 0 Then
            If Target.Value <> "" Then
                Select Case Target.Value
                    Case "Blank Sheet": Msgbox_Blank_Sheet
                    Case "Place Concrete": Msgbox_Concrete
                    Case "Asphalt": Msgbox_Asphalt
                    Case "Silicone Joints": Msgbox_Silicone_Joints
                    Case "Compression Seal": Msgbox_Compression_Seal
                    Case "CTB": Msgbox_CTB
                    Case "CTS": Msgbox_CTS
                    Case "Aggregate Base": Msgbox_Aggregate_Base
    
                End Select
            End If
        End If
    End If
    
    If Not Intersect(Target, Range("A:A")) Is Nothing Then
        If Target.Row Mod 62 = 0 Then
            If Target.Value <> "" Then
                Select Case Target.Value
                    Case "Blank Sheet": Msgbox_Blank_Sheet
                    Case "Place Concrete": Msgbox_Concrete
                    Case "Asphalt": Msgbox_Asphalt
                    Case "Silicone Joints": Msgbox_Silicone_Joints
                    Case "Compression Seal": Msgbox_Compression_Seal
                    Case "CTB": Msgbox_CTB
                    Case "CTS": Msgbox_CTS
                    Case "Aggregate Base": Msgbox_Aggregate_Base
    
    and so on..............
    and so on..............

  2. #2
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Need help cleaning up my code...

    Are you asking how to loop through the code from 21 to 2921?


    DIM I as integer
    
    For i = 21 to 2291 step 20
         do work
    next i
    Please ensure you mark your thread as Solved once it is. Click here to see how.
    If a post helps, please don't forget to add to our reputation by clicking the star icon in the bottom left-hand corner of a post.

  3. #3
    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: Need help cleaning up my code...

    Hi, pasqualebaldi,

    maybe like this:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Count > 1 Then Exit Sub
    
    If Not Intersect(Target, Range("A21:A2921")) Is Nothing Then
      If Target.Row Mod 2 = 0 And Target.Row Mod 20 = 2 Then
        If Target.Value <> "" Then
          Select Case Target.Value
              Case "Blank Sheet": Msgbox_Blank_Sheet
              Case "Place Concrete": Msgbox_Concrete
              Case "Asphalt": Msgbox_Asphalt
              Case "Silicone Joints": Msgbox_Silicone_Joints
              Case "Compression Seal": Msgbox_Compression_Seal
              Case "CTB": Msgbox_CTB
              Case "CTS": Msgbox_CTS
              Case "Aggregate Base": Msgbox_Aggregate_Base
              Case Else
          End Select
        End If
      End If
    End If
    End Sub
    BTW: the code works only on Column A, where is the relation to Column C as you give up that range?

    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

  4. #4
    Forum Contributor
    Join Date
    09-09-2013
    Location
    California, USA
    MS-Off Ver
    MS 365 Subscription
    Posts
    130

    Re: Need help cleaning up my code...

    Quote Originally Posted by HaHoBe View Post
    Hi, pasqualebaldi,

    maybe like this:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Count > 1 Then Exit Sub
    
    If Not Intersect(Target, Range("A21:A2921")) Is Nothing Then
      If Target.Row Mod 2 = 0 And Target.Row Mod 20 = 2 Then
        If Target.Value <> "" Then
          Select Case Target.Value
              Case "Blank Sheet": Msgbox_Blank_Sheet
              Case "Place Concrete": Msgbox_Concrete
              Case "Asphalt": Msgbox_Asphalt
              Case "Silicone Joints": Msgbox_Silicone_Joints
              Case "Compression Seal": Msgbox_Compression_Seal
              Case "CTB": Msgbox_CTB
              Case "CTS": Msgbox_CTS
              Case "Aggregate Base": Msgbox_Aggregate_Base
              Case Else
          End Select
        End If
      End If
    End If
    End Sub
    BTW: the code works only on Column A, where is the relation to Column C as you give up that range?

    Ciao,
    Holger
    I'm sorry, when one of the cases are picked it pastes data in column C. I meant to say column A.

    Thank you very much! This worked perfectly and it is much less code than what I had!

+ 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. [SOLVED] Help with cleaning up code ?
    By james 35 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-04-2013, 06:05 AM
  2. Cleaning up this code
    By toaksie in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-25-2012, 05:50 PM
  3. Need help cleaning up VBA add-in code
    By Jarvoisier in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-03-2011, 05:45 AM
  4. Code needs cleaning up
    By hambone in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-09-2010, 10:51 AM
  5. Help Cleaning up Code
    By Lost and Looking for Help in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-24-2006, 09:30 AM

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