Opening an Excel spreadsheet by using a "Public Function"

Opening an Excel spreadsheet by using a "Public Function"

isocam
Collaborator Collaborator
350 Views
0 Replies
Message 1 of 1

Opening an Excel spreadsheet by using a "Public Function"

isocam
Collaborator
Collaborator

Can anybody help???

 

I an trying to create a Public Function for opening and closing a Excel Spreadsheet so that I can read data contained in the spreadsheet.

 

My program uses the following to open a Excel Spreadsheet

 

Public Function CentreDrillData()
    Dim oXl As Object

    Dim oXlWorkBook As Object

    Dim oXLWorkSheet As Object

    Set oXl = CreateObject("Excel.Application")

    Set oXlWorkBook = oXl.Workbooks.Open(ToolDataFileName)

    Set oXLWorkSheet = oXlWorkBook.Sheets(2)
   
    For Counter = 3 To 13
        If CentreDrillSize = Trim(oXLWorkSheet.cells(Counter, ToolDescriptionCell).Text) Then
           SpindleSpeed = oXLWorkSheet.cells(Counter, PartMaterialCell + 1).Text

           Depth = oXLWorkSheet.cells(Counter, ToolDescriptionCell + 1).Text

           Exit For
        End If
    Next


    Call CloseExcel(oXlWorkBook, oXl)
End Function

 

The CloseExcel works OK

 

I want to put the first 6 lines into a Public function thus....


Public Function OpenExcel(l)
    Dim oXl As Object

    Dim oXlWorkBook As Object

    Dim oXLWorkSheet As Object

    Set oXl = CreateObject("Excel.Application")

    Set oXlWorkBook = oXl.Workbooks.Open(ToolDataFileName)

    Set oXLWorkSheet = oXlWorkBook.Sheets(ExcelSheetNumber)
End Function

 

But this Public Function does not work!

 

I want to replace the first lines of the main function to Call OpenExcel.

 

Does anybody know what is wrong????

 

Many thanks in advance!!!

 

IsoCAM

0 Likes
351 Views
0 Replies
Replies (0)