Help cleaning up some code if you please

EssKayKay

Active Member
Joined
Jan 5, 2003
Messages
354
Office Version
  1. 2007
Platform
  1. Windows
Hello,
This is no biggie here - really just wondering if there is a more efficient way to code this (my programming skills are very limited to say the least).

I have three routines that basically do the same thing to three sets of cells. I simply copied the code from the first set; then edit it for the next two. It appears to be doing what I want. However, I suspect there is a better way to achieve this.

I don’t expect anyone to spend much time on this but if it looks easy enough to clean up, I would be interested.

Here’s my code –
VBA Code:
‘- - - 1st One-Time - - -
     If Range("K8:K9").Locked = False And Range("K9") = "" Then
          MsgBox " Missing One-Time Amount" & vbNewLine & " Re-enter Payment details.", , " One-Time Pmt."
          Range("K8,K9").Select
          Selection.ClearContents
          Range("K9").Select
          Selection.Locked = True
          With Selection.Interior
              .Color = 15984868
         End With
         Range("K8").Select
         Exit Sub
End If


‘- - - 2nd One-Time - - -
     If Range("K10:K11").Locked = False And Range("K11") = "" Then
          MsgBox " Missing One-Time Amount" & vbNewLine & " Re-enter Payment details.", , " One-Time Pmt."
          Range("K10,K11").Select
          Selection.ClearContents
          Range("K11").Select
          Selection.Locked = True
          With Selection.Interior
              .Color = 15984868
          End With
          Range("K10").Select
          Exit Sub
     End If

‘- - - 3rd One-Time - - -
     If Range("K12:K13").Locked = False And Range("K13") = "" Then
          MsgBox " Missing One-Time Amount" & vbNewLine & " Re-enter Payment details.", , " One-Time Pmt."
          Range("K12,K13").Select
          Selection.ClearContents
          Range("K13").Select
          Selection.Locked = True
          With Selection.Interior
              .Color = 15984868
          End With
          Range("K12").Select
     Exit Sub
End If

Thanks for viewing,
Steve
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try:
VBA Code:
Sub Test()
    Application.ScreenUpdating = False
    Dim x As Long
    For x = 8 To 12 Step 2
        If Range("K" & x & ":K" & x + 1).Locked = False And Range("K" & x + 1) = "" Then
            MsgBox " Missing One-Time Amount" & vbNewLine & " Re-enter Payment details.", , " One-Time Pmt."
            Range("K" & x & ":K" & x + 1).ClearContents
            With Range("K" & x + 1)
                .Locked = True
                .Color = 15984868
            End With
        End If
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
I had a similar solution to mumps (and I stole a few simplifications as well), but it allows you to set the first row (myRowBase). I did add some extra variables (myCell1 and myCell2) just for clarity and troubleshooting.

VBA Code:
Sub test()
Dim myRowBase As Integer
Dim myCell1 As String
Dim myCell2 As String

myRowBase = 8
For i = 0 To 2
    myCell1 = "K" & myRowBase + 2 * i
    myCell2 = "K" & myRowBase + 2 * i + 1
    If Range(myCell1 & ":" & myCell2).Locked = False And Range(myCell2) = "" Then
        MsgBox " Missing One-Time Amount" & vbNewLine & " Re-enter Payment details.", , " One-Time Pmt."
        Range(myCell1 & ":" & myCell2).ClearContents
        With Range(myCell2)
            .Locked = True
            .Interior.Color = 15984868
        End With
        Range(myCell1).Select
        Exit Sub
    End If
Next i

End Sub
 
Upvote 0
Thank you Mumps for your quick response. I placed your code in my worksheet. The routine is Triggered via a command button. When I clicked on it I received a Run-time error ‘438’. It hangs on the .Color = 15984868.

Here are the two messages:

Error1.jpg
Error2.jpg
Thanks,
Steve
 
Upvote 0
Instead of
VBA Code:
.Color = 15984868
try
VBA Code:
.Interior.Color = 15984868
 
Upvote 0
OOPS. . .
I did notice one small issue. I have a bit more testing, particularly for the third run. I will get back to you on this (I have another appointment this afternoon).

Once again, thank you - much appreciated,
Steve K.
 
Upvote 0
We (read you) are very close. However, this process is the first of other operations taking place in the Sub (cmdPmtMade_Click).

What happens is if the second cell is left blank (i.e., K9, K11, or K13), after the “Missing One-Time” Message appears, the remainder of the Sub is processes. What I was hoping for is if the message appears, then stop the remainder of the Sub and return to the first cell in the set (i.e., K8, K10, or K12).

Thank you all,
Steve K
 
Upvote 0
Try:
VBA Code:
Sub Test()
    Application.ScreenUpdating = False
    Dim x As Long
    For x = 8 To 12 Step 2
        If Range("K" & x & ":K" & x + 1).Locked = False And Range("K" & x + 1) = "" Then
            MsgBox " Missing One-Time Amount" & vbNewLine & " Re-enter Payment details.", , " One-Time Pmt."
            Range("K" & x).Select
            Exit Sub
        End If
        Range("K" & x & ":K" & x + 1).ClearContents
        With Range("K" & x + 1)
            .Locked = True
            .Interior.Color = 15984868
        End With
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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