Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 33
Default Sub CommandButton1 help

I have a workbook with a number of sheets, on each sheet there is three
columns Date Amount Po Number. This vba is supposed to only to look in
column three (Po Number). Po Numbers have six digits. The problem I have with
this
vba is if you only input 2 digits it will look over the whole workbook
looking in nearly every cell, as there is over 600,000 records. This can take
a long time to run. Is there anyway I could adapt the Macro or escape the
program if I input less than 6 digits.

Thanks

Sub CommandButton1_Click()
Dim WhatToFind As Variant
Dim Found As Boolean
Found = False
WhatToFind = Application.InputBox("INPUT PO NUMBER ?", "Search", , 100,
100, , , 2)
If WhatToFind < "" And Not WhatToFind = False Then
For Each oSheet In ActiveWorkbook.Worksheets
oSheet.Activate
oSheet.[c3].Activate
Set Firstcell = Columns(3).Find(What:=WhatToFind,
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows,
SearchDirection:=xlNext, MatchCase:=False)

If Not Firstcell Is Nothing Then
Firstcell.Activate
Found = True
MsgBox ("PO CASHED!" & Firstcell.Text)

Set NextCell = Columns(3).FindNext(After:=ActiveCell)
While (Not NextCell Is Nothing) And (Not
NextCell.Address = Firstcell.Address)

If Not NextCell.Address = Firstcell.Address Then
NextCell.Activate
Found = True
MsgBox ("PO CASHED!" & NextCell.Text)
Set NextCell = Cells.FindNext(After:=ActiveCell)
End If
Wend
End If
Set NextCell = Nothing
Set Firstcell = Nothing
Next oSheet
If Not Found Then
MsgBox ("PO NOT FOUND")
End If
End If
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 2,202
Default Sub CommandButton1 help

This is your first If-Then statement...

If WhatToFind < "" And Not WhatToFind = False Then


Change it to this...

If WhatToFind Like "######" And Not WhatToFind = False Then

although I am not exactly sure what the second expression is supposed to be
doing for you... the only time (Not WhatToFind = False) will be true is if
WhatToFind equals -1. The new first expression I gave you will only be true
if WhatToFind is composed of exactly 6 digits... I would think that test all
by itself would be enough for your requirements.

Rick


"Monty" wrote in message
...
I have a workbook with a number of sheets, on each sheet there is three
columns Date Amount Po Number. This vba is supposed to only to look
in
column three (Po Number). Po Numbers have six digits. The problem I have
with
this
vba is if you only input 2 digits it will look over the whole workbook
looking in nearly every cell, as there is over 600,000 records. This can
take
a long time to run. Is there anyway I could adapt the Macro or escape the
program if I input less than 6 digits.

Thanks

Sub CommandButton1_Click()
Dim WhatToFind As Variant
Dim Found As Boolean
Found = False
WhatToFind = Application.InputBox("INPUT PO NUMBER ?", "Search", , 100,
100, , , 2)
If WhatToFind < "" And Not WhatToFind = False Then
For Each oSheet In ActiveWorkbook.Worksheets
oSheet.Activate
oSheet.[c3].Activate
Set Firstcell = Columns(3).Find(What:=WhatToFind,
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows,
SearchDirection:=xlNext, MatchCase:=False)

If Not Firstcell Is Nothing Then
Firstcell.Activate
Found = True
MsgBox ("PO CASHED!" & Firstcell.Text)

Set NextCell = Columns(3).FindNext(After:=ActiveCell)
While (Not NextCell Is Nothing) And (Not
NextCell.Address = Firstcell.Address)

If Not NextCell.Address = Firstcell.Address Then
NextCell.Activate
Found = True
MsgBox ("PO CASHED!" & NextCell.Text)
Set NextCell = Cells.FindNext(After:=ActiveCell)
End If
Wend
End If
Set NextCell = Nothing
Set Firstcell = Nothing
Next oSheet
If Not Found Then
MsgBox ("PO NOT FOUND")
End If
End If
End Sub


  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 33
Default Sub CommandButton1 help

Cheers

This worked a dream.

Monty

"Rick Rothstein (MVP - VB)" wrote:

This is your first If-Then statement...

If WhatToFind < "" And Not WhatToFind = False Then


Change it to this...

If WhatToFind Like "######" And Not WhatToFind = False Then

although I am not exactly sure what the second expression is supposed to be
doing for you... the only time (Not WhatToFind = False) will be true is if
WhatToFind equals -1. The new first expression I gave you will only be true
if WhatToFind is composed of exactly 6 digits... I would think that test all
by itself would be enough for your requirements.

Rick


"Monty" wrote in message
...
I have a workbook with a number of sheets, on each sheet there is three
columns Date Amount Po Number. This vba is supposed to only to look
in
column three (Po Number). Po Numbers have six digits. The problem I have
with
this
vba is if you only input 2 digits it will look over the whole workbook
looking in nearly every cell, as there is over 600,000 records. This can
take
a long time to run. Is there anyway I could adapt the Macro or escape the
program if I input less than 6 digits.

Thanks

Sub CommandButton1_Click()
Dim WhatToFind As Variant
Dim Found As Boolean
Found = False
WhatToFind = Application.InputBox("INPUT PO NUMBER ?", "Search", , 100,
100, , , 2)
If WhatToFind < "" And Not WhatToFind = False Then
For Each oSheet In ActiveWorkbook.Worksheets
oSheet.Activate
oSheet.[c3].Activate
Set Firstcell = Columns(3).Find(What:=WhatToFind,
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows,
SearchDirection:=xlNext, MatchCase:=False)

If Not Firstcell Is Nothing Then
Firstcell.Activate
Found = True
MsgBox ("PO CASHED!" & Firstcell.Text)

Set NextCell = Columns(3).FindNext(After:=ActiveCell)
While (Not NextCell Is Nothing) And (Not
NextCell.Address = Firstcell.Address)

If Not NextCell.Address = Firstcell.Address Then
NextCell.Activate
Found = True
MsgBox ("PO CASHED!" & NextCell.Text)
Set NextCell = Cells.FindNext(After:=ActiveCell)
End If
Wend
End If
Set NextCell = Nothing
Set Firstcell = Nothing
Next oSheet
If Not Found Then
MsgBox ("PO NOT FOUND")
End If
End If
End Sub



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
How do I label a command button so it doesn't say CommandButton1? [email protected] Excel Discussion (Misc queries) 2 December 10th 05 12:40 AM


All times are GMT +1. The time now is 02:26 PM.

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

About Us

"It's about Microsoft Excel"