Error Handing if Find not found

Shiseiji

Board Regular
Joined
Oct 23, 2009
Messages
214
Office Version
  1. 2019
Platform
  1. Windows
VBA Code:
Range("A1", Cells(LastCol)).Name = "ws_2Users_SearchRow"
        With Range("ws_2Users_SearchRow")
            .Find(What:="UPN", LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext).Name = "ws_2Users_UPN"
            .Find(What:="eMail", LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext).Name = "ws_2Users_Email"
            '.Find(What:="SamAccountName", LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext).Name = "ws_2Users_SamAccountName"
            .Find(What:="AtHocUserName", LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext).Name = "ws_2Users_AtHocUserName"
        End With
 
Last edited by a moderator:

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
How about
VBA Code:
   Dim Fnd As Range
   Range("A1", Cells(LastCol)).Name = "ws_2Users_SearchRow"
   With Range("ws_2Users_SearchRow")
       Set Fnd = .Find(What:="UPN", LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
       If Not Fnd Is Nothing Then Fnd.Name = "ws_2Users_UPN"
       Set Fnd = .Find(What:="eMail", LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
       If Not Fnd Is Nothing Then Fnd.Name = "ws_2Users_Email"
 
Upvote 0
Solution
Oh stupid me! Wasn't done looking for answers. Sorry! Then took >:10 trying to fix it. Argggg

So the text I import 99.9% of the time has the headers I've coded for below. I had an error on the line that's now a rem.
I've never been good at error handing so I thought I'd ask for help. So below are the ranges I'm naming. Looking for suggestions on "next" when there's an error.

VBA Code:
   Dim LastCol As Integer
    Dim tbl_2Users As Range
    Dim sName   As String
    Dim ThisWb  As Workbook
    Dim ThisWs  As Worksheet
    '
    Set ThisWb = ActiveWorkbook
    Set ThisWs = ws_2Users
    '
'------------Start Remove any Bad Named Ranges---------------
      With ThisWb
      Call m_DeleteAllBadNamesThisWb
'------------End Remove any Bad Named Ranges---------------
'--Named Ranges (incomplete)---------------
'tbl_2Users
'ws_2Users_SearchRow
'ws_2Users_New
'ws_2Users_UPN
'ws_2Users_Email
'ws_2Users_SAM
'ws_2Users_RngUPN"
'ws_2Users_RngEmail
'ws_2Users_RngSAM
'
'--End List of Named Ranges-----------------------------------------
'------------Start Find Last Row and Last Column---------------
    LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
    LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
'------------End Find Last Row and Last Column---------------
'------------Start Naming Ranges---------------
Range("A1", Cells(LastCol)).Name = "ws_2Users_SearchRow"
        With Range("ws_2Users_SearchRow")
            .Find(What:="UPN", LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext).Name = "ws_2Users_UPN"
            .Find(What:="eMail", LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext).Name = "ws_2Users_Email"
            '.Find(What:="SamAccountName", LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext).Name = "ws_2Users_SamAccountName"
            .Find(What:="AtHocUserName", LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext).Name = "ws_2Users_AtHocUserName"
        End With
    Range("A1").Name = "ws_2Users_New"
    Range("ws_2Users_New").Offset(1, 0).Resize(LastRow).Name = "ws_2Users_RngNew"
    Range("ws_2Users_UPN").Offset(1, 0).Resize(LastRow).Name = "ws_2Users_RngUPN"
    Range("ws_2Users_Email").Offset(1, 0).Resize(LastRow).Name = "ws_2Users_RngEmail"
    Range("ws_2Users_username").Offset(1, 0).Resize(LastRow).Name = "ws_2Users_RngUsername"
    Range("ws_2Users_SamAccountName").Offset(1, 0).Resize(LastRow).Name = "ws_2Users_RngSamAccountName"
    Range("AtHocUserName").Offset(1, 0).Resize(LastRow).Name = "ws_2Users_RngAtHocUserName"
    Range("AtHocUserName").Value = "AtHoc Username"

Thanks!
Ron
 
Upvote 0
How about
VBA Code:
   Dim Fnd As Range
   Range("A1", Cells(LastCol)).Name = "ws_2Users_SearchRow"
   With Range("ws_2Users_SearchRow")
       Set Fnd = .Find(What:="UPN", LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
       If Not Fnd Is Nothing Then Fnd.Name = "ws_2Users_UPN"
       Set Fnd = .Find(What:="eMail", LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
       If Not Fnd Is Nothing Then Fnd.Name = "ws_2Users_Email"
Oh Fluff, you are just too much! Thank you! I didn't get my mea culpa posted!

You constantly make my life so much less stressful. Thank you!

Ron
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,210
Members
448,874
Latest member
b1step2far

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