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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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,214,619
Messages
6,120,550
Members
448,970
Latest member
kennimack

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