Solved

Import Single Cells from Excel

Posted on 2011-09-08
15
266 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

920 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