Advertisement
Guest User

Excel userform code

a guest
Oct 16th, 2024
130
0
11 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.15 KB | Source Code | 0 0
  1.  
  2. Option Explicit
  3.  
  4. Private Sub cmdCancel_Click()
  5.  
  6. Unload Me
  7.  
  8. End Sub
  9.  
  10. Private Sub createFldr(fldr As String)
  11.  
  12. fldr = Left(Application.UserLibraryPath, InStr(10, Application.UserLibraryPath, "\")) & _
  13.         filepath & _
  14.         "\" & fldr
  15.  
  16. If Dir(fldr, vbDirectory) = "" Then
  17.     Debug.Print (fldr)
  18.     MkDir (fldr)
  19. Else
  20.     MsgBox ("Folder already created")
  21. End If
  22.  
  23. End Sub
  24.  
  25. Private Function OKerrCheck(obj As Object, nam As String) As String
  26.  
  27. Dim msg As String
  28.  
  29. msg = "Select a " & nam & vbNewLine
  30.  
  31. With obj
  32.     If .Value = "" Then
  33.         OKerrCheck = msg
  34.     ElseIf .ListIndex = -1 Then
  35.         If MsgBox(nam & " not found. Do you want to add it to the list?", vbYesNo) = 7 Then OKerrCheck = msg
  36.     Else
  37.         OKerrCheck = ""
  38.     End If
  39. End With
  40.  
  41. End Function
  42.  
  43. Private Sub cmdOK_Click()
  44.  
  45. Application.ScreenUpdating = False
  46. Application.Calculation = xlCalculationManual
  47.  
  48. Dim assNum As Range
  49. Dim response As Integer
  50. Dim errmsg As String
  51. Set assNum = Sheet3.Cells(2, 6)
  52.  
  53. 'Error checking (adds text to the error message. If the message isn't blank then the sub is exited)
  54. With Me
  55.    
  56.     If .txtDesc = "" Then errmsg = errmsg & "Description missing" & vbNewLine
  57.     If .txtSN = "" Then errmsg = errmsg & "Serial Number missing" & vbNewLine
  58.     If .txtModel = "" Then errmsg = errmsg & "Model Number missing" & vbNewLine
  59.    
  60.     errmsg = errmsg & OKerrCheck(.cmbCat, "Category")
  61.     errmsg = errmsg & OKerrCheck(.cmbManuf, "Manufacturer")
  62.     errmsg = errmsg & OKerrCheck(.cmbSupplier, "Supplier")
  63.     errmsg = errmsg & OKerrCheck(.cmbLocat, "Location")
  64.  
  65.     If .cmbSys.Value = "" Then
  66.         errmsg = errmsg & "Select a System " & vbNewLine
  67.     ElseIf .cmbSys.ListIndex = -1 Then
  68.         response = MsgBox("System not found. Do you want to create a new System?", vbYesNoCancel)
  69.         If response = 2 Then
  70.             Exit Sub
  71.         ElseIf response = 7 Then
  72.             errmsg = errmsg & "Select a System " & vbNewLine
  73.         ElseIf response = 6 Then
  74.             createFldr (.cmbSys.Value)
  75.         End If
  76.     End If
  77.    
  78.     If .chkPPM And .cmbPPMFreq.ListIndex = -1 Then errmsg = errmsg & "Select a Physical Maintenance frequency " & vbNewLine
  79.     If .chkICal And .cmbICalFreq.ListIndex = -1 Then errmsg = errmsg & "Select an Internal Calibration frequency " & vbNewLine
  80.     If .chkECal And .cmbECalFreq.ListIndex = -1 Then errmsg = errmsg & "Select an External Calibration frequency " & vbNewLine
  81. End With
  82.  
  83. If Not errmsg = "" Then
  84.     errmsg = Left(errmsg, Len(errmsg) - 1)
  85.     MsgBox (errmsg)
  86.     Exit Sub
  87. End If
  88.  
  89.  
  90. With Sheet2.ListObjects("Table2")
  91. '   Add new row
  92.     .ListRows.Add (1)
  93.     .ListRows(2).Range.Copy
  94.     .ListRows(1).Range.PasteSpecial xlPasteFormats
  95.    
  96.     assNum = assNum + 1
  97.    
  98. '   Populate asset list
  99.     With .ListRows(1)
  100.         .Range(1) = "PAC-" & assNum
  101.         .Range(2) = Me.txtDesc.Value
  102.         .Range(3) = Me.txtSN.Value
  103.         .Range(4) = Me.txtModel.Value
  104.         .Range(5) = Me.cmbCat.Value
  105.         .Range(6) = Me.cmbManuf.Value
  106.         .Range(7) = Me.cmbSupplier.Value
  107.         .Range(8) = Me.cmbLocat.Value
  108.         .Range(9) = Me.cmbSys.Value
  109.         .Range(10) = Me.txtTag.Value
  110.         .Range(11) = Date
  111.  
  112.         If Me.chkATEX Then .Range(12) = "5 Yearly" Else: .Range(12) = "-"
  113.         If Me.chkStat Then .Range(15) = Date Else: .Range(15) = "-"
  114.         If Me.chkICal Then .Range(17) = Me.cmbICalFreq.List(Me.cmbICalFreq.ListIndex, 1) Else: .Range(17) = "-"
  115.         If Me.chkECal Then
  116.             .Range(20) = Me.cmbECalFreq.List(Me.cmbECalFreq.ListIndex, 1)
  117.             .Range(21) = Date
  118.         Else
  119.             .Range(20) = "-": .Range(21) = "-"
  120.         End If
  121.         If Not Me.chkElec Then .Range(23) = "-"
  122.         If Me.chkPPM Then
  123.             .Range(25) = Me.cmbPPMFreq.List(Me.cmbPPMFreq.ListIndex, 1)
  124.             .Range(26) = Date
  125.         Else
  126.             .Range(25) = "-": .Range(26) = "-"
  127.         End If
  128.        
  129.         'Create Asset folder
  130.         createFldr (Me.cmbSys.Value & "\" & .Range(1).Value)
  131.        
  132.     End With
  133. End With
  134.  
  135. Application.ScreenUpdating = True
  136. Application.Calculation = xlCalculationAutomatic
  137.  
  138. End Sub
  139.  
  140. Public Sub UserForm_Initialize()
  141.  
  142. Dim arr As Variant, transArr(0 To 6, 0 To 1) As Variant
  143. Dim i As Integer, j As Integer
  144.  
  145. arr = Array(Array(Null, "1m", "3m", "6m", "1y", "3y", "5y"), Array(Null, "Monthly", "Quarterly", "6 Monthly", "Yearly", "3 Yearly", "5 Yearly"))
  146.  
  147. For i = 0 To 1
  148.     For j = 0 To 6
  149.         transArr(j, i) = arr(i)(j)
  150.     Next j
  151. Next i
  152.    
  153. With Application.WorksheetFunction
  154.     Me.cmbCat.List = .Sort(.Unique(Range("Table2[Equipment Category]").Value))
  155.     Me.cmbManuf.List = .Sort(.Unique(Range("Table2[Manufacturer]").Value))
  156.     Me.cmbSupplier.List = .Sort(.Unique(Range("Table2[Supplier]").Value))
  157.     Me.cmbLocat.List = .Sort(.Unique(Range("Table2[Location]").Value))
  158.     Me.cmbSys.List = .Sort(.Unique(Range("Table2[System Related To]").Value))
  159. End With
  160.  
  161. Me.cmbPPMFreq.List = transArr
  162. Me.cmbICalFreq.List = transArr
  163. Me.cmbECalFreq.List = transArr
  164.  
  165. Me.StartUpPosition = 0
  166. Me.Top = frmCtrl.Top + frmCtrl.Height + 0
  167. Me.Left = Application.Left + Application.Width - Me.Width - 25
  168.  
  169. End Sub
  170.  
Tags: Excel macro
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement