davedave24
Programmer
Hi, I'm trying search for a number (week number) in column A, then move to the next empty cell below that. I have the code but it's malfunctioning at the search point.
This is the function to determine the week number based on textDate. This works fine.
When I hit the Enter button, that function is called and the week number is found and loaded into the integer WeekNumber (this particular function uses the first monday of the year as the start of week 1). This works fine, I've tested with a msgbox and it finds the right week number.
I then run the routine FindWeekNumber, which is from [link="[URL unfurl="true"]http://www.rondebruin.nl/find.htm"[/URL]]this page[/url]. CustomerName is a string based on the customer's name selected from a combobox (it then switches to the sheet named after that customer. This bit works fine)
The problem is that it moves to the wrong week number. If it's 38, it goes to 39; 39 goes to 40. Also if you enter the same date twice, it will overwrite, instead of going to the next row.
Essentially - 1) search column A for WeekNumber; 2) move the next empty row below
This is the function to determine the week number based on textDate. This works fine.
Code:
Public Function WeekNumberFromDate(DT As Date, StartDate As Date) As Long
WeekNumberFromDate = Int(((DT - StartDate) + 6) / 7) + Abs(Weekday(DT) = Weekday(StartDate))
End Function
When I hit the Enter button, that function is called and the week number is found and loaded into the integer WeekNumber (this particular function uses the first monday of the year as the start of week 1). This works fine, I've tested with a msgbox and it finds the right week number.
Code:
WeekNumber = WeekNumberFromDate(textDate, "04/01/10")
I then run the routine FindWeekNumber, which is from [link="[URL unfurl="true"]http://www.rondebruin.nl/find.htm"[/URL]]this page[/url]. CustomerName is a string based on the customer's name selected from a combobox (it then switches to the sheet named after that customer. This bit works fine)
Code:
Sub FindWeekNumber()
'search Column A of the worksheet for the week number determined by the date in textDate
Dim Rng As Range
If Trim(WeekNumber) <> "" Then
With Workbooks("All Deliveries 2010").Worksheets(CustomerName).Range("A:A") 'select column A in the sheet named after the customer
Set Rng = .Find(What:=WeekNumber, _
After:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then 'if a positive result is returned (the week number has been found)
Application.Goto Rng, True 'set the active cell on the week number we searched for
ActiveCell.End(xlDown).Select 'find next empty cell below
ActiveCell.Offset(1, 0).Select 'move down 1 cell
ActiveCell = textDate 'write the data
Else
MsgBox "Nothing found" 'do stuff here to add the week number grid
End If
End With
End If
End Sub
The problem is that it moves to the wrong week number. If it's 38, it goes to 39; 39 goes to 40. Also if you enter the same date twice, it will overwrite, instead of going to the next row.
Essentially - 1) search column A for WeekNumber; 2) move the next empty row below