Excel vba code

RJ1969

Board Regular
Joined
Sep 13, 2014
Messages
76
Hello All,<o:p></o:p>
I have Sheet1 , Sheet2 and Sheet3 in a workbook. <o:p></o:p>
Can anyone edit the code vba code below to make it work justonly SHEET1 and not other sheets. <o:p></o:p>
Thank you.


PubliccurSheet<o:p></o:p>

PrivateSub btnsearch1_Click()<o:p></o:p>

Dimi, LastRow, caseFound As Boolean<o:p></o:p>

Application.ScreenUpdating= False<o:p></o:p>

cassFound= False<o:p></o:p>

IfMe.txtSrch = "" Then<o:p></o:p>

ExitSub<o:p></o:p>

EndIf<o:p></o:p>

curSheet= ActiveSheet.Name<o:p></o:p>

'hideLocation 2 and 3 fields if next case does not have multiple locations<o:p></o:p>

Me.txtLoc2.Visible= False<o:p></o:p>

Me.txtLoc2.Value= ""<o:p></o:p>

Me.lblLoc2.Visible= False<o:p></o:p>

Me.txtLoc3.Visible= False<o:p></o:p>

Me.txtLoc3.Value= ""<o:p></o:p>

Me.lblLoc3.Visible= False<o:p></o:p>

ForEach ws In Sheets<o:p></o:p>

ws.Activate<o:p></o:p>

<o:p></o:p>

'set LastRow for current worksheet beingsearched<o:p></o:p>

LastRow= ws.Range("A" & Rows.count).End(xlUp).Row 'set last row number for current worksheet<o:p></o:p>

'loop through column C of worksheet beingsearched and evaluated case number match<o:p></o:p>

Fori = 2 To LastRow<o:p></o:p>

IfUCase(Cells(i, "A").Value) = UCase(Me.txtSrch) Then 'convert to upper case for matching. VBA is case sensitive<o:p></o:p>

caseFound= True<o:p></o:p>

'Me.txtDname= ws.Range("D" & i).Value<o:p></o:p>

IfMe.txtLoc.Value = "" Then<o:p></o:p>

Me.txtLoc= ws.Range("B" & i).Value<o:p></o:p>

EndIf<o:p></o:p>

'if a second location exists for casenumber, show Loc2 label and text box and enter the location<o:p></o:p>

IfMe.txtLoc2.Value = "" And ws.Range("B" & i).Value<> Me.txtLoc Then<o:p></o:p>

Me.txtLoc2.Visible= True<o:p></o:p>

Me.lblLoc2.Visible= True<o:p></o:p>

Me.txtLoc2.Value= ws.Range("B" & i).Value<o:p></o:p>

EndIf<o:p></o:p>

'if a third location exists for case number,show Loc3 label and text box and enter the location<o:p></o:p>

IfMe.txtLoc3.Value = "" And Me.txtLoc2.Value <> "" Andws.Range("B" & i).Value <> Me.txtLoc2 Andws.Range("B" & i).Value <> Me.txtLoc Then<o:p></o:p>

Me.txtLoc3.Visible= True<o:p></o:p>

Me.lblLoc3.Visible= True<o:p></o:p>

Me.txtLoc3.Value= ws.Range("B" & i).Value<o:p></o:p>

EndIf<o:p></o:p>

EndIf<o:p></o:p>

Nexti<o:p></o:p>

Nextws<o:p></o:p>

Sheets(curSheet).Activate<o:p></o:p>

'Me.txtDname.SetFocus<o:p></o:p>

IfcaseFound = False Then<o:p></o:p>

MsgBox"No record found for Case " & Me.txtSrch.Value, vbInformation,"Not Found"<o:p></o:p>

EndIf<o:p></o:p>

EndSub<o:p></o:p>

<o:p> </o:p>

PrivateSub txtSrch_Enter()<o:p></o:p>

Me.txtSrch.Value= ""<o:p></o:p>

Me.txtSrch.BackColor= &HFFFF& 'added for colortabuserform<o:p></o:p>

'Me.txtDname.Value= ""<o:p></o:p>

Me.txtLoc.Value= ""<o:p></o:p>

Me.txtLoc2.Value= ""<o:p></o:p>

Me.txtLoc3.Value= ""<o:p></o:p>

EndSub<o:p></o:p>

<o:p> </o:p>

PrivateSub UserForm_Activate()<o:p></o:p>

Me.lblLoc2.Visible= False<o:p></o:p>

Me.txtLoc2.Visible= False<o:p></o:p>

Me.lblLoc3.Visible= False<o:p></o:p>
Me.txtLoc3.Visible



 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
your code did not transfer well. You should use CODE delimters.
but,
remove the line: For Each ws In Sheets
and its: NEXT ws

replace :'For Each ws In Sheets
with SHEETS("SHEET1").ACTIVATE
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top