Using .Find

mchac

Well-known Member
Joined
Apr 15, 2013
Messages
531
I have a spreadsheet that has the word "Start" in cell A1 then it is intended that the sub below will insert a time in the first blank cell it finds in column A starting its search in A1 then working down the column.

This works fine for the first two times the sub is executed, so in puts a time in cell A2 then A3. After that it only updates cell A3.

What am I missing?
Thanks in advance for your help.

Code:
Sub StartTime()

    Dim HOUR As Long
    Dim MINUTE As Long
    Dim LAST_ROW_NUMBER As Long
    
    HOUR = Int(Timer / 3600)
    MINUTE = Round(((Timer / 3600) - Int(Timer / 3600)) * 60, 0)
        
    LAST_ROW_NUMBER = Sheets("Sheet1").Range("A:A").Find("*", Sheets("Sheet1").Range("A1"), xlValues, xlPart, xlByColumns, xlNext).Row
    
    Range("A" & LAST_ROW_NUMBER + 1) = "=TIME(" & HOUR & "," & MINUTE & ",0)"


End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

mchac

Well-known Member
Joined
Apr 15, 2013
Messages
531
Thanks Mark.
My understanding is that ByRows searches across a row then moves to the next row; ByColumns is down a column then moves to the next. XlSearchOrder Enumeration*[Excel 2007 Developer Reference]. I want to go down column A but practically I think it would work either way in this case.

However I get the same behavior, only updating A3, with either choice.
 
Upvote 0

Marcelo Branco

MrExcel MVP
Joined
Aug 23, 2010
Messages
17,100
Office Version
  1. 365
  2. 2016
  3. 2010
Platform
  1. Windows
Try

Code:
LAST_ROW_NUMBER = Sheets("Sheet1").Range("A:A").Find("*", Sheets("Sheet1").Range("A1"), _
        xlValues, xlPart, xlByColumns, [B][COLOR=#0000cd]xlPrevious[/COLOR][/B]).Row

M.
 
Upvote 0

mchac

Well-known Member
Joined
Apr 15, 2013
Messages
531
ADVERTISEMENT
That does work. But why doesn't xlNext work?
 
Upvote 0

Marcelo Branco

MrExcel MVP
Joined
Aug 23, 2010
Messages
17,100
Office Version
  1. 365
  2. 2016
  3. 2010
Platform
  1. Windows
That does work. But why doesn't xlNext work?

SearchDirection:=xlNext searches for next matching value in range.

You are looking for any value (What:="*") after Sheets("Sheet1").Range("A1") - that's why the Find method always returns Sheets("Sheet1").Range("A2") with SearchDirection as xlNext , after the first time the macro is run.

M.
 
Last edited:
Upvote 0

mchac

Well-known Member
Joined
Apr 15, 2013
Messages
531
Ah, of course. You can see I'm using a function that was suggested to me and I don't understand it completely.

Thank you Marcelo and Mark.
 
Upvote 0

Forum statistics

Threads
1,195,988
Messages
6,012,714
Members
441,722
Latest member
tpaman1975

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