Excel

Userform with doubleclick filled with values

Ease of Use

Intermediate

Version tested with

2002 

Submitted by:

Charlize

Description:

Doubleclick anywhere in your sheet to bring up userform. If you click on existing row with data, data will be filled in in the form by using a call routine (otherwise it will be the last row with data). 

Discussion:

Easy alter some data by using a form with doubleclick and to go back and forth to another record. 

Code:

instructions for use

			

*** Paste this In worksheet1 code module Option Explicit Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) 'doubleclick anywhere on the sheet to 'show the form 'Leave edit mode Cancel = True 'call the procedure to fetch values with target GetData Target 'show the form UserForm1.Show End Sub *** End of worksheet module *** Paste this In the userform code Option Explicit Private Sub NextRecord_Click() 'Proceed to next record RowNum = RowNum + 1 If Cells(RowNum, 1).Value <> vbNullString Then GetData Cells(RowNum, 1) Me.Caption = "Record no.: " & RowNum - 1 Else 'It's not allowed to skip last record Beep RowNum = RowNum - 1 Me.Caption = "Last record in database !!!" End If End Sub Private Sub PreviousRecord_Click() 'Go back one record If RowNum = 2 Then RowNum = 2 Beep Me.Caption = "First record in database !!!" Else RowNum = RowNum - 1 Me.Caption = "Record no.: " & RowNum - 1 End If GetData Cells(RowNum, 1) End Sub Private Sub UserForm_Activate() Me.Caption = "Record no.: " & RowNum - 1 End Sub *** End of userform code *** Paste this In a normal module Option Explicit 'Used for positioning a record Public RowNum As Integer Sub GetData(Optional Target As Range) 'Check if value of targetcell <> empty If Not Target Is Nothing And Target(, 1).Value <> vbNullString Then RowNum = Target.Row Else 'If targetcell = empty substract 1 from the row 'until value of column A of the row <> vbnullstring RowNum = Target.Row Do While Cells(RowNum, 1).Value = vbNullString RowNum = RowNum - 1 Loop End If 'fill the form with the values With UserForm1 .TxtName.Value = Cells(RowNum, 2) .LblDate.Caption = Cells(RowNum, 1) .TxtServerName.Value = Cells(RowNum, 3) .TxtLocation.Value = Cells(RowNum, 4) End With End Sub *** End of module

How to use:

  1. Alt+F11 to go through vb editor
  2. right click on leftpane on your project and add a userform and normal code module
  3. the form needs two commandbuttons, 3 textboxes and 5 labels
  4. paste the appropriate codes (between ***) to the right place
 

Test the code:

  1. fill some values in column A, B, C and D
  2. click on a line with some values
  3. click anywhere in your sheet (even with no values)
 

Sample File:

Scroll_in_Form-v2.zip 9.01KB 

Approved by mdmackillop


This entry has been viewed 446 times.

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