PDA

View Full Version : Word updates Excel Help



Jchess
06-06-2012, 08:07 AM
I have an Excel sheet with Option Buttons used to add a value into a cell on the same sheet. I use the following code to do this.

Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then Range("D15").Value = 1
End Sub

Private Sub OptionButton2_Click()
If OptionButton2.Value = True Then Range("D15").Value = 2
End Sub

Private Sub OptionButton3_Click()
If OptionButton3.Value = True Then Range("D15").Value = 3
End Sub

Private Sub OptionButton4_Click()
If OptionButton4.Value = True Then Range("D15").Value = 4
End Sub

What I'm now trying to do is to have the Excel sheet updated from a Word document. I want the Option Buttons to be in a Word document and for the selection to update the Excel sheet. I'm using Office 2003.

If anyone can point me in the right direction I'd really be grateful.

Tinbendr
06-07-2012, 03:41 AM
This example uses a commandbutton event to 'update' the Excel file.

Private Sub CommandButton1_Click()
Dim oXL As Object
Dim WB As Object

Set oXL = CreateObject("Excel.Application")

Set WB = oXL.workbooks.Open("C:\Documents and Settings\Owner\My Documents\VBA\Excel to Word.xls")

Select Case True

Case OptionButton1
WB.worksheets(1).Range("D15").Value = 1

Case OptionButton2
WB.worksheets(1).Range("D15").Value = 2

Case OptionButton3
WB.worksheets(1).Range("D15").Value = 3

Case OptionButton4
WB.worksheets(1).Range("D15").Value = 4
End Select

WB.Close True
Set WB = Nothing
Set oXL = Nothing

Unload Me

End Sub

Jchess
06-07-2012, 09:20 AM
Thank you for taking the time to respond to my post. I commented out the Unload Me statement since it was undefined. I changed the Set WB line to reflect the name of my Excel sheet and its location. The macro runs, but nothing seems to happen. I also changed the Range in my sheet to reflect the cell number that I wanted to update, both using the cell location F4 and by using the name of the cell which is Knowledge. Still nothing happens. Did I do something wrong. I'm using the code modified as follows:
Private Sub CommandButton1_Click()
Dim oXL As Object
Dim WB As Object

Set oXL = CreateObject("Excel.Application")

Set WB = oXL.workbooks.Open("D:\VBA\Summary.xls")

Select Case True

Case OptionButton37
WB.worksheets(1).Range("Knowledge").Value = 1

Case OptionButton38
WB.worksheets(1).Range("Knowledge").Value = 2

Case OptionButton39
WB.worksheets(1).Range("Knowledge").Value = 3

Case OptionButton40
WB.worksheets(1).Range("Knowledge").Value = 4
End Select

WB.Close True
Set WB = Nothing
Set oXL = Nothing

' Unload Me

End Sub

Jchess
06-07-2012, 09:40 AM
Disregard the last post. It Worked!

I just realized that I was using Check Boxes instead of Option Boxes.

Thank you so very much for taking the time to help me with this problem.

Jchess
06-07-2012, 09:44 AM
Thank you for your response to my post. Your solution worked perfectly. I am very grateful.

James Chesshire