Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading/Updating Excel

Status
Not open for further replies.

Juppo

Technical User
Jun 13, 2003
15
GB

I can copy a template excel sheet (Purchase form) but need to be able to update specific cells (D9 - Customer Name,D11 - Address1 etc). I've searched and can find loadsa of ways to write to an excel sheet but really Id love to be able to;
1) Read in data from specific cells
2) Update specific cells

Could somebody point in the right direction or give an example? Loads of thanks in advance!
 
Using ADO you can update/read from the spreadsheet

just look over the threads in regarsd to updating or retrieving a recordset, and use an Excel ADO connection
 
Juppo,

You can try this.

Filespec = "C:\Excel\MyExeclFile.xls"
Set objXL = WScript.CreateObject("EXCEL.application")
objXL.Visible = True
objXL.WorkBooks.Open Filespec
Set objWkBook = objXL.Workbooks.Item("MyExeclFile.xls")
Set colSheets = objWkBook.Sheets
WshShell.popup "Reading sheet: " & "Active" & ".", 3
Set Actv = objWkBook.Worksheets(1)

rows = Actv.UsedRange.rows.Count
cols = Actv.UsedRange.columns.Count

val1 = Ucase( trim( Actv.Cells(1,1).Value ) )
val2 = Ucase( trim( Actv.Cells(2,1).Value ) )
WshShell.popup "Val1= " & val1 & ", Val2= " & & val2, 3
objWkBook.Close
objXL.quit
Set colSheets = Nothing
Set objWkBook = Nothing
Set objXL = Nothing

fengshui_1998
 
Thanks FengShui1998 for the code but Im getting errors as it doesn't like the WScript, at least I have a rough idea about what Im aiming for.
 
Juppo,

Sorry. If you are doing it from an ASP page, change the following to:

Set objXL = Server.CreateObject("EXCEL.application")


fengshui_1998
 
Hi I'v got it working but my update causes an error, whats wrong ?
Code:
<%
  Filespec = &quot;C:\test3.xls&quot;
	
  Set objXL = Server.CreateObject(&quot;EXCEL.application&quot;)
      objXL.Visible = True
      objXL.WorkBooks.Open Filespec
      
  Set objWkBook = objXL.Workbooks.Item(&quot;test3.xls&quot;)
  Set colSheets = objWkBook.Sheets
  Set Actv = objWkBook.Worksheets(1)

  rows = Actv.UsedRange.rows.Count
  cols = Actv.UsedRange.columns.Count
    
    Actv.Cells(2,1).Value = 7777
  	Actv.Cells(2,2).Value = &quot;Dairyman&quot;
    Actv.Cells(2,3).Value = &quot;=Sum(A4+B4)&quot;
    Actv.Cells(2,4).Value = &quot;SLLLLLLAAAAAAAAGGGGGGGG&quot;

Actv.Update
Code:
  objWkBook.Close
  objWkBook.quit

  objXL.quit
  Set colSheets = Nothing
  Set objWkBook = Nothing
  Set objXL = Nothing 
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top