Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
What am I doing wrong here?
I've searched this discussion group, and google groups, but can't find the solution to the error, or an alternate way to code this. This code sorts the worksheet by the dates in column B, then searches the cells in the column looking for the last instance of myDate. (I need the address so I can select a range and move it to another worksheet.) But I keep getting stuck with this "Object var... not set" error. If I'm missing something fundamental here, please point me in the right direction. I've spent several hours looking for and trying alternate ways of doing this, and researching setting object variables, but I keep ending up back at the same error. Private Sub btnDate_Click() UserForm1.Hide Range("A1:E261").Sort Key1:=Range("B1"), Order1:=xlAscending, Header:= _ xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal If opt8.Value = True Then iDays = 9 If opt15.Value = True Then iDays = 16 If opt31.Value = True Then iDays = 32 'using "Set myDate = Date - iDays" gets error on next line myDate = Date - iDays 'select column B to search in Cells(1, 2).Activate ActiveCell.End(xlDown).Select strLast = Selection.Address Set myRange = Range("B1", strLast) 'as written, error occurs on next line myRange.Find(what:=myDate, lookat:=xlPart).Select strLast = Selection.Address Set myRange = Nothing End Sub Any help would be extremely appreciated! |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Walter,
It is probably because MyRange.Find(what:=myDate, lookat:=xlPart) returns Nothing, therefore applying the Select: MyRange.Find(what:=myDate, lookat:=xlPart).Select on Nothing generates an error. (assuming myRange is not Nothing) To make sure of that, use instead: Dim rg as range '... prior code set rg = myRange.Find(what:=myDate, lookat:=xlPart) if rg is nothing then Msgbox "Value not found. Search for: " & mydate & " in " myRange.Address Exit Sub End if Rg.Select If it is the problem, the above code will show what you are looking for (mydate) and where (address of myrange) Probably mydate is not in a 'searchable' format. Try to replace mydate in the Find method by: - Format(mydate,"mm/dd/yyyy") ' formatted string as the expected date format in myRange or - datevalue(mydate) Regards, Sébastien "Walter" wrote: What am I doing wrong here? I've searched this discussion group, and google groups, but can't find the solution to the error, or an alternate way to code this. This code sorts the worksheet by the dates in column B, then searches the cells in the column looking for the last instance of myDate. (I need the address so I can select a range and move it to another worksheet.) But I keep getting stuck with this "Object var... not set" error. If I'm missing something fundamental here, please point me in the right direction. I've spent several hours looking for and trying alternate ways of doing this, and researching setting object variables, but I keep ending up back at the same error. Private Sub btnDate_Click() UserForm1.Hide Range("A1:E261").Sort Key1:=Range("B1"), Order1:=xlAscending, Header:= _ xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal If opt8.Value = True Then iDays = 9 If opt15.Value = True Then iDays = 16 If opt31.Value = True Then iDays = 32 'using "Set myDate = Date - iDays" gets error on next line myDate = Date - iDays 'select column B to search in Cells(1, 2).Activate ActiveCell.End(xlDown).Select strLast = Selection.Address Set myRange = Range("B1", strLast) 'as written, error occurs on next line myRange.Find(what:=myDate, lookat:=xlPart).Select strLast = Selection.Address Set myRange = Nothing End Sub Any help would be extremely appreciated! |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
That solved it! Many, many thanks!
Walter "sebastienm" wrote: Hi Walter, It is probably because MyRange.Find(what:=myDate, lookat:=xlPart) returns Nothing, therefore applying the Select: MyRange.Find(what:=myDate, lookat:=xlPart).Select on Nothing generates an error. (assuming myRange is not Nothing) To make sure of that, use instead: Dim rg as range '... prior code set rg = myRange.Find(what:=myDate, lookat:=xlPart) if rg is nothing then Msgbox "Value not found. Search for: " & mydate & " in " myRange.Address Exit Sub End if Rg.Select If it is the problem, the above code will show what you are looking for (mydate) and where (address of myrange) Probably mydate is not in a 'searchable' format. Try to replace mydate in the Find method by: - Format(mydate,"mm/dd/yyyy") ' formatted string as the expected date format in myRange or - datevalue(mydate) Regards, Sébastien "Walter" wrote: What am I doing wrong here? I've searched this discussion group, and google groups, but can't find the solution to the error, or an alternate way to code this. This code sorts the worksheet by the dates in column B, then searches the cells in the column looking for the last instance of myDate. (I need the address so I can select a range and move it to another worksheet.) But I keep getting stuck with this "Object var... not set" error. If I'm missing something fundamental here, please point me in the right direction. I've spent several hours looking for and trying alternate ways of doing this, and researching setting object variables, but I keep ending up back at the same error. Private Sub btnDate_Click() UserForm1.Hide Range("A1:E261").Sort Key1:=Range("B1"), Order1:=xlAscending, Header:= _ xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal If opt8.Value = True Then iDays = 9 If opt15.Value = True Then iDays = 16 If opt31.Value = True Then iDays = 32 'using "Set myDate = Date - iDays" gets error on next line myDate = Date - iDays 'select column B to search in Cells(1, 2).Activate ActiveCell.End(xlDown).Select strLast = Selection.Address Set myRange = Range("B1", strLast) 'as written, error occurs on next line myRange.Find(what:=myDate, lookat:=xlPart).Select strLast = Selection.Address Set myRange = Nothing End Sub Any help would be extremely appreciated! |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Runtime Error '91' Object variable or With block variable not set | Excel Discussion (Misc queries) | |||
Object or with block not set | Excel Worksheet Functions | |||
Cells.Find error Object variable or With block variable not set | Excel Programming | |||
"Run-time error 91: Object variable or With block not set" | Excel Programming | |||
Error 91 - Object variable with block variable not set | Excel Programming |