VBA code for a User form

Atlantis764

New Member
Joined
Jan 10, 2022
Messages
17
Office Version
  1. 2019
Platform
  1. Windows
Hi all,

I have a User Form and when I enter a new record all the data are transferred to another sheet (Database).
All the records are filled on column E but I want to be filled on the corresponding column (based on column B -Year and column D - Month).
I filled with yellow the cells where I need the values to be imputed.

Book1.xlsm
J
10
Database


The VBA code that I am using is this:

VBA Code:
Option Explicit
Sub Reset()
    Dim iRow As Long   
    iRow = [Counta(Database!A:A)]   
    With UserForm1
        .TxtHours.Value = ""
       
        .CmbYear.Clear
        .CmbName.Clear
        .CmbMonth.Clear
       
        .CmbYear.AddItem "2022"
        .CmbYear.AddItem "2023"
        .CmbYear.AddItem "2024"
       
        .CmbName.AddItem "aaa"
        .CmbName.AddItem "bbb"
        .CmbName.AddItem "ccc"
        .CmbName.AddItem "ddd"
        .CmbName.AddItem "eee"
       
        .CmbMonth.AddItem "January"
        .CmbMonth.AddItem "February"
        .CmbMonth.AddItem "March"
        .CmbMonth.AddItem "April"
        .CmbMonth.AddItem "May"
        .CmbMonth.AddItem "June"
        .CmbMonth.AddItem "July"
        .CmbMonth.AddItem "August"
        .CmbMonth.AddItem "September"
        .CmbMonth.AddItem "October"
        .CmbMonth.AddItem "November"
        .CmbMonth.AddItem "December"
   
        .LstDatabase.ColumnCount = 7
        .LstDatabase.ColumnHeads = True
       
        .LstDatabase.ColumnWidths = "30,50,60,60,30,30,30"       
        If iRow > 1 Then
            .LstDatabase.RowSource = "Database!A2:AB" & iRow
        Else
            .LstDatabase.RowSource = "Database!A2:AB2"
        End If          
    End With
End Sub

Sub Submit()
    Dim sh As Worksheet
    Dim iRow As Long   
    Set sh = ThisWorkbook.Sheets("Database")   
    iRow = [Counta(Database!A:A)] + 1   
    With sh       
        .Cells(iRow, 1) = iRow - 1
        .Cells(iRow, 2) = UserForm1.CmbYear.Value
        .Cells(iRow, 3) = UserForm1.CmbName.Value
        .Cells(iRow, 4) = UserForm1.CmbMonth.Value
        .Cells(iRow, 5) = UserForm1.TxtHours.Value
        .Cells(iRow, 29) = Application.UserName   
    End With
End Sub

Sub Show_Form()
    UserForm1.Show
End Sub

Thanks for all your help!
 

Attachments

  • Database_sheet.jpg
    Database_sheet.jpg
    136.3 KB · Views: 16
  • UserForm.jpg
    UserForm.jpg
    121.2 KB · Views: 13
Last edited by a moderator:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi,

Change Submit code as below:

VBA Code:
Sub Submit()
    Dim sh As Worksheet
    Dim iRow As Long, colno As Integer, iCol As Long
    Set sh = ThisWorkbook.Sheets("Database")
    iRow = [Counta(Database!A:A)] + 1
    iCol = Sheets("Database").Cells(1, Columns.Count).End(xlToLeft).Column - 1

    With sh
        .Cells(iRow, 1) = iRow - 1
        .Cells(iRow, 2) = UserForm1.CmbYear.Value
        .Cells(iRow, 3) = UserForm1.CmbName.Value
        .Cells(iRow, 4) = UserForm1.CmbMonth.Value
        For colno = 5 To iCol
            If UserForm1.CmbMonth.Value = Format(.Cells(1, colno), "MMMM") And _
            UserForm1.CmbYear.Value = Format(.Cells(1, colno), "YYYY") Then
                .Cells(iRow, colno) = UserForm1.txtHours.Value
            End If
        Next
        .Cells(iRow, iCol+1) = Application.UserName
    End With
End Sub
 
Upvote 0
Solution
Hi,

Change Submit code as below:

VBA Code:
Sub Submit()
    Dim sh As Worksheet
    Dim iRow As Long, colno As Integer, iCol As Long
    Set sh = ThisWorkbook.Sheets("Database")
    iRow = [Counta(Database!A:A)] + 1
    iCol = Sheets("Database").Cells(1, Columns.Count).End(xlToLeft).Column - 1

    With sh
        .Cells(iRow, 1) = iRow - 1
        .Cells(iRow, 2) = UserForm1.CmbYear.Value
        .Cells(iRow, 3) = UserForm1.CmbName.Value
        .Cells(iRow, 4) = UserForm1.CmbMonth.Value
        For colno = 5 To iCol
            If UserForm1.CmbMonth.Value = Format(.Cells(1, colno), "MMMM") And _
            UserForm1.CmbYear.Value = Format(.Cells(1, colno), "YYYY") Then
                .Cells(iRow, colno) = UserForm1.txtHours.Value
            End If
        Next
        .Cells(iRow, iCol+1) = Application.UserName
    End With
End Sub
Thank you very much!!!
The code works great!
 
Upvote 0

Forum statistics

Threads
1,214,391
Messages
6,119,239
Members
448,879
Latest member
VanGirl

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
Back
Top