Thanks to all the people who reply so promptly to my questions! I have another one related to the form I am working on. When I try to call this function from another, I get the "Object variable or Block variable not set" error... but when I paste it as a macro in Excel I have no problem... could anyone please help me explain this?
The error happens at the Cells.Find line - starting on line 7. Thanks in advance. - Function Finder(DateSelect As String) As Integer
-
Dim egg As String
-
Dim colval As Integer
-
-
egg = "AmtU_" & DateSelect
-
- Cells.Find(What:=egg, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
-
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _
-
.Activate
-
Finder = ActiveCell.Column
-
Exit Function
-
End Function
8 8693
Thanks to all the people who reply so promptly to my questions! I have another one related to the form I am working on. When I try to call this function from another, I get the "Object variable or Block variable not set" error... but when I paste it as a macro in Excel I have no problem... could anyone please help me explain this?
The error happens at the Cells.Find line - starting on line 7. Thanks in advance. - Function Finder(DateSelect As String) As Integer
-
Dim egg As String
-
Dim colval As Integer
-
-
egg = "AmtU_" & DateSelect
-
- Cells.Find(What:=egg, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
-
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _
-
.Activate
-
Finder = ActiveCell.Column
-
Exit Function
-
End Function
Hi, cloh.
Keep in my mind that the code is not in Excel. ;)
Properties like "Cells", "ActiveCell" available in Excel module without referencing as long as they are the properties of Excel.Application class.
In Access module you need to reference them through Excel.Application object obtained on excel opening via CreateObject or GetObject.
Hi FishVal,
You're right of course. Thanks for being patient with this newbie :-)
I tried to reference the properties using the Excel.Application object like you said... I still have a problem with the "Cells" property. My code is as shown below. All I want is to search for the string egg in the first row of each worksheet. - Function Finder(DateSelect As String) As Integer
-
Dim egg As String
-
Dim colval As Integer
-
Dim xlapp As excel.Application
-
Dim xlbook As excel.Workbook
-
Dim xlsheet As excel.Worksheet
-
Dim xlrange As excel.range
-
Dim ExcelObject As Object
-
-
Set xlapp = CreateObject("Excel.Application")
-
Set xlbook = xlapp.Workbooks.Add
-
Set xlsheet = xlbook.ActiveSheet
-
-
xlsheet.range("A1").Select
-
egg = "AmtU_" & DateSelect
-
xlsheet.Cells.Find(What:=egg, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
-
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _
-
.Activate
-
Finder = ActiveCell.Column
-
Exit Function
-
End Function
actually nevermind! as soon as i took away the "xlsheet" from line 16 my code worked... but if you can explain it to me, then at least i'll learn something! :-)
actually nevermind! as soon as i took away the "xlsheet" from line 16 my code worked... but if you can explain it to me, then at least i'll learn something! :-)
:)
Actually I don't know what to explain.
I really don't understand why it works without reference to Worksheet object.
The referencing problem I still see in the code is implicit call of ActiveCell property in line #16, #19.
You're right, FishVal... the problem still exists... this is the code as it stands and the same error still appears. I'm not quite sure why... - Function Finder(DateSelect As String) As Integer
-
Dim egg As String
-
Dim colval As Integer
-
Dim xlapp As excel.Application
-
Dim xlbook As excel.Workbook
-
Dim xlsheet As excel.Worksheet
-
Dim xlrange As excel.range
-
'Dim xlcell As
-
Dim ExcelObject As Object
-
-
Set xlapp = GetObject(, "Excel.Application")
-
Set xlbook = xlapp.Workbooks.Add
-
Set xlsheet = xlbook.ActiveSheet
-
-
xlsheet.range("A1").Select
-
-
egg = "AmtU_" & DateSelect
-
'MsgBox "this is egg " & egg
-
-
Cells.Find(What:=egg, LookIn:=xlValues, LookAt:= _
-
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _
-
.Activate
-
Finder = ActiveCell.Column
-
-
Exit Function
-
End Function
You're right, FishVal... the problem still exists... this is the code as it stands and the same error still appears. I'm not quite sure why... - Function Finder(DateSelect As String) As Integer
-
Dim egg As String
-
Dim colval As Integer
-
Dim xlapp As excel.Application
-
Dim xlbook As excel.Workbook
-
Dim xlsheet As excel.Worksheet
-
Dim xlrange As excel.range
-
'Dim xlcell As
-
Dim ExcelObject As Object
-
-
Set xlapp = GetObject(, "Excel.Application")
-
Set xlbook = xlapp.Workbooks.Add
-
Set xlsheet = xlbook.ActiveSheet
-
-
xlsheet.range("A1").Select
-
-
egg = "AmtU_" & DateSelect
-
'MsgBox "this is egg " & egg
-
-
Cells.Find(What:=egg, LookIn:=xlValues, LookAt:= _
-
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _
-
.Activate
-
Finder = ActiveCell.Column
-
-
Exit Function
-
End Function
Hi, cloh.
Finally I've got a time to look close to your code. - code
- Cells.Find(What:=egg, LookIn:=xlValues, LookAt:= _
-
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _
-
.Activate
will fail when no cell will be found. Really you try to invoke Activate method for Range object expected to be returned by Cells.Find method. If no cell found then object is set to Nothing and Nothing.Activate will raise error you are getting. - you use ActiveCell to determine the result of search but some cell will always be active though not corresponding to search criteria
Here is a sample code for searching within worksheet. -
Public Function Finder(DateSelect As Integer) As Integer
-
-
Dim egg As String
-
Dim colval As Integer
-
Dim xlapp As Excel.Application
-
Dim xlbook As Excel.Workbook
-
Dim xlsheet As Excel.Worksheet
-
Dim xlrange As Excel.Range
-
-
Set xlapp = CreateObject("Excel.Application")
-
With xlapp
-
.Visible = True
-
'leave Excel app running when the reference (xlapp) destroyed
-
.UserControl = True
-
Set xlbook = .Workbooks.Add
-
Set xlsheet = xlbook.ActiveSheet
-
End With
-
-
egg = "AmtU_" & DateSelect
-
'run find method for the whole worksheet
-
'and set range object to cell found
-
Set xlrange = xlsheet.Cells.Find(egg)
-
If xlrange Is Nothing Then
-
'nothing found
-
Finder = -1
-
Else
-
'cell found
-
Finder = xlrange.Row
-
End If
-
-
-
'destroy objects, Excel application stays running
-
'bkz xlapp.UserControl = True
-
Set xlrange = Nothing
-
Set xlsheet = Nothing
-
Set xlbook = Nothing
-
Set xlapp = Nothing
-
-
End Function
-
FishVal, what would i do without you? I used the sample code as a template and modified my code to match what I wanted and it now works! I've attached it at the bottom in case anyone has a similar problem. Thank you SO much! - Function Finder(DateSelect As String, sheetindex As Integer) As Integer
-
Dim egg As String
-
Dim colval As Integer
-
Dim xlapp As excel.Application
-
Dim xlbook As excel.Workbook
-
Dim xlsheet As excel.Worksheet
-
Dim xlrange As excel.range
-
Dim sheetname As String
-
sheetname = "LocID" & sheetindex
-
DateSelect_Ref = Format(DateSelect, "MM/DD/YYYY")
-
-
Set xlapp = GetObject(, "Excel.Application")
-
Set xlbook = ActiveWorkbook
-
Set xlsheet = xlbook.Worksheets(sheetindex)
-
-
xlsheet.Activate
-
Dim title As String
-
-
egg = "AmtU_" & DateSelect_Ref
-
Set xlrange = xlsheet.Cells.Find(egg)
-
If xlrange Is Nothing Then
-
Finder = -1
-
Else
-
Finder = xlrange.Column
-
End If
-
-
Set xlrange = Nothing
-
Set xlsheet = Nothing
-
Set xlbook = Nothing
-
End Function
Hi, cloh.
I'm glad you appreciate my help and thanks for posting successful solution for those that visit the thread later.
You are welcome.
Good luck and happy coding. :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ike |
last post by:
Anyone knows what cuases this under 6.0 ? -Ike
|
by: AIM |
last post by:
Error in msvc in building inheritance.obj to build hello.pyd
Hello,
I am trying to build the boost 1.31.0 sample extension hello.cpp.
I can not compile the file inheritance.cpp because the two...
|
by: Lauren Quantrell |
last post by:
I get the following error:
"Object variable or Width block variable not set error"
trying to run this in my Access2000 .ADP database:
CurrentDb.Properties.Append...
|
by: Peter Frost |
last post by:
Please help
I don't know if this is possible but what I would really like to do is
to use On Error Goto to capture the code that is being executed when
an error occurs.
Any help would be much...
|
by: RJN |
last post by:
Hi
Sorry for posting again. I'm calling a shared method in the class. The
following error is found in production though I'm not able to reproduce.
...
|
by: HKSHK |
last post by:
This list compares the error codes used in VB.NET 2003 with those used
in VB6.
Error Codes:
============
3: This Error number is obsolete and no longer used. (Formerly:
Return without GoSub)...
|
by: majo |
last post by:
Hi,
I had posted this once earlier, but couldnt get my problem solved.
When i run an asp.net application in windows 2003 server with IIS 6.0, it
"SOMETIMES" gives me the below given error....
|
by: Richard Hollenbeck |
last post by:
I've marked the line in this subroutine where I've been getting this error.
It may be something stupid but I've been staring at this error trying to fix
it for over an hour. I'm pretty sure the...
|
by: Newbie19 |
last post by:
I'm trying to get a list of all subfolders in a folder on a share drive, but I keep on getting this error message:
Object variable or With block variable not set.
Description: An unhandled...
|
by: Al G |
last post by:
Hi,
I'm converting a bit of POP3 VB6 code to VB2005, and have run into this
error with the following code.
Can someone help me find out what I'm missing/doing wrong?
'holds the attachments...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |