excel sheet not showing the value after submit the user form

ellyna

Banned - Rules violations
Joined
Nov 29, 2020
Messages
89
Office Version
  1. 2013
Platform
  1. 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
 

Some videos you may like

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,388
Office Version
  1. 2019
Platform
  1. Windows
delete following line

Rich (BB code):
'1st control is date textbox so keep its value and setfocus
                    If ctrl.TabIndex > 0 Then ctrl.Value = "" Else ctrl.SetFocus

Dave
 
Solution

ellyna

Banned - Rules violations
Joined
Nov 29, 2020
Messages
89
Office Version
  1. 2013
Platform
  1. Windows
delete following line

Rich (BB code):
'1st control is date textbox so keep its value and setfocus
                    If ctrl.TabIndex > 0 Then ctrl.Value = "" Else ctrl.SetFocus

Dave
ok Thank you
 

Watch MrExcel Video

Forum statistics

Threads
1,123,281
Messages
5,600,711
Members
414,401
Latest member
grenona2020

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top