George_Solomon
asked on
Displaying row values from excel on a form
I have an excel spreadsheet and I have data in rows A1:A5. What I want is when a user clicks a buttom on a user form, the data in row A1 is displayed in a textbox. If the user clicks the button again, then the data from row 2 will be diplayed in the textbox.... Any help is GREATLY APPRECIATED...
Thanks,
GS
Thanks,
GS
ok first of all you set the control source property of the textbox to A1 then If you give me a min I will post some of my PAQ's to show you how to do this :)
Take a look at this :
https://www.experts-exchange.com/questions/21335618/Excel-VBA-Code-Problem-S.html
And scroll down to the end :) There is my other 2nd PAQ, that should help you with that :)
https://www.experts-exchange.com/questions/21335618/Excel-VBA-Code-Problem-S.html
And scroll down to the end :) There is my other 2nd PAQ, that should help you with that :)
btw you insert that code into the command button making sure that you have the textbox name instead of the name I have given them and you will need to alter the code slightly but that gives you exactly what you are looking for as far as I can tell :)
In VB6 you could add a reference to the Microsoft Excel Object Library but what if the user is on Row 5 and presses the button again. Do you want it to go back to Row 1 and start over? This example will do that:
' add reference to Microsoft Excel Object Library
' declarations area of form
Dim xl As New Excel.Application
Dim wb As Excel.Workbook, ws As Excel.Worksheet
Dim x As Integer
Private Sub Form_Load()
Set wb = xl.Workbooks.Open("C:\myfi le.xls")
Set ws = wb.Sheets("Sheet1")
x = 0
End Sub
Private Sub Command1_Click()
If x = 5 Then x = 0
x = x + 1
Text1.Text = ws.Range("A" & x).Value
End Sub
Private Sub Form_Unload(Cancel As Integer)
wb.Close
xl.Quit
Set ws = Nothing
Set wb = Nothing
Set xl = Nothing
End Sub
' add reference to Microsoft Excel Object Library
' declarations area of form
Dim xl As New Excel.Application
Dim wb As Excel.Workbook, ws As Excel.Worksheet
Dim x As Integer
Private Sub Form_Load()
Set wb = xl.Workbooks.Open("C:\myfi
Set ws = wb.Sheets("Sheet1")
x = 0
End Sub
Private Sub Command1_Click()
If x = 5 Then x = 0
x = x + 1
Text1.Text = ws.Range("A" & x).Value
End Sub
Private Sub Form_Unload(Cancel As Integer)
wb.Close
xl.Quit
Set ws = Nothing
Set wb = Nothing
Set xl = Nothing
End Sub
ASKER
vinnyd79,
This is how the program should work:
1. The user scans a serial number and presses a button on the form. Pressing the button will create an excel sheet that has the serial number as the file name and create a director named results and save the file there (c:\results\1235.xls)
2. As the user answers each of the 5 questions at appear on the form (question asked from form,yes, no,comment), the data will written to the spreadsheet created with the serial number until all 5 step have been completed. At this time a message will appear in the text windo "ALL DONE"
This is how the program should work:
1. The user scans a serial number and presses a button on the form. Pressing the button will create an excel sheet that has the serial number as the file name and create a director named results and save the file there (c:\results\1235.xls)
2. As the user answers each of the 5 questions at appear on the form (question asked from form,yes, no,comment), the data will written to the spreadsheet created with the serial number until all 5 step have been completed. At this time a message will appear in the text windo "ALL DONE"
As I said earlier, user ADODC control and treat Excel Sheet as a database.
Thus, each column in excel sheet becoms column of your database table and each row becomes a row in the table (in your example - 5)
You do not have to do anything extra - just treat excel sheet as a database.
Amit
Thus, each column in excel sheet becoms column of your database table and each row becomes a row in the table (in your example - 5)
You do not have to do anything extra - just treat excel sheet as a database.
Amit
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You do not have to read cell by cell (as shown above) if you implment using ADoDC control
Amit
Amit
ASKER
Vinnyd79,
How do I add a reference to Microsoft Excel Object Library
How do I add a reference to Microsoft Excel Object Library
In VB goto the Projects Menu and select References,then Select Microsoft Excel Object Library. Personally I would not use an ADODC control,but you could.
If VB
1. Connect to the Excel using ADODC control, read and display
If PB
1. Connect to Excel using datawindow and retrieve
Let me know and i can help u with either or any other options