ellyna
Banned - Rules violations
- Joined
- Nov 29, 2020
- Messages
- 89
- Office Version
- 2013
- Platform
- Windows
I have a VBA multipage. I faced a problem on my excel sheet as the value I enter in user form do not show in the excel sheet.
VBA Code:
Option Explicit
Sub AddRecord(ByVal Form As Object)
'dmt32 Dec 2020
Dim myWorksheet As Worksheet
Dim NewRecord As Range
Dim ctrl As Control
Dim c As Integer, PageNo As Integer, i As Integer
Dim NextNo As Long
Dim SheetName As String
Dim Response As VbMsgBoxResult
Dim ActivePage As Object
On Error GoTo myerror
With Form.MultiPage1
'get worksheet name from page tab
SheetName = .Pages(.Value).Caption
'get page no
PageNo = .Value
End With
'inform user
Response = MsgBox("Do you want To save the data To " & SheetName & " ?", 36, "Confirmation")
If Response = vbNo Then Exit Sub
'set object variable to worksheet
Set myWorksheet = ThisWorkbook.Worksheets(SheetName)
'set object variable to Active Page
Set ActivePage = Form.MultiPage1.Pages(PageNo)
With myWorksheet
'get next number
NextNo = Val(Application.Max(.Columns(1)) + 1)
'get next blank cell in range
Set NewRecord = .Cells(.Cells(.Rows.Count, "A").End(xlUp).Row + 1, 1)
End With
'add new record
c = 1
With NewRecord
.Value = NextNo
.Offset(, 2).Value = Now()
.Offset(, 2).NumberFormat = "dd-mm-yyyy | HH:mm:ss"
End With
'index each control on selected page
For i = 1 To ActivePage.Controls.Count
'loop through each textbox or combobox control on page
For Each ctrl In ActivePage.Controls
'to ensure each control posts data in correct order,
'we check if control tabindex matches index value
If ctrl.TabIndex = i - 1 Then
If TypeName(ctrl) = "TextBox" Or TypeName(ctrl) = "ComboBox" Then
'post record to range
With NewRecord.Offset(, IIf(c > 1, c + 1, c))
'check for date format & value
If ctrl.Value Like "##/##/####" And IsDate(ctrl.Value) Then
.Value = DateValue(ctrl.Value)
Else
.Value = ctrl.Value
End If
End With
'clear control
'1st control is date textbox so keep its value and setfocus
If ctrl.TabIndex > 0 Then ctrl.Value = "" Else ctrl.SetFocus
c = c + 1
End If
End If
Next ctrl
Next i
'inform user
MsgBox "Record Added To Sheet: " & SheetName, 64, "Record Added"
myerror:
'report errors
If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub