PC Review


Reply
Thread Tools Rate Thread

Code question

 
 
=?Utf-8?B?TWVraW5uaWs=?=
Guest
Posts: n/a
 
      31st Oct 2007
Does the worksheet have to active for this code to work?

Private Sub BtnDelete_Click()
Dim fRow As Long

On Error GoTo ender
fRow = Columns(1).Find(What:=TxtMan.Value, _
After:=Cells(5000, 1), LookIn:=xlFormulas, _
LookAT:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False).Row
Rows(fRow).Delete
Exit Sub

ender:
MsgBox "Value not found"
End Sub

Because if it does not then it doesn't work. Right now it requires it to be
and I would like to knwo how to make so it doesn't need the sheet to be
active?
 
Reply With Quote
 
 
 
 
JE McGimpsey
Guest
Posts: n/a
 
      31st Oct 2007
One way:

Private Sub BtnDelete_Click()
Dim fRow As Long

On Error GoTo ender
With Sheets("Sheet1")
fRow = .Columns(1).Find( _
What:=TxtMan.Value, _
After:=.Cells(5000, 1), _
LookIn:=xlFormulas, _
LookAT:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False).Row
.Rows(fRow).Delete
End With
Exit Sub
ender:
MsgBox "Value not found"
End Sub


In article <E177C04F-D5A1-406A-9C08-(E-Mail Removed)>,
Mekinnik <(E-Mail Removed)> wrote:

> Does the worksheet have to active for this code to work?
>
> Private Sub BtnDelete_Click()
> Dim fRow As Long
>
> On Error GoTo ender
> fRow = Columns(1).Find(What:=TxtMan.Value, _
> After:=Cells(5000, 1), LookIn:=xlFormulas, _
> LookAT:=xlWhole, SearchOrder:=xlByRows, _
> SearchDirection:=xlNext, MatchCase:=False, _
> SearchFormat:=False).Row
> Rows(fRow).Delete
> Exit Sub
>
> ender:
> MsgBox "Value not found"
> End Sub
>
> Because if it does not then it doesn't work. Right now it requires it to be
> and I would like to knwo how to make so it doesn't need the sheet to be
> active?

 
Reply With Quote
 
=?Utf-8?B?SU5UUDU2?=
Guest
Posts: n/a
 
      31st Oct 2007
To make it not care what is active, use variables

Private Sub BtnDelete_Click()
Dim fRow As Long,WS as Worksheet, rngHitCell as Range
Set WS = Workbooks("Whatever").Worksheets("Whatever")

Set rngHitCell = WS.Columns(1).Find(What:=TxtMan.Value, _
After:=Cells(5000, 1), LookIn:=xlFormulas, _
LookAT:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False)

If rngHitCell is Nothing Then
MsgBox "Value not found"
Else
WS.Rows(rngHitCell.Row).Delete
End If

End Sub

"Mekinnik" wrote:

> Does the worksheet have to active for this code to work?
>
> Private Sub BtnDelete_Click()
> Dim fRow As Long
>
> On Error GoTo ender
> fRow = Columns(1).Find(What:=TxtMan.Value, _
> After:=Cells(5000, 1), LookIn:=xlFormulas, _
> LookAT:=xlWhole, SearchOrder:=xlByRows, _
> SearchDirection:=xlNext, MatchCase:=False, _
> SearchFormat:=False).Row
> Rows(fRow).Delete
> Exit Sub
>
> ender:
> MsgBox "Value not found"
> End Sub
>
> Because if it does not then it doesn't work. Right now it requires it to be
> and I would like to knwo how to make so it doesn't need the sheet to be
> active?

 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      31st Oct 2007
When in doubt you are best off to be explicit. Include the sheet and then you
will always hit the right sheet. Then there are no worries (so long as you
have not tried to select or activate something). Try something more like
this...

Private Sub BtnDelete_Click()
Dim fRow As Long

On Error GoTo ender
fRow = Sheets("Sheet1").Columns(1).Find(What:=TxtMan.Value, _
After:=Cells(5000, 1), LookIn:=xlFormulas, _
LookAT:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False).Row
Rows(fRow).Delete
Exit Sub

ender:
MsgBox "Value not found"
End Sub
--
HTH...

Jim Thomlinson


"Mekinnik" wrote:

> Does the worksheet have to active for this code to work?
>
> Private Sub BtnDelete_Click()
> Dim fRow As Long
>
> On Error GoTo ender
> fRow = Columns(1).Find(What:=TxtMan.Value, _
> After:=Cells(5000, 1), LookIn:=xlFormulas, _
> LookAT:=xlWhole, SearchOrder:=xlByRows, _
> SearchDirection:=xlNext, MatchCase:=False, _
> SearchFormat:=False).Row
> Rows(fRow).Delete
> Exit Sub
>
> ender:
> MsgBox "Value not found"
> End Sub
>
> Because if it does not then it doesn't work. Right now it requires it to be
> and I would like to knwo how to make so it doesn't need the sheet to be
> active?

 
Reply With Quote
 
=?Utf-8?B?SU5UUDU2?=
Guest
Posts: n/a
 
      31st Oct 2007
Actually, I forgot something ....

Private Sub BtnDelete_Click()
Dim fRow As Long,WS as Worksheet, rngHitCell as Range
Set WS = Workbooks("Whatever").Worksheets("Whatever")

Set rngHitCell = WS.Columns(1).Find(What:=TxtMan.Value, _
After:=Cells(5000, 1), LookIn:=xlFormulas, _
LookAT:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False)

If rngHitCell is Nothing Then
MsgBox "Value not found"
Else
If rngHitCell.Row > 5000 then
WS.Rows(rngHitCell.Row).Delete
Else
Msgbox "Value found before Row 5000"
End If
End If

End Sub

"INTP56" wrote:

> To make it not care what is active, use variables
>
> Private Sub BtnDelete_Click()
> Dim fRow As Long,WS as Worksheet, rngHitCell as Range
> Set WS = Workbooks("Whatever").Worksheets("Whatever")
>
> Set rngHitCell = WS.Columns(1).Find(What:=TxtMan.Value, _
> After:=Cells(5000, 1), LookIn:=xlFormulas, _
> LookAT:=xlWhole, SearchOrder:=xlByRows, _
> SearchDirection:=xlNext, MatchCase:=False, _
> SearchFormat:=False)
>
> If rngHitCell is Nothing Then
> MsgBox "Value not found"
> Else
> WS.Rows(rngHitCell.Row).Delete
> End If
>
> End Sub
>
> "Mekinnik" wrote:
>
> > Does the worksheet have to active for this code to work?
> >
> > Private Sub BtnDelete_Click()
> > Dim fRow As Long
> >
> > On Error GoTo ender
> > fRow = Columns(1).Find(What:=TxtMan.Value, _
> > After:=Cells(5000, 1), LookIn:=xlFormulas, _
> > LookAT:=xlWhole, SearchOrder:=xlByRows, _
> > SearchDirection:=xlNext, MatchCase:=False, _
> > SearchFormat:=False).Row
> > Rows(fRow).Delete
> > Exit Sub
> >
> > ender:
> > MsgBox "Value not found"
> > End Sub
> >
> > Because if it does not then it doesn't work. Right now it requires it to be
> > and I would like to knwo how to make so it doesn't need the sheet to be
> > active?

 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      31st Oct 2007
Oops I missed one reference. JE's code does it. Or you could tighten things
up with this...

Private Sub BtnDelete_Click()

On Error GoTo ender
Sheets("Sheet1").Columns(1).Find(What:=TxtMan.Value, _
After:=Cells(5000, 1), LookIn:=xlFormulas, _
LookAT:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False).EntireRow.Delete
Exit Sub

ender:
MsgBox "Value not found"
End Sub
--
HTH...

Jim Thomlinson


"Jim Thomlinson" wrote:

> When in doubt you are best off to be explicit. Include the sheet and then you
> will always hit the right sheet. Then there are no worries (so long as you
> have not tried to select or activate something). Try something more like
> this...
>
> Private Sub BtnDelete_Click()
> Dim fRow As Long
>
> On Error GoTo ender
> fRow = Sheets("Sheet1").Columns(1).Find(What:=TxtMan.Value, _
> After:=Cells(5000, 1), LookIn:=xlFormulas, _
> LookAT:=xlWhole, SearchOrder:=xlByRows, _
> SearchDirection:=xlNext, MatchCase:=False, _
> SearchFormat:=False).Row
> Rows(fRow).Delete
> Exit Sub
>
> ender:
> MsgBox "Value not found"
> End Sub
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Mekinnik" wrote:
>
> > Does the worksheet have to active for this code to work?
> >
> > Private Sub BtnDelete_Click()
> > Dim fRow As Long
> >
> > On Error GoTo ender
> > fRow = Columns(1).Find(What:=TxtMan.Value, _
> > After:=Cells(5000, 1), LookIn:=xlFormulas, _
> > LookAT:=xlWhole, SearchOrder:=xlByRows, _
> > SearchDirection:=xlNext, MatchCase:=False, _
> > SearchFormat:=False).Row
> > Rows(fRow).Delete
> > Exit Sub
> >
> > ender:
> > MsgBox "Value not found"
> > End Sub
> >
> > Because if it does not then it doesn't work. Right now it requires it to be
> > and I would like to knwo how to make so it doesn't need the sheet to be
> > active?

 
Reply With Quote
 
=?Utf-8?B?SU5UUDU2?=
Guest
Posts: n/a
 
      31st Oct 2007
Just wanted to caution you about this method. If you are sure the search will
never find the value in the 1st 5000 rows, then this is fine. However, if you
are specifying After because it may be, then you could delete a row above row
5000 using this method.

Another way to avoid this is to limit the search via
..Range(.Cells(5001,1),.Cells(65536,1)).Find( ....)

Bob


"Jim Thomlinson" wrote:

> Oops I missed one reference. JE's code does it. Or you could tighten things
> up with this...
>
> Private Sub BtnDelete_Click()
>
> On Error GoTo ender
> Sheets("Sheet1").Columns(1).Find(What:=TxtMan.Value, _
> After:=Cells(5000, 1), LookIn:=xlFormulas, _
> LookAT:=xlWhole, SearchOrder:=xlByRows, _
> SearchDirection:=xlNext, MatchCase:=False, _
> SearchFormat:=False).EntireRow.Delete
> Exit Sub
>
> ender:
> MsgBox "Value not found"
> End Sub
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Jim Thomlinson" wrote:
>
> > When in doubt you are best off to be explicit. Include the sheet and then you
> > will always hit the right sheet. Then there are no worries (so long as you
> > have not tried to select or activate something). Try something more like
> > this...
> >
> > Private Sub BtnDelete_Click()
> > Dim fRow As Long
> >
> > On Error GoTo ender
> > fRow = Sheets("Sheet1").Columns(1).Find(What:=TxtMan.Value, _
> > After:=Cells(5000, 1), LookIn:=xlFormulas, _
> > LookAT:=xlWhole, SearchOrder:=xlByRows, _
> > SearchDirection:=xlNext, MatchCase:=False, _
> > SearchFormat:=False).Row
> > Rows(fRow).Delete
> > Exit Sub
> >
> > ender:
> > MsgBox "Value not found"
> > End Sub
> > --
> > HTH...
> >
> > Jim Thomlinson
> >
> >
> > "Mekinnik" wrote:
> >
> > > Does the worksheet have to active for this code to work?
> > >
> > > Private Sub BtnDelete_Click()
> > > Dim fRow As Long
> > >
> > > On Error GoTo ender
> > > fRow = Columns(1).Find(What:=TxtMan.Value, _
> > > After:=Cells(5000, 1), LookIn:=xlFormulas, _
> > > LookAT:=xlWhole, SearchOrder:=xlByRows, _
> > > SearchDirection:=xlNext, MatchCase:=False, _
> > > SearchFormat:=False).Row
> > > Rows(fRow).Delete
> > > Exit Sub
> > >
> > > ender:
> > > MsgBox "Value not found"
> > > End Sub
> > >
> > > Because if it does not then it doesn't work. Right now it requires it to be
> > > and I would like to knwo how to make so it doesn't need the sheet to be
> > > active?

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Newbie question - code attached to spreadsheet vs code in module DomThePom Microsoft Excel Programming 1 14th Apr 2008 03:35 PM
newbie question: Is WMI code must be written in VB code? jzhang0502@gmail.com Windows XP WMI 2 5th Apr 2005 01:52 AM
Another GUID Question - Code Project code Glyn Meek Microsoft Dot NET Compact Framework 7 16th Mar 2005 05:35 PM
Reverse Zip Code Lookup Code Question! Dave Elliott Microsoft Access Forms 1 15th Nov 2004 07:32 PM
a question about calling unmanged code from managed code chaor Microsoft C# .NET 2 16th Jun 2004 05:24 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:19 AM.