#1   Report Post  
Posted to microsoft.public.excel.misc
Bob Bob is offline
external usenet poster
 
Posts: 972
Default Goto code

I am using the following code to Goto the cell entered in cell "D3". It works
if there is a perfect match. If they do not match the code stops on line 3
with an error. What should be added to the code so that it will work when not
found and stay in cell "D3"? (With a pop of box "Number not Found" or
something like that)

The column has numbers like 4512 and 4512-1 and 4512-01 to match.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address < "$D$3" Then Exit Sub
Columns(2).Find(Target).Select
End Sub

Thank for your help,

Bob
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Goto code

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim res As Variant
If Target.Address < "$D$3" Then Exit Sub
res = Application.Match(Target.Value, Me.Range("B:B"), 0)
If IsError(res) Then
'stay put
'beep '???
Else
Me.Range("B:B")(res).Select
End If
End Sub


Bob wrote:

I am using the following code to Goto the cell entered in cell "D3". It works
if there is a perfect match. If they do not match the code stops on line 3
with an error. What should be added to the code so that it will work when not
found and stay in cell "D3"? (With a pop of box "Number not Found" or
something like that)

The column has numbers like 4512 and 4512-1 and 4512-01 to match.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address < "$D$3" Then Exit Sub
Columns(2).Find(Target).Select
End Sub

Thank for your help,

Bob


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.misc
Bob Bob is offline
external usenet poster
 
Posts: 972
Default Goto code

Dave, I am new to VB. I copied the code and getting an error with "Option
Explicit" if I remove it from the code the Goto works with a match, it does
not work if there is not a match and moves out of the cell. What may be wrong?

"Dave Peterson" wrote:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim res As Variant
If Target.Address < "$D$3" Then Exit Sub
res = Application.Match(Target.Value, Me.Range("B:B"), 0)
If IsError(res) Then
'stay put
'beep '???
Else
Me.Range("B:B")(res).Select
End If
End Sub


Bob wrote:

I am using the following code to Goto the cell entered in cell "D3". It works
if there is a perfect match. If they do not match the code stops on line 3
with an error. What should be added to the code so that it will work when not
found and stay in cell "D3"? (With a pop of box "Number not Found" or
something like that)

The column has numbers like 4512 and 4512-1 and 4512-01 to match.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address < "$D$3" Then Exit Sub
Columns(2).Find(Target).Select
End Sub

Thank for your help,

Bob


--

Dave Peterson

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Goto code

First the easy answer...

"Option Explicit" is a directive to the compiler that tells it that you as the
programmer are going to declare your variables. This line goes at the top of
your module. All the subs and functions in that module will have to have all
the variables declared.

Then if you make a typing mistake:

Dim lCtr as long
lCtr = 1Ctr + 1

You'll get an error since a mistyped variable wasn't Dimmed.

Did you see the error in my example:
One of those lCtr's is (ELL-ctr) and the other is (one)-ctr.

Without that "option explicit" the top, excel will just figure you know what
you're doing and meant to do that. But your code (probably) won't run as
intended.

These kinds of typos can be difficult to find. Forcing yourself to declare the
variables will ease this kind of debugging problem.

And a nice thing about declaring variables is you can use VBA's intellisense.
If you do:

dim Wks as worksheet
set wks = worksheets("sheet1")

then later type

Wks.
(note the dot)
You'll see a list of all the possible methods and properties that go with that
variable type (worksheet in my example).

And another nice thing...

If you have a variable like this:

Dim myCounterOfReceiptsPaidOnTime as long

You can type

mycount
and hit ctrl-spacebar.
You'll see a list of all the stuff that starts with those characters and you can
pick the one you want (or it's only this variable, it'll autocomplete for you).

And I think most people who spend anytime writing macros have this option turned
on automatically for all new modules (you'll still have to type it for existing
modules).

Inside the VBE
Tools|Options|Editor Tab|Check "Require Variable Declaration"

It's one of those things that may seem like too much work at the beginning, but
it really makes life easier when you're typing and debugging.


==============

I like to leave the cursor in the cell that I just changed. I use this setting:
Tools|Options|edit|
I leave the "Move selection after enter" unchecked.

I like this for all my editting, though.

If you don't want to change this setting, you can have your code go back to that
D3 cell.

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim res As Variant
If Target.Address < "$D$3" Then Exit Sub
res = Application.Match(Target.Value, Me.Range("B:B"), 0)
If IsError(res) Then
'stay put
'beep '???
Application.EnableEvents = False
Target.Select
Application.EnableEvents = True
Else
Me.Range("B:B")(res).Select
End If
End Sub


Bob wrote:

Dave, I am new to VB. I copied the code and getting an error with "Option
Explicit" if I remove it from the code the Goto works with a match, it does
not work if there is not a match and moves out of the cell. What may be wrong?

"Dave Peterson" wrote:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim res As Variant
If Target.Address < "$D$3" Then Exit Sub
res = Application.Match(Target.Value, Me.Range("B:B"), 0)
If IsError(res) Then
'stay put
'beep '???
Else
Me.Range("B:B")(res).Select
End If
End Sub


Bob wrote:

I am using the following code to Goto the cell entered in cell "D3". It works
if there is a perfect match. If they do not match the code stops on line 3
with an error. What should be added to the code so that it will work when not
found and stay in cell "D3"? (With a pop of box "Number not Found" or
something like that)

The column has numbers like 4512 and 4512-1 and 4512-01 to match.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address < "$D$3" Then Exit Sub
Columns(2).Find(Target).Select
End Sub

Thank for your help,

Bob


--

Dave Peterson


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.misc
Bob Bob is offline
external usenet poster
 
Posts: 972
Default Goto code

Works Great
Thanks for your help,
Bob

"Dave Peterson" wrote:

First the easy answer...

"Option Explicit" is a directive to the compiler that tells it that you as the
programmer are going to declare your variables. This line goes at the top of
your module. All the subs and functions in that module will have to have all
the variables declared.

Then if you make a typing mistake:

Dim lCtr as long
lCtr = 1Ctr + 1

You'll get an error since a mistyped variable wasn't Dimmed.

Did you see the error in my example:
One of those lCtr's is (ELL-ctr) and the other is (one)-ctr.

Without that "option explicit" the top, excel will just figure you know what
you're doing and meant to do that. But your code (probably) won't run as
intended.

These kinds of typos can be difficult to find. Forcing yourself to declare the
variables will ease this kind of debugging problem.

And a nice thing about declaring variables is you can use VBA's intellisense.
If you do:

dim Wks as worksheet
set wks = worksheets("sheet1")

then later type

Wks.
(note the dot)
You'll see a list of all the possible methods and properties that go with that
variable type (worksheet in my example).

And another nice thing...

If you have a variable like this:

Dim myCounterOfReceiptsPaidOnTime as long

You can type

mycount
and hit ctrl-spacebar.
You'll see a list of all the stuff that starts with those characters and you can
pick the one you want (or it's only this variable, it'll autocomplete for you).

And I think most people who spend anytime writing macros have this option turned
on automatically for all new modules (you'll still have to type it for existing
modules).

Inside the VBE
Tools|Options|Editor Tab|Check "Require Variable Declaration"

It's one of those things that may seem like too much work at the beginning, but
it really makes life easier when you're typing and debugging.


==============

I like to leave the cursor in the cell that I just changed. I use this setting:
Tools|Options|edit|
I leave the "Move selection after enter" unchecked.

I like this for all my editting, though.

If you don't want to change this setting, you can have your code go back to that
D3 cell.

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim res As Variant
If Target.Address < "$D$3" Then Exit Sub
res = Application.Match(Target.Value, Me.Range("B:B"), 0)
If IsError(res) Then
'stay put
'beep '???
Application.EnableEvents = False
Target.Select
Application.EnableEvents = True
Else
Me.Range("B:B")(res).Select
End If
End Sub


Bob wrote:

Dave, I am new to VB. I copied the code and getting an error with "Option
Explicit" if I remove it from the code the Goto works with a match, it does
not work if there is not a match and moves out of the cell. What may be wrong?

"Dave Peterson" wrote:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim res As Variant
If Target.Address < "$D$3" Then Exit Sub
res = Application.Match(Target.Value, Me.Range("B:B"), 0)
If IsError(res) Then
'stay put
'beep '???
Else
Me.Range("B:B")(res).Select
End If
End Sub


Bob wrote:

I am using the following code to Goto the cell entered in cell "D3". It works
if there is a perfect match. If they do not match the code stops on line 3
with an error. What should be added to the code so that it will work when not
found and stay in cell "D3"? (With a pop of box "Number not Found" or
something like that)

The column has numbers like 4512 and 4512-1 and 4512-01 to match.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address < "$D$3" Then Exit Sub
Columns(2).Find(Target).Select
End Sub

Thank for your help,

Bob

--

Dave Peterson


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can someone help me put psuedo code into actual excel macro?? bxc2739 Excel Discussion (Misc queries) 1 April 22nd 06 02:58 PM
Help, need to speed up this macro retseort Excel Discussion (Misc queries) 3 January 12th 06 12:33 PM
Conform a total to a list of results? xmaveric Excel Discussion (Misc queries) 1 August 21st 05 07:22 PM
Macro for changing text to Proper Case JPriest Excel Worksheet Functions 3 August 8th 05 09:31 PM
Using other workbooks.. DavidMunday Excel Worksheet Functions 2 July 1st 05 07:35 AM


All times are GMT +1. The time now is 12:40 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"