VBA to create formated sign-in sheet from database

Rog84

New Member
Joined
Apr 6, 2017
Messages
9
Good day everyone,

I hope you will be able to give me a hand with this, basically I have an excel workbook where the first tab is a list of shifts associated with the employee name and the date sorted in order of shift and date.

Then I've created a 2nd tab called "Sign-in Creator", where you can enter the date needed (Usually we prepare this for the next day) then you click on the button where I would like to associate a VBA to create a new worksheet named with the date and formats it to a sign-in sheet and add the proper number of lines needed depending on how many agents or working.

I've also added a tab as an example to give an idea of what I would like to get as a result.

Thanks,
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Here is the document : Sign-in sheet.xlsx - Google Drive

Thanks,

Good day everyone,

I hope you will be able to give me a hand with this, basically I have an excel workbook where the first tab is a list of shifts associated with the employee name and the date sorted in order of shift and date.

Then I've created a 2nd tab called "Sign-in Creator", where you can enter the date needed (Usually we prepare this for the next day) then you click on the button where I would like to associate a VBA to create a new worksheet named with the date and formats it to a sign-in sheet and add the proper number of lines needed depending on how many agents or working.

I've also added a tab as an example to give an idea of what I would like to get as a result.

Thanks,
 
Upvote 0
Hia
I've uploaded a modified file here
Sign-in sheet.xls - Google Drive
I've turned one of your example sheets into a template for the macro to use. For reference the code is
Code:
Sub CreateSht()

    Dim Rng As Range
    Dim Dte As String
    Dim Rw As Integer
    Dim UsdRws As Integer
    Dim Agnts As Integer

Application.ScreenUpdating = False

    UsdRws = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row
    Dte = Format(Sheets("Sign-in creator").Range("B2"), "dd MMM YYYY")
    
    On Error Resume Next
    Sheets(Dte).Activate
    On Error GoTo 0
    If ActiveSheet.Name = Dte Then
        MsgBox "Sheet " & Dte & "already exists"
        Exit Sub
    End If
    
    Sheets("Template").Copy after:=Sheets(Sheets.Count)
    ActiveSheet.Name = Dte
    
    Agnts = WorksheetFunction.CountIf(Sheets("Data").Columns(1), Dte)
    
    Rw = 3
    With Sheets(Dte)
        .Range("B1").Value = Dte
        .Range("A4").Resize(Agnts - 2).EntireRow.Insert
        For Each Rng In Sheets("Data").Range("A2:A" & UsdRws)
            If Format(Rng.Value, "dd MMM YYYY") = Dte Then
                .Range("A" & Rw).Value = Rng.Offset(, 2).Value
                .Range("B" & Rw).Value = Rng.Offset(, 1).Value
                Rw = Rw + 1
            End If
        Next Rng
        .Range("M3").Resize(Agnts).Formula = "=[COLOR=#0000ff]A1&B1[/COLOR]"
    End With
    
Application.ScreenUpdating = True
    
End Sub
You'll need to convert this file into an .xlsm file.
Also it looked like you had a formula in column M, if so, modify the code in blue (above) or else delete that line of code.
HTH
 
Upvote 0
Thank you so much Fluff for taking the time of doing this !

I'm having a little issue with the macro though, even after converting the file to .xlsm, I always get the following error message..

"Run-time error '1004':

Application-defined or object-defined error"

The new is created anyways, but the rows doesn't match the number of people working that day and the cells for the names are also blank (no names), I'm not sure if it's related, but I've noticed the date in cell B4 says for example "25 juil 2017" instead of "25 Jul 2017", maybe it's related ?

Thank you so much again!

Rog

Hia
I've uploaded a modified file here
Sign-in sheet.xls - Google Drive
I've turned one of your example sheets into a template for the macro to use. For reference the code is
Code:
Sub CreateSht()

    Dim Rng As Range
    Dim Dte As String
    Dim Rw As Integer
    Dim UsdRws As Integer
    Dim Agnts As Integer

Application.ScreenUpdating = False

    UsdRws = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row
    Dte = Format(Sheets("Sign-in creator").Range("B2"), "dd MMM YYYY")
    
    On Error Resume Next
    Sheets(Dte).Activate
    On Error GoTo 0
    If ActiveSheet.Name = Dte Then
        MsgBox "Sheet " & Dte & "already exists"
        Exit Sub
    End If
    
    Sheets("Template").Copy after:=Sheets(Sheets.Count)
    ActiveSheet.Name = Dte
    
    Agnts = WorksheetFunction.CountIf(Sheets("Data").Columns(1), Dte)
    
    Rw = 3
    With Sheets(Dte)
        .Range("B1").Value = Dte
        .Range("A4").Resize(Agnts - 2).EntireRow.Insert
        For Each Rng In Sheets("Data").Range("A2:A" & UsdRws)
            If Format(Rng.Value, "dd MMM YYYY") = Dte Then
                .Range("A" & Rw).Value = Rng.Offset(, 2).Value
                .Range("B" & Rw).Value = Rng.Offset(, 1).Value
                Rw = Rw + 1
            End If
        Next Rng
        .Range("M3").Resize(Agnts).Formula = "=[COLOR=#0000ff]A1&B1[/COLOR]"
    End With
    
Application.ScreenUpdating = True
    
End Sub
You'll need to convert this file into an .xlsm file.
Also it looked like you had a formula in column M, if so, modify the code in blue (above) or else delete that line of code.
HTH
 
Upvote 0
Nevermind, the problem was my language setting !

It works like a charm, thank you so much Fluff :) !
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,999
Messages
6,128,192
Members
449,431
Latest member
Taekwon

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