Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21096

[RESOLVED] Write textbox data to a specified excel worksheet

$
0
0
Hi
I am a bit of a novice but eager to learn to keep the old grey matter exercised and would like help to achieve if possible the following:

I have an Excel Workbook saved as .xls in c:\Users\Mike\Documents\VB6Projects\Files which I am attempting to update via my VB Program/Form.

Data variables from the form are strvarId, strName, ckbNewMember,datDateLp,intExactHc,intPlayHc,intCat

How can I write this data to a specified Worksheet/Row without having the worksheet visible. In other words I want to do this in the background.

I have tried several suggested lumps of code but to no avail. The latest (shown below) gives me the following run-time error 424 - Object required


Private Sub Form_Load()
'do declare these variables you need to add a reference
'to the microsoft excel 'xx' object library.

'you need two text boxes and two command buttons
'on the form, an excel file in c:\book1.xls ** My system will not let me save to here even though I have administrator rights!!**
** so I created my file in the path shown below**

Dim xl As New Excel.Application
Dim xlsheet As Excel.Worksheet
Dim xlwbook As Excel.Workbook
End Sub
Private Sub Command1_Click()
'the benifit of placing numbers in (row, col) is that you
'can loop through different directions if required. I could
'have used column names like "A1" 'etc.

Text1.Text = xlsheet.Cells(2, 1) ' row 2 col 1 ** This is the line that produces the error **
Text2.Text = xlsheet.Cells(2, 2) ' row 2 col 2

'don't forget to do this or you'll not be able to open
'book1.xls again, untill you restart you pc.
xl.ActiveWorkbook.Close False, "c:\Users\Mike\Documents\VB6Projects\Files\Book1.xls"
xl.Quit
End Sub

Private Sub Command2_Click()
xlsheet.Cells(2, 1) = Text1.Text
xlsheet.Cells(2, 2) = Text2.Text
xlwbook.Save

'don't forget to do this or you'll not be able to open
'book1.xls again, untill you restart you pc.
xl.ActiveWorkbook.Close False, "c:\Users\Mike\Documents\VB6Projects\Files\Book1.xls"
xl.Quit
End Sub

Private Sub Form1_Load()
Set xlwbook = xl.Workbooks.Open("c:\Users\Mike\Documents\VB6Projects\Files\Book1.xls")
Set xlsheet = xlwbook.Sheets.Item(1)
End Sub

Private Sub Form1_Unload(Cancel As Integer)
Set xlwbook = Nothing
Set xl = Nothing
End Sub

Thanks in anticipation

Viewing all articles
Browse latest Browse all 21096

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>