98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsForum home » Delegate support and help forum » Microsoft Access VBA Training and help » VBA across Access and Excel
VBA across Access and Excel
Resolved · High Priority · Version 2003
Monique has attended:
Excel VBA Intro Intermediate course
VBA across Access and Excel
I am currently writing a VBA macro in Access which opens and uses an Excel spreadsheet to import certain pieces of data. I am trying to use the Find method to find a piece of text in the Excel spreadsheet and, using the range returned, obtain the row that the piece of text is in (I know that there is only one instance of the text in the spreadsheet).
However, I receive a Compile error: User-defined type not defined on the Range variable which I have set to hold the result of the Find method. Is this because Access and Excel have different find methods? Or because I am trying to run an Excel VBA method from Access VBA?
I would be grateful if you would let me know if I am doing something wrong, or whether there is anyother way of achieving my purpose (ideally without using a for loop). I have attached a snippet of my code below which hopefully will show what I am doing.
Many thanks,
Monique
NOTE: xl is an Object variable holding the Excel application and spreadsheet to copy data from. Any undeclared variables are Public variables declared in the main module.
Private Sub Main2UploadTAs()
Dim UnitRange As Range
Dim UnitRow As Integer
Dim Column As Integer
xl.Sheets(1).Select
' The rows of data start at row 21
Row = 21
' While there is a new row of data
Do While Not IsEmpty(xl.Cells(Row, 11))
' Select the plant
Plant = xl.Cells(Row, 3)
' Select the data for that unit for that year
TAssay = xl.Cells(Row, 11)
*
This is the section of code which I am having problems with
*
' Select the sheet to search in
xl.Sheets("UNITS").Select
' Find the cell which contains the text in the Plant string
Set UnitRange = xl.Cells.Find(What:=Plant, After:=xl.Cells(1, 1), LookIn:=xl.xlValues, LookAt:= _
xl.xlPart, SearchOrder:=xl.xlByRows, SearchDirection:=xl.xlNext, MatchCase:=False _
, SearchFormat:=False)
UnitRow = UnitRange.Row
*
The code from this point on works
*
' The first unit ID is in column 5
Column = 5
Do While Not IsEmpty(xl.Cells(UnitRow, Column))
' Select the unit ID
UnitID = xl.Cells(UnitRow, Column)
' Update the record in the database
DoCmd.RunSQL "UPDATE ProductionUnitAssayCalendar SET [Assay]=" & TailsAssay & " WHERE [ProductionUnitId]=" & UnitID & " AND [TypeId]=1 AND [StartDate]=#01/01/" & Year & "#;"
' Move across the list of units
Column = Column + 1
Loop
' Move down the list of plants
Row = Row + 1
Loop
Next Year
End Sub
RE: VBA across Access and Excel
Hi Monique
Thank you for your question.
The problem that you are facing is that you are trying to use the excel object model from within access. This is possible, but it requires a range of techniques that are quite involved (they take up to a couple of hours on our advanced VBA course.
In general you need to reference the excel object model by opening the VBE and clicking tools_references and selecting the "microsoft excel 12 object library" option. This will allow you to use excel objects in your code. The syntax is however different and too involved to discuss here. The following link provides a good overview
http://word.mvps.org/fAQs/InterDev/EarlyvsLateBinding.htm
Alternatively, I would recommend attanding a vba advanced course
Regards
Stephen
Tue 24 Mar 2009: Automatically marked as resolved.
Training information:
Welcome. Please choose your application (eg. Excel) and then post your question. Our Microsoft Qualified trainers will then respond within 24 hours (working days). Frequently Asked Questions
Any suggestions, questions or comments? Please post in the Improve the forum thread. |