Wednesday, September 11, 2013

How to Transfer Data From UserForm to a Worksheet


How to Transfer Data From a UserForm to a Worksheet
1. Open Microsoft Excel and create a new blank workbook by clicking on the File menu and selecting 'New.' Save the Excel file as 'update_worksheet.xls.'
2. Keep the workbook open and open the Visual Basic Editor by hitting Alt-F11. Create a new UserForm by clicking on the Insert menu and selecting 'UserForm.' Name the UserForm 'transferForm' by changing the Name attribute in the Properties menu. Change the Caption attribute to 'transferForm.'
3. Add a textbox to the form by selecting a textbox from the toolbox and dragging it into place on the form. Name the textbox 'transferInput' in the properties window. If the toolbox is not open, select the View menu and Toolbox.
4. Add an update button to the form by selecting a CommandButton from the toolbox and dragging it into place on the form. Name the CommandButton 'transferButton' in the properties window and change the caption attribute to 'Update Worksheet.'
5. Add a close button to the form by selecting a second CommandButton from the toolbox and dragging it into place on the form. Name the CommandButton 'closeButton' in the properties window and change the caption attribute to 'Close Form.'
6. Open the Visual Basic code editor by double-clicking on the transferButton (update button). Add code to the transferButton _click() function that transfers any data entered into the textbox to the update_worksheet.xls worksheet when the user clicks the button. Close the Visual Basic code editor.Private Sub transferButton_Click()Dim transferWorksheet as WorksheetSet transferWorksheet = Worksheets('Sheet1')transferWorksheet.Cells(1,1).Value = Me.transferInput.ValueEnd Sub
7. Reopen the Visual Basic code editor by double-clicking on the closeButton (close button). Add code to the closeButton_Click() function that closes the form when the user clicks the button. Close the Visual Basic code editor.Private Sub closeButton_Click()Unload MeEnd Sub
8. Run the form's code by clicking on the Run menu and selecting 'Run Sub/UserForm.' Enter data into the input field and click the 'transferButton.' Ensure that the data is successfully transferred to the first cell value in the Excel worksheet, switching to Excel if necessary. Save and close the running UserForm.
9. Use an Excel event procedure to open the UserForm when a user opens the workbook in Excel. Open the Project Explorer and double-click the ThisWorkbook code window. Enter a macro that opens the form when the worksheet opens and save the macro. Save and close Visual Basic and update_worksheet.xls.Private Sub Workbook_Open()transferForm.ShowEnd Sub
10. Reopen 'update_worksheet.xls.' The UserForm transferForm will open. Type some text into the 'transferInput' field and click the 'transferButton.' The text entered into the transferForm UserForm will be transferred to the first cell in Sheet1 in update_worksheet.xls.

Blogger news