[Solved] VBA code for submitting details in two sheets of excel

sivanagaraju

New member
I tried to create an excel form for submitting the details which are to be entered in seperate sheets using youtube.
I need the all details to be entered in the sheet "Total" and also to respective sheets as selected in "Type"
I am able to enter the details in respective sheets as selected in "Type" but not in the sheet "Total"
Please help so that I could enter in both the sheets.
 

Attachments

Hello,

You’ve done the hard part already (writing to the sheet chosen in E6: Type). To also log the same entry to Total, write the values once into an array and append that row to both sheets. The macro below is a drop-in replacement that avoids Select/Activate (more reliable) and validates the Type sheet name.

Code:
Option Explicit
Private Function SheetExists(ByVal sName As String) As Boolean
    On Error Resume Next
    SheetExists = Not ThisWorkbook.Worksheets(sName) Is Nothing
    On Error GoTo 0
End Function

Public Sub ENTRY()
    Dim wsEntry As Worksheet, wsTotal As Worksheet, wsType As Worksheet
    Dim typeName As String
    Dim arr As Variant
    Dim rowType As Long, rowTot As Long

    Set wsEntry = ThisWorkbook.Worksheets("ENTRY")
    Set wsTotal = ThisWorkbook.Worksheets("Total")

    ' Read form values from ENTRY
    ' Order written to columns B:E:  E4, E6 (Type), E8, E10
    arr = Array(wsEntry.Range("E4").Value, _
                wsEntry.Range("E6").Value, _
                wsEntry.Range("E8").Value, _
                wsEntry.Range("E10").Value)

    ' Validate Type and get the destination sheet
    typeName = CStr(wsEntry.Range("E6").Value)
    If Len(typeName) = 0 Then
        MsgBox "Please choose a Type in cell E6.", vbExclamation
        Exit Sub
    End If
    If Not SheetExists(typeName) Then
        MsgBox "The sheet """ & typeName & """ does not exist.", vbCritical
        Exit Sub
    End If
    Set wsType = ThisWorkbook.Worksheets(typeName)

    ' Append to the Type-selected sheet
    rowType = wsType.Cells(wsType.Rows.Count, "B").End(xlUp).Row + 1
    wsType.Cells(rowType, "B").Resize(1, UBound(arr) + 1).Value = arr
    wsType.Cells(rowType, "A").Value = Now   ' optional timestamp in col A

    ' Also append to the Total sheet
    rowTot = wsTotal.Cells(wsTotal.Rows.Count, "B").End(xlUp).Row + 1
    wsTotal.Cells(rowTot, "B").Resize(1, UBound(arr) + 1).Value = arr
    wsTotal.Cells(rowTot, "A").Value = Now   ' optional timestamp in col A

    ' Clear the form inputs
    wsEntry.Range("E4,E6,E8,E10").ClearContents

    MsgBox "Entry saved to """ & typeName & """ and ""Total"".", vbInformation
End Sub

Public Sub RESET1()
    ThisWorkbook.Worksheets("ENTRY").Range("E4,E6,E8,E10").ClearContents
End Sub

This keeps your layout (writing to columns B:E) and simply ensures the same row goes to Total as well as the chosen Type sheet.
If your “Total” sheet needs extra columns (e.g., a running ID), say the word and I’ll add it.
 

Online statistics

Members online
0
Guests online
80
Total visitors
80

Forum statistics

Threads
435
Messages
1,922
Members
1,072
Latest member
8kbetreport
Back
Top