Solved

Import Single Cells from Excel

Posted on 2011-09-08
15
265 Views
Last Modified: 2012-05-12
Not sure why I am unable to isolate and pick one cell. Anyone see what I'm doing wrong here? I attached screen shots of the error I'm getting.
Public Function TestExcel()
Dim x
  x = fExcelCellADO("C1", "\\pacper.local\PPI Share\ProductionShopFloor\client\TESTING\ZR0040911.xls", "Inputs!")
  MsgBox x
End Function


Function fExcelCellADO(strCell As String, strFileName As String, strSheetName As String)
   
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
             & "Data Source=" & strFileName & ";" _
             & "Extended Properties='Excel 8.0;HDR=No;IMEX=1';"
             
    Set rs = New ADODB.Recordset
    'rs.Open "SELECT F1 FROM [" & strSheetName & "$" & strCell & ":" & strCell & "]", cn
    rs.Open "SELECT F1 FROM [" & strSheetName & strCell & "]", cn
    fExcelCellADO = rs!f1
 
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
 
End Function

Open in new window

VBAError.doc
0
Comment
Question by:Ei0914
  • 8
  • 7
15 Comments
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 500 total points
ID: 36504110
try this revised function


Function fExcelCell (strCell As String, strFileName As String, strSheetName As String)
   
    Dim xlObj as object
      set  xlObj=createobject("excel.application")
                            xlobj.workbooks.open strFileName
                  fExcelCell= xlObj .Worksheets(strSheetName).Range(strCell).value

      xlobj.quit
                   set xlObj=nothing
end function
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 36504157

test this codes
Public Function TestExcel()
Dim x
  x = fExcelCell("C1", "\\pacper.local\PPI Share\ProductionShopFloor\client\TESTING\ZR0040911.xls", "Inputs!")
  MsgBox x
End Function

Function fExcelCell(strCell As String, strFileName As String, strSheetName As String)
   
Dim xlObj As Object
    Set xlObj = CreateObject("excel.application")
    xlObj.workbooks.Open strFileName
    fExcelCell = xlObj.Worksheets(strSheetName).Range(strCell).Value

xlObj.Quit
Set xlObj = Nothing
End Function

Open in new window

0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 36504175
change this

  x = fExcelCell("C1", "\\pacper.local\PPI Share\ProductionShopFloor\client\TESTING\ZR0040911.xls", "Inputs!")

with

  x = fExcelCell("C1", "\\pacper.local\PPI Share\ProductionShopFloor\client\TESTING\ZR0040911.xls", "Inputs")

remove the bang sign from  "Inputs!"
0
 

Author Comment

by:Ei0914
ID: 36504191
Subscript Out of Range

-->>  fExcelCell= xlObj .Worksheets(strSheetName).Range(strCell).value

0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 36504237
this line is errorneous

---------------------v------ remove the space between xlObj and the dot
 fExcelCell= xlObj .Worksheets(strSheetName).Range(strCell).value


copy these codes


Public Function TestExcel()
Dim x
  x = fExcelCell("C1", "\\pacper.local\PPI Share\ProductionShopFloor\client\TESTING\ZR0040911.xls", "Inputs")
  MsgBox x
End Function

Function fExcelCell(strCell As String, strFileName As String, strSheetName As String)
   
Dim xlObj As Object
    Set xlObj = CreateObject("excel.application")
    xlObj.workbooks.Open strFileName
    fExcelCell = xlObj.Worksheets(strSheetName).Range(strCell).Value

xlObj.Quit
Set xlObj = Nothing
End Function

Open in new window



0
 

Author Comment

by:Ei0914
ID: 36504327
Strange -- same error... and regarding your previous comment --- there was no space -- odd.
0
 

Author Comment

by:Ei0914
ID: 36504344
I tried this - added the exclamation point - still no good.

 x = fExcelCell("C1", "\\pacper.local\PPI Share\ProductionShopFloor\client\TESTING\ZR0040911.xls", "Inputs!")

I'm going to import the spreadsheet instead of having it linked to check the field types.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 119

Expert Comment

by:Rey Obrero
ID: 36504355
can you post the codes that you are USING..
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 36504379
tr y this change, you should see the excel file Open

also take note that the path you are passing has a { dot }  on this part  "\\pacper.local\

Function fExcelCell(strCell As String, strFileName As String, strSheetName As String)
   
Dim xlObj As Object
    Set xlObj = CreateObject("excel.application")
    xlObj.workbooks.Open strFileName

    xlObj.visible= true

Stop

    fExcelCell = xlObj.Worksheets(strSheetName).Range(strCell).Value

xlObj.Quit
Set xlObj = Nothing
End Function
0
 

Author Comment

by:Ei0914
ID: 36504519
Same -- Subscript out of range. I researched the error

It said that it is because the column has multiple data types -- which is true. This spreadsheet doesn't use column headings -- have tons of formulas, etc. So the individual cells need to be access as opposed to importing an entire sheet.

If I import the spreadsheet I can probably query the fields that way based on an autonumber record ID

0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 36504535
upload a copy of the excel file...
0
 

Accepted Solution

by:
Ei0914 earned 0 total points
ID: 36504543
Oooops I forgot to take the "!" out of the sheet name. When I removed it it worked!!!!

Thanks a lot folks!
0
 

Author Closing Comment

by:Ei0914
ID: 36527913
Thanks a lot!!!!
0
 

Author Comment

by:Ei0914
ID: 36504556
I didn't award the points properly -- Capricorn should get the "solution"

Sorry.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 36504607
if you are just reading the posts before acting on your own, it would have been done from this post

http:#a36504175

http:#a36504237
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now