|
-
Feb 24th, 2010, 11:26 AM
#1
Thread Starter
Junior Member
-
Feb 24th, 2010, 01:36 PM
#2
Re: Excel VBA - Worksheet_Change Function
I would start with formatting the code so its easier to read.
Code:
If Not Intersect(Target, Range("C5:C31")) Is Nothing Then
Select Case Target.Cells.Value
Case "Sent"
MsgBox ("Input Time")
End Select
Else
Exit Sub
End If
But when you look at it you can see that a select case is not needed since there is only one case statement.
Code:
If Not Intersect(Target, Range("C5:C31")) Is Nothing Then
If Target.Cells.Value = "Sent" Then
MsgBox ("Input Time")
End If
Else
Exit Sub
End If
What do you mean "whole worksheet code"? Do you mean if you place it in the "ThisWorkbook" class?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 25th, 2010, 07:12 AM
#3
Thread Starter
Junior Member
Re: Excel VBA - Worksheet_Change Function
Hi,
thanks for the amended code.
I meant to put the worksheet_change code into the active worksheet, not the 'this workbook'.
When i change the cell value in C5:C31 to 'sent', the code works perfectly, and that's what i wanted.
But when i check the box to clear the content of row(s), with the 'clear' command button, a debug window pops out saying [run-time error'13': type mismatch], and then highlight the code [If Target.Cells.Value = "Sent" Then].
I am not sure if the command button code contains anything that crash with the 'worksheet change code'?
thx
xwxc
-
Feb 25th, 2010, 03:33 PM
#4
Re: Excel VBA - Worksheet_Change Function
problem arises when target is multiple cells, limit code to only work if target is single cell
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 25th, 2010, 05:11 PM
#5
Re: Excel VBA - Worksheet_Change Function
Code:
If Not Intersect(Target(1), Range("C5:C31")) Is Nothing Then
If Target(1).Value = "Sent" Then
MsgBox ("Input Time")
End If
End If
-
Feb 26th, 2010, 12:42 PM
#6
Thread Starter
Junior Member
Re: Excel VBA - Worksheet_Change Function
Hi, many thanks, the code works perfectly.
Do you mind to explain to me why the additional of #1 next to "target" makes such a different? I tried to do a search but couldn't be satisfied.
Appreciate your assistance
Thx,
xwxc
-
Feb 28th, 2010, 12:25 AM
#7
Re: Excel VBA - Worksheet_Change Function
Target is a range, it may have only one cell (such as when user change a cell in column C) or a range of cells (such as when the code clear a row).
When Target is a single cell, Target.Value returns a single value that can be used to compare with string value "Sent" without problem.
When Target has two or more cells, Target.Value returns an array that CANNOT be used to compare with string value "Sent". This is why you hit the error on clearing.
The use of Target(1) to make sure that you test only a single cell, that is the first cell in the Target range.
Target(1) is the shortcut of Target.Cells(1,1).
In more general cases, such as when user copy and paste to multiple cells at the same time then we need to test every cell in the Target range (but need to prompt only one MsgBox):
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim CheckRange As Range
Dim aCell As Range
Set CheckRange = Intersect(Target, Range("C5:C31"))
If Not CheckRange Is Nothing Then
For Each aCell In CheckRange
If aCell.Value = "Sent" Then
MsgBox ("Input Time")
Exit For
End If
Next
End If
End Sub
-
Mar 3rd, 2010, 08:57 AM
#8
Thread Starter
Junior Member
Re: Excel VBA - Worksheet_Change Function
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
|