VBA Novice, need help breaking code into smaller pieces

PntdBlu

New Member
Joined
Sep 27, 2017
Messages
2
I am a true beginner with editing code, but I have a workbook that has many repeating formulas and I got to a point when building the file that it no longer works. In trying to resolve the issue, what I am assuming is that I need to break up the following code into smaller pieces:

-------------------------------------------------------------------
Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
If Target.Address = "$K$6" Then
If IsNumeric(Target) Then
Application.EnableEvents = False
Target.Offset(, 1) = Target.Offset(, 1) + Target
Application.EnableEvents = True
End If
End If
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
If Target.Address = "$K$7" Then
If IsNumeric(Target) Then
Application.EnableEvents = False
Target.Offset(, 1) = Target.Offset(, 1) + Target.Value
Application.EnableEvents = True
End If
End If
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
If Target.Address = "$K$8" Then
If IsNumeric(Target) Then
Application.EnableEvents = False
Target.Offset(, 1) = Target.Offset(, 1) + Target.Value
Application.EnableEvents = True
End If
End If
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
If Target.Address = "$K$9" Then
If IsNumeric(Target) Then
Application.EnableEvents = False
Target.Offset(, 1) = Target.Offset(, 1) + Target.Value
Application.EnableEvents = True
End If
End If

...and this code goes on and on and then moves to another column, with the same formula, etc.
----------------------------------------

Is it possible to revise the Target.Address to a range? (i.e. $K$9:$K$25)

I have tried to format the code into smaller increments, but then I get other errors.

Any help is appreciated.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
If the range checked simply increases by one, you can probably just loop through it.

Something like:

Code:
Dim x As Long

For x = 9 To 25


    If Target.Address = "$K$" & x Then
        If IsNumeric(Target) Then
            Application.EnableEvents = False
            Target.Offset(, 1) = Target.Offset(, 1) + Target.Value
            Application.EnableEvents = True
        End If
    End If


Next x
 
Upvote 0
I hate that it took me this long to reply, but I literally FINALLY just got this worksheet updated and working! It took me a long time to really digest your solution and apply it. Thank you so much for your help!
 
Upvote 0
An alternative approach, without using a loop would be
Code:
Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
    If Not Intersect(Target, Range("K6:K25")) Then
        If IsNumeric(Target) Then
            Application.EnableEvents = False
            Target.Offset(, 1) = Target.Offset(, 1) + Target
            Application.EnableEvents = True
        End If
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top