• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • Last Modified:

using the excel sheet in a VB form

I am trying to use the excel sheet in a vb form to keep the program clean.  When I added the sheet to the form it's default name is 'Sheet1'.  I want to be able to populate the sheet from a access DB and allow the user to make changes and then save data back to DB.  I'm having problems referencing the cells.  Can anyone show me the code on how to reference the 'Sheet1' so I can control the cells?
0
jayhogan
Asked:
jayhogan
  • 4
  • 3
1 Solution
 
Dave_GreeneCommented:
Here is an example

Private Sub cmdButtonSum_Click()

   Dim Obj1 As Object
   Dim answer As Integer
       
        Set Obj1 = CreateObject("Excel.Application")
        Obj1.Visible = True
        Obj1.Workbooks.Add
        Obj1.Cells(1, 1).Value = textbox1.Text
        Obj1.Cells(2, 1).Value = textbox2.Text
        Obj1.Cells(3, 1).Formula = "=R1C1+R2C1"
        answer = Obj1.Cells(3, 1)
        MsgBox (answer)
        Obj1.Application.Quit
        Set Obj1 = Nothing



End Sub

0
 
Alon_hCommented:
Hi jayhogan.

Try this :

Private Sub Command1_Click()
Dim xlApp As New Excel.Application
Dim xlWb As New Excel.Workbook
Dim xlSht As Excel.Sheets

   Set xlApp = CreateObject("Excel.Application")
   xlApp.Visible = True
   
   Set xlWb = xlApp.Workbooks.Add
   Set xlSht = xlWb.Sheets
   
   xlSht(1).Cells(1, 1) = "Test"
   MsgBox xlSht(1).Name
   
   xlApp.Quit
   Set xlSht = Nothing
   Set xlWb = Nothing
   Set xlApp = Nothing
   
End Sub
0
 
jayhoganAuthor Commented:
these 2 routines work but they still open excel outside of vb.  What I've added is under Project - Componets - Insertable Objects - and then Microsoft Excel Worksheet.  Adding this allows me to create a sheet on a form.  That's the one I'm trying to edit.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Dave_GreeneCommented:
Just get rid of  "Set Obj1 = CreateObject("Excel.Application")"

Use Obj1 or whatever your control name is on the form  

Cheers
0
 
jayhoganAuthor Commented:
when i try to run

frmAddTags.Sheet1.Cells(1,1)

i get an error 'Method or Data Member not found
0
 
Dave_GreeneCommented:
looks like you are bypassing your excel object and coding right to the sheet level.  

frmAddTags.<EXCELControlNameHere>.Sheet1.Cells(1,1)
0
 
jayhoganAuthor Commented:
I figured it out. my line must read

frmAddTags.Sheet1.Object.Worksheets("Sheet1").Cells(1, 1) = "????"

I'm going to accept your answer because you did get me going towards the right direction. thanks
0
 
jayhoganAuthor Commented:
I figured it out. my line must read

frmAddTags.Sheet1.Object.Worksheets("Sheet1").Cells(1, 1) = "????"

I'm going to accept your answer because you did get me going towards the right direction. thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now