I cannot figure out the declarations to write to an excel file that is already open.

I open the excel file and make it visible with this:
Private Sub Button1_Click
Dim objExcel As New Excel.Application
Dim objWB As Excel.Workbook
objWB = objExcel.Workbooks.Open("F:\My Software\files\test.xls")
objExcel.Visible=true
End Sub

What are the declarations needed to write to this open this file and write to it?
Private Sub Button2_Click
[[I DON'T KNOW WHAT TO PUT HERE]]
row = 1
column = 1
objExcel.Cells(row, column).Value = "cell data"
End Sub

Thank you,
BroncoTrojan

Recommended Answers

All 4 Replies

it is enough to change

objExcel.ActiveSheet.Cells(row, column).Value = "cell data"

Alternatives to first reply - working from memory but tooltips will provide correct spelling and parameters! And other objects
Dim sht as Excel.Worksheet
Dim rng as Excel.Range
'sheet does not need to be active or visible
sht = objWorkbook.Worksheets("Sheet name")
rng = sht.Range("A1")
rng.value = " some value"
rng.Offset(1,10).value="something else"
'get to end of sheet content
rng=rng.Specialcells(xlEnd).Offset(1,1)

Thank you for your help - it is working now. I used both the objExcel.ActiveSheet.Cells(row, column).Value = "cell data" and
sht = objWorkbook.Worksheets("Sheet name")
in my solution. I also changed my declarations in the Public Class for the form to
Private objExcel As New Excel.Application
Private objWB As Excel.Workbook

You saved me a lot of time and frustration - I am most grateful!
BroncoTrojan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.