Set Object with string from .Find works but not same string from left

Tanquen

Board Regular
Joined
Dec 14, 2017
Messages
78
Office Version
  1. 365
Platform
  1. Windows
Are they both not giving me a string? The left function gives a Type Mismatch.

They both seem to give the same string when I test the same functions but output the result in a string variable.

If CL.Value is "TEST_001" they both come back with "TEST"?

VBA Code:
Dim rngFoundST As String

rngFoundST = .Find(Replace(CL.Value, "_001", ""), LookIn:=xlValues, LookAt:=xlWhole)
rngFoundST = Left(CL.Value, Len(CL.Value) - 4)


VBA Code:
Sub Get_Short_Descriptions_From_Tables()
    '***Get Short Descriptions From the Tables Sheet
        Dim sh As Worksheet, rng As Range, d As String, fRng As Range, CL As Range, Count As Integer, TagNameColumn As Integer, rngFoundST As String
    
        TagNameColumn = Worksheets("Function").Range("B5").Value
    
        Count = 1
                
        rngFoundST = "TEST123"
        rngFoundST = Left("saefasefaswef", Len("asdasefs") - 4)
    
        Set sh = Worksheets("Function")
 
        With sh
            Worksheets("Function").Range("B11:C999").Font.Bold = False
            Worksheets("Function").Range("B11:C999").Interior.ColorIndex = 0
        
            Set fRng = Worksheets("Function").Range("A10:A999")
        
            For Each CL In fRng
                If Not IsEmpty(CL.Value) Then
                    With Worksheets("Tables").Range("A:A")
                        Set rngFound = .Find(Replace(CL.Value, "_001", ""), LookIn:=xlValues, LookAt:=xlWhole)
                        'Set rngFound = Left(CL.Value, Len(CL.Value) - 4)
                        If Not rngFound Is Nothing Then
                            If rngFound.Offset(, TagNameColumn) = "N/A" Then
                                CL.Offset(, 1).Value = Worksheets("Function").Range("A10").Value & "_00" & Count
                                CL.Offset(, 1).Font.Bold = True
                                CL.Offset(, 1).Interior.Color = RGB(255, 242, 204)
                                Count = Count + 1
                            Else
                                CL.Offset(, 1).Value = Replace(rngFound.Offset(, TagNameColumn), "-", "_")
                                CL.Offset(, 1).Font.Bold = True
                                CL.Offset(, 2).Value = Replace(rngFound.Offset(, 1), "-", "_")
                                CL.Offset(, 2).Font.Bold = True
                                CL.Offset(, 3).Value = Replace(rngFound.Offset(, 2), "_", "-")
                            End If
                        Else
                            If InStr(CL.Value, "_001") Then
                              CL.Offset(, 1).Interior.Color = RGB(255, 255, 155)
                            End If
                        End If
                    End With
                End If
            Next CL
        End With
End Sub
 
Last edited:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Are they both not giving me a string? The left function gives a Type Mismatch.

They both seem to give the same string when I test the same functions but output the result in a string variable.

If CL.Value is "TEST_001" they both come back with "TEST"?

VBA Code:
Dim rngFoundST As String

rngFoundST = .Find(Replace(CL.Value, "_001", ""), LookIn:=xlValues, LookAt:=xlWhole)
rngFoundST = Left(CL.Value, Len(CL.Value) - 4)


VBA Code:
Sub Get_Short_Descriptions_From_Tables()
    '***Get Short Descriptions From the Tables Sheet
        Dim sh As Worksheet, rng As Range, d As String, fRng As Range, CL As Range, Count As Integer, TagNameColumn As Integer, rngFoundST As String
   
        TagNameColumn = Worksheets("Function").Range("B5").Value
   
        Count = 1
               
        rngFoundST = "TEST123"
        rngFoundST = Left("saefasefaswef", Len("asdasefs") - 4)
   
        Set sh = Worksheets("Function")
 
        With sh
            Worksheets("Function").Range("B11:C999").Font.Bold = False
            Worksheets("Function").Range("B11:C999").Interior.ColorIndex = 0
       
            Set fRng = Worksheets("Function").Range("A10:A999")
       
            For Each CL In fRng
                If Not IsEmpty(CL.Value) Then
                    With Worksheets("Tables").Range("A:A")
                        Set rngFound = .Find(Replace(CL.Value, "_001", ""), LookIn:=xlValues, LookAt:=xlWhole)
                        'Set rngFound = Left(CL.Value, Len(CL.Value) - 4)
                        If Not rngFound Is Nothing Then
                            If rngFound.Offset(, TagNameColumn) = "N/A" Then
                                CL.Offset(, 1).Value = Worksheets("Function").Range("A10").Value & "_00" & Count
                                CL.Offset(, 1).Font.Bold = True
                                CL.Offset(, 1).Interior.Color = RGB(255, 242, 204)
                                Count = Count + 1
                            Else
                                CL.Offset(, 1).Value = Replace(rngFound.Offset(, TagNameColumn), "-", "_")
                                CL.Offset(, 1).Font.Bold = True
                                CL.Offset(, 2).Value = Replace(rngFound.Offset(, 1), "-", "_")
                                CL.Offset(, 2).Font.Bold = True
                                CL.Offset(, 3).Value = Replace(rngFound.Offset(, 2), "_", "-")
                            End If
                        Else
                            If InStr(CL.Value, "_001") Then
                              CL.Offset(, 1).Interior.Color = RGB(255, 255, 155)
                            End If
                        End If
                    End With
                End If
            Next CL
        End With
End Sub

Code:
        Set rngFound = .Find(Replace(CL.Value, "_001", ""), LookIn:=xlValues, LookAt:=xlWhole)

Not real familiar with the Find method but shouldn't Replace precede find?
 
Upvote 0
I think I messed it up.

I need to replace just the "Replace" function not the whole thing. :)


It should be like this:
VBA Code:
Set rngFound = .Find(Left(CL.Value, Len(CL.Value) - 4), LookIn:=xlValues, LookAt:=xlWhole)

Can I delete the thread?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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