Excel

Sample file to enter standard text in multiple columns from range of data sources

Ease of Use

Easy

Version tested with

2000 

Submitted by:

mdmackillop

Description:

The code shows how an abbreviated code can be used as the source for extended data selection and allows entry into multiple cells in a spreadsheet. The code allows for renaming of datasource sheets "on the fly". 

Discussion:

The code was created to assist recording of repetitive data and subsequent entry into a spreadsheet format. Noting down/dictating "2A Steel" in the attached example saves time for the surveyor and reduces errors in data entry where complex references are otherwise required. Although constructed for a specific purpose, the code is easily adjusted for individual needs. 

Code:

instructions for use

			

'>> Userform; 1 listbox, 1 textbox; Code as follows Option Explicit Option Compare Text Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean) 'Enter data on DoubleClick DoFill End Sub Private Sub ListBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) 'Enter data if Enter key is pressed If KeyAscii = 13 Then DoFill End Sub Private Sub UserForm_Initialize() 'Store shortcut TextBox1 = ActiveCell.Value 'Populate list from appropriate sheet ListBox1.List() = Range("Ref" & TextBox1).Value 'Select first item in listbox ListBox1.ListIndex = 0 End Sub Sub DoFill() Dim MyCell As String Dim Tmp1 As String, Tmp2 As String, Tmp3 As String, Tmp4 As String Application.ScreenUpdating = False MyCell = ActiveCell.Address Worksheets("sheet" & TextBox1).Select 'Read in values from selected columns, based on list order Tmp1 = Cells(ListBox1.ListIndex + 1, 3).Value Tmp2 = Cells(ListBox1.ListIndex + 1, 4).Value Tmp3 = Cells(ListBox1.ListIndex + 1, 5).Value Tmp4 = Cells(ListBox1.ListIndex + 1, 6).Value Worksheets("Data").Select 'Select starting cell Range(MyCell).Select 'Write in values to selected columns; adjust as required ActiveCell.Offset(0, 0).Formula = Tmp1 ActiveCell.Offset(0, 1).Formula = Tmp2 ActiveCell.Offset(0, 2).Formula = Tmp3 ActiveCell.Offset(0, 3).Formula = Tmp4 ActiveCell.Offset(0, 3).Select Unload UserForm1 Application.ScreenUpdating = True End Sub '>> Standard module code Sub Shows() On Error Resume Next UserForm1.Show False End Sub 'Called by WorkBook Open to create RangeNames Sub DoNames() Dim Nm, ShName As String, Ref As String Dim i As Integer 'Delete names starting with Ref to avoid conflict For Each Nm In ActiveWorkbook.Names If Left(Nm.Name, 3) = "Ref" Then Nm.Delete Next 'Create names for each sheet except first for Columns A:C For i = 2 To Sheets.Count Ref = "Ref" & Right(Sheets(i).Name, Len(Sheets(i).Name) - 5) ShName = "=" & Sheets(i).Name & "!A:C" ActiveWorkbook.Names.Add Name:=Ref, RefersTo:=ShName Next End Sub '>> "Sheet1" code Option Explicit Option Compare Text Private Sub Worksheet_Change(ByVal Target As Range) If Target.Column() = 6 And Target.Text <> "" Then DoNames 'Comment out this line if sheetname changes are not required 'during normal use. Names will be created on opening only. Target.Select Shows End If End Sub '>> Workbook code Private Sub Workbook_Open() 'Create RangeNames DoNames End Sub

How to use:

  1. See attached example for code stored in Worksheet, WorkBook, UserForm and Standard modules.
 

Test the code:

  1. Open the attached workbook
  2. In "Data", Column F enter 2A, 2B or 2C in any cell. Select an item from the listbox on the Userform for detailed information to be added to the spreadsheet.
  3. See textboxes for further information.
 

Sample File:

DataEntry.zip 21.1KB 

Approved by mdmackillop


This entry has been viewed 162 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express