Work sheet find

Nuwan

New Member
Joined
Nov 28, 2006
Messages
29
Hi,
I want to find and compare a work sheet with my given name.

Dim NewSheetName as String
For Each Ws.Name In ThisWorkbook.Worksheets
Set Find1 = .Find(What:=NewSheetName, MatchCase:=True)
If Not Find1 Is Nothing Then
'-----------------------------------True condition

Else
'-----------------------------------False condition
Endif



above macro not working.............

help me

Nuwan
 
Last edited:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hello Nuwan,

To compare the worksheets' names to a specific name...
Code:
Sub Macro1()
  Dim NewSheetName As string
  Dim Wks As Worksheet

    For Each Wks In Worksheets
      If Wks.Name = NewSheetName Then
      
      Else
      
      End If
    Next Wks
End Sub
Sincerely,
Leith Ross
 
Upvote 0
Hi Leith,
This is different.

I want to search all sheets at once and if not exists do my else job
this is looping until "worksheet name list" end in my another sheet


Dim TT as integer
Dim NewSheetName as String


TT=1
do
NewSheetName=sheet1.range("A" & TT).text

For Each Ws.Name In ThisWorkbook.Worksheets
Set Find1 = .Find(What:=NewSheetName, MatchCase:=True)
If Not Find1 Is Nothing Then
'-----------------------------------True condition

Else
'-----------------------------------False condition
Endif
Next Ws
TT=TT+1
loop until sheet1.range("A" & TT).text=""


above macro not working.............

Dim NewSheetName as String
With ThisWorkbook.Worksheets(Ws.Name)
Set Find1 = .Find(What:=NewSheetName, MatchCase:=True)
If Not Find1 Is Nothing Then
'-----------------------------------True condition

Else
'-----------------------------------False condition
Endif
End with
2nd method also not correct

pls help me

Nuwan
 
Last edited:
Upvote 0
Hi,

"Find" is compulsory in my macro. because
If Not Find1 Is Nothing Then
'-----------------------------------True condition

Else

Sheets.Add.Name = sheet1.range("A" & TT).text

Endif

Nuwan
 
Upvote 0
Hello Nuwan,

OK, you want to search a range for a given worksheet name.
Code:
Sub Macro1()
  Dim NewSheetName As String
  Dim TT As Long
  Dim Ws As Worksheet

    TT = 1

      For Each Ws In ThisWorkbook.Worksheets
        NewSheetName = Sheet1.Cells(TT, "A")
        If NewSheetName = "" Then Exit Sub
        Set Find1 = Ws.Cells.Find(What:=NewSheetName, _
                                  After:=Ws.Cells(1, 1), _
                                  LookIn:=xlValues, _
                                  LookAt:=xlWhole, _
                                  SearchOrder:=xlByRows, _
                                  SearchDirection:=xlNext, _
                                  MatchCase:=False)
        If Not Find1 Is Nothing Then
          'Code for match found
        Else
          'Code for no match found
        End If
        TT = TT + 1
      Next Ws
End Sub
Sincerely,
Leith Ross
 
Upvote 0
Dear leith,

still there is a problem, I want to create new sheet if sheet not exists
(for next loop try to add same sheet each time, of the loop)

see next post
nuwan
 
Upvote 0
Dear leith,

still there is a problem, I want to create new sheet if sheet not exists
("for next" loop try to add same sheet each and every time and generate errors)

Sub Macro1()
Dim NewSheetName As String
Dim TT As Long
Dim Ws As Worksheet

Do
'----------------------'Code for match found 1
Else
NewSheetName = sheet1.range("A" & TT).text
For Each Ws In ThisWorkbook.Worksheets

If NewSheetName = "" Then Exit Sub
Set Find1 = Ws.Cells.Find(What:=NewSheetName, After:=Ws.Cells(1, 1), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If Not Find1 Is Nothing Then
'---------------------'Code for match found 2
Else
'---------------------Sheets.Add.Name = sheet1.range("A" & TT).text
End If
End With
Next Ws
End If
TT = TT + 1
Loop Until sheet1.range("A" & TT).text ""
End Sub

How to solve this

nuwan
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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