-
Jan 25th, 2011, 11:17 AM
#1
Thread Starter
Lively Member
What this Private Sub does?
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cellContents As String
Dim valLength As Integer
cellContents = Trim(Str(Val(Target.Value)))
valLength = Len(cellContents)
If valLength <> 3 Then
MsgBox ("Please enter a 3 digit area code.")
Cells(9, "C").Select
Else
Cells(9, "C").Value = cellContents
Cells(9, "D").Select
End If
End Sub
-
Jan 25th, 2011, 12:39 PM
#2
Re: What this Private Sub does?
It's an event handler. When the worksheet changes, it runs. It checks a cell and makes sure that the contents has a length of three. No more, no less. Thou shalt not count one, nor shall thou count two unless proceeding directly to three. Five is right out!
-tg
-
Jan 29th, 2011, 08:45 PM
#3
Fanatic Member
Re: What this Private Sub does?
bravo! bravo! lol i love monty python
-
Jan 29th, 2011, 11:11 PM
#4
Re: What this Private Sub does?
I would also suggest adding the .EnableEvents Line
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cellContents As String
Dim valLength As Integer
cellContents = Trim(Str(Val(Target.Value)))
valLength = Len(cellContents)
Application.EnableEvents = False
If valLength <> 3 Then
MsgBox ("Please enter a 3 digit area code.")
Cells(9, "C").Select
Else
Cells(9, "C").Value = cellContents
Cells(9, "D").Select
End If
Application.EnableEvents = True
End Sub
Sid
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|