Strange behaviour using Range().Find and column width

Tom_H

New Member
Joined
Feb 9, 2017
Messages
24
If I run the code below it works fine. First test1 and then test2.
As long as the width of the columns show the whole number in the cells!!
But if I make column A as narrow as the number change to ## and hide column B, test2 will not find the first and the second number?!
(I found a workaround using "match", not tested yet..)

Code:
Sub test1()
    Cells(1, 1) = 12345
    Cells(1, 2) = 23456
    Cells(1, 3) = 34567
    
    Dim rng As Range
    Set rng = Range(Cells(1, 1), Cells(1, 3))
    rng.Copy Destination:=Cells(1, 5)
End Sub


Sub test2()
    Dim rngFind As Range
    
    For c = 5 To 7
        Set rngFind = Range(Cells(1, 1), Cells(1, 3)).Find(What:=Cells(1, c), LookIn:=xlValues, LookAt:=xlWhole)
        If rngFind Is Nothing Then
            MsgBox "Cant find " & Cells(1, c)
        Else
            MsgBox "Found " & rngFind
        End If
    Next c
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
if I change to text it works fine...
Code:
    Cells(1, 1) = "asdfg"    
    Cells(1, 2) = "sdfgh"
    Cells(1, 3) = "dfghj"
 
Last edited:
Upvote 0
hi,

Have implement you code to a command button and it is working all fine for me. after change the column with of A till ## and hide column B.
Just a suggestion, check of the column B format cell Locked or hidden.
Could be possible that your other codes could protect the column B after hide.

Private Sub CommandButton1_Click()
'Sub test1()
Cells(1, 1) = 12345
Cells(1, 2) = 23456
Cells(1, 3) = 34567

Dim rng As Range
Set rng = Range(Cells(1, 1), Cells(1, 3))
rng.Copy Destination:=Cells(1, 5)
End Sub


Sub test2()
Dim rngFind As Range

For c = 5 To 7
Set rngFind = Range(Cells(1, 1), Cells(1, 3)).Find(What:=Cells(1, c), LookIn:=xlValues, LookAt:=xlWhole)
If rngFind Is Nothing Then
MsgBox "Cant find " & Cells(1, c)
Else
MsgBox "Found " & rngFind
End If
Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

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