Tidy/Shorten my code?

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi ,

I have this VBA code that works fine but I was wondering if there was a way of shortening it so it looks better?

VBA Code:
'FOR LINE J SLIPSHEET HOLDER
Application.EnableEvents = False
If Target.Column = 5 And Range("E" & Selection.Row) = "J" Or Range("E" & Selection.Row) = "j" And Range("F" & Selection.Row) = 3 _
Or Target.Column = 5 And Range("E" & Selection.Row) = "J" Or Range("E" & Selection.Row) = "j" And Range("F" & Selection.Row) = 12 _
Or Target.Column = 5 And Range("E" & Selection.Row) = "J" Or Range("E" & Selection.Row) = "j" And Range("F" & Selection.Row) = 16 _
Or Target.Column = 5 And Range("E" & Selection.Row) = "J" Or Range("E" & Selection.Row) = "j" And Range("F" & Selection.Row) = 19 Then
MsgBox "Please make sure the slip sheet holder is locked in the correct position."
Application.EnableEvents = True
End If

Also in the near future I will be need this code to also work on Lines N, L and K. So if it's possible to add these lines in to a new code, then that would be great.

Any help would be appreciated.

Thanks

Dan
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
A more readable code could look like below. Not sure what you're doing, since you are using the Selection Range object. In most cases it would return the same Range object as Target. Anyway, try this.
VBA Code:
If Target.Column = 5 Then
    If UCase(Range("E" & Selection.Row)) = "J" Then
        Select Case Range("F" & Selection.Row).Value
        Case 3, 12, 16, 19
            MsgBox "Please make sure the slip sheet holder is locked in the correct position."
        End Select
    End If
End If
 
Upvote 0
Solution
Hi

Try this:

VBA Code:
Dim MyRows As String
MyRows = "|3|12|16|19|"

If Target.Column = 5 And LCase(Range("E" & Target.Row).Value) = "j" And InStrRev(MyRows, "|" & Range("F" & Target.Row).Value & "|") Then
MsgBox "Please make sure the slip sheet holder is locked in the correct position."
End If
 
Upvote 0
Thank you GWteB & dave3009 for helping me on my code.

I am going to use GWteB code just because I understand more than dave3009's code.

Thanks

Dan
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,742
Members
448,989
Latest member
mariah3

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