Advertisement
profess79

Data entry sequence

Mar 3rd, 2023 (edited)
1,711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VisualBasic 2.28 KB | Source Code | 0 0
  1. Option Explicit
  2. Dim ChkErr      As Long
  3. Dim Txt1        As String
  4.  
  5. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  6.     On Error GoTo ErrLne
  7.     Dim L1          As Long
  8.     Dim L2          As Long
  9.     Static RngLast  As Range
  10.     If Not Application.Intersect(Target, Range("A1:ZZ65000")) Is Nothing Then
  11.         If Not RngLast Is Nothing Then
  12.             If Range(Target.Address).Column = 1 And Range(RngLast.Address).Column = 1 Then
  13.                 If RngLast.Address <> Target.Address Then
  14.                     'MsgBox "Lost focus " & RngLast.Address & " " & Range(RngLast.Address).Row & " " & Range(RngLast.Address).Column & " " & RngLast.Value & " Txt1=" & Txt1 & "(" & Len(Txt1) & ")"
  15.                    If Txt1 = "" Then
  16.                         If Not (RngLast.Value = "") Then
  17.                             L1 = 0
  18.                             For L2 = 1 To 65500
  19.                                 If Val(ActiveSheet.Cells(L2, 2)) > L1 Then L1 = Val(ActiveSheet.Cells(L2, 2))
  20.                             Next L2
  21.                             ActiveSheet.Cells(Range(RngLast.Address).Row, Range(RngLast.Address).Column + 1) = L1 + 1
  22.                         End If
  23.                     Else
  24.                         If RngLast.Value = "" Then
  25.                             L1 = ActiveSheet.Cells(Range(RngLast.Address).Row, Range(RngLast.Address).Column + 1)
  26.                             ActiveSheet.Cells(Range(RngLast.Address).Row, Range(RngLast.Address).Column + 1) = ""
  27.                             For L2 = 1 To 65500
  28.                                 If Val(ActiveSheet.Cells(L2, 2)) > L1 Then ActiveSheet.Cells(L2, 2) = Val(ActiveSheet.Cells(L2, 2)) - 1
  29.                             Next L2
  30.                         End If
  31.                     End If
  32.                 End If
  33.             End If
  34.         End If
  35.  
  36.         If Range(Target.Address).Column = 1 Then
  37.             Txt1 = Target.Value
  38.             'MsgBox "Got focus " & Target.Address & " " & Range(Target.Address).Row & " " & Range(Target.Address).Column & " " & Target.Value & " Txt1=" & Txt1 & "(" & Len(Txt1) & ")"
  39.        End If
  40.         Set RngLast = Target
  41.     End If
  42.    
  43.     Exit Sub
  44.  
  45. ErrLne:
  46.     ChkErr = Err.Number
  47.     MsgBox "Error " & Err.Number & " : " & Err.Description, vbInformation
  48.     Resume Next
  49.  
  50. End Sub
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement