Read/Write to Excel

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,835
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I would like to read and write to an Excel worksheet from another app, like Access or VB6.
From Googling I see examples but I can't quite get anything to work.
This will read from Excel but crashes when I try to write/save.
VBA Code:
Dim objExcel As Object, wb As Object
    Set objExcel = CreateObject("Excel.Application")
    Set wb = objExcel.workbooks.Open("C:\Temp\flacs.xls")
    objExcel.Worksheets("Sheet1").Activate
    With objExcel
        Debug.Print .cells(1, 1)
        .cells(1, 1) = "test"
    End With
 
  '  objExcel.displayalerts = False
   ' objExcel.Save
   ' objExcel.quit
    Set wb = Nothing
    Set objExcel = Nothing
Can anyone help fix this? Thanks.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
You might try the following...

VBA Code:
Dim objExcel As Object, wb As Object
    Set objExcel = CreateObject("Excel.Application")
    Set wb = objExcel.Workbooks.Open("C:\Temp\flacs.xls")
    wb.Worksheets("Sheet1").Activate
    With wb.Sheets("Sheet1")
        Debug.Print .Cells(1, 1)
        .Cells(1, 1) = "test"
    End With
 
  '  objExcel.displayalerts = False
   ' objExcel.Save
   ' objExcel.quit
    Set wb = Nothing
    Set objExcel = Nothing
 
Upvote 0
Thanks Tonyyy I'll try it out.. I think part of the problem is I don't want to open (or at least see) the workbook. So after writing the data I have to know the command to save it. I have had a bit of trouble with this, being told when next opened that it's 'locked for editing'.
 
Upvote 0
The following will still open the workbook (as well as close it and save the changes), but you won't see it...

VBA Code:
Dim objExcel As Object, wb As Object
Application.ScreenUpdating = False

Set objExcel = CreateObject("Excel.Application")
Set wb = objExcel.Workbooks.Open("C:\Temp\flacs.xls")

With wb.Sheets("Sheet1")
    Debug.Print .Cells(1, 1)
    .Cells(1, 1) = "test"
End With
wb.Close SaveChanges:=True

' objExcel.displayalerts = False
' objExcel.Save
' objExcel.quit
Set wb = Nothing
Set objExcel = Nothing
Application.ScreenUpdating = True
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top