Strange behaviour using Range().Find and column width

Tom_H

New Member
Joined
Feb 9, 2017
Messages
23
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().

Tom_H

New Member
Joined
Feb 9, 2017
Messages
23
if I change to text it works fine...
Code:
    Cells(1, 1) = "asdfg"    
    Cells(1, 2) = "sdfgh"
    Cells(1, 3) = "dfghj"
 
Last edited:

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
692
Office Version
  1. 2013
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
 

Tom_H

New Member
Joined
Feb 9, 2017
Messages
23
OK, found a better solution...
Change LookIn:=xlValues to LookIn:=xlFormulas
:)
 

Forum statistics

Threads
1,148,108
Messages
5,744,876
Members
423,907
Latest member
zerocool88

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
Top