Enter data and generate a new sheet with that data

JessyVal

New Member
Joined
Feb 23, 2021
Messages
30
Office Version
  1. 365
Hi Guys, I am trying to do the following:
1. I have a Master sheet, in this sheet I need to enter some details regarding patients, once I click Save entry, I want three things to happen.

a) Generate a new sheet called as the patient name and I want to see all the patient information
b) Populate another sheet called (Database) with all the entries that I add
c) Once I click Save entry and point a and b happened I want to clear the row 3 in Master (to be able to enter another patient)

I was using this code and was genereting the sheets witht the name and the patient information, and was populating the database sheet too, however, in the database sheet did not go to the next row, just overwrite the first row, but for some reason stoped working.

Sub createPatientSheet()
Dim lastRow As Long
Dim cont As Long
Dim patientName As String
Dim exist As Boolean

On Error Resume Next
lastRow = sheetMaster.Range("A" & Rows.Count).End(xlUp).Row
For cont = 3 To lastRow
exist = False
patientName = sheetMaster.Cells(cont, 1)
exist = Not Worksheets(Master) Is Nothing
If exist = False Then
sheetTemplate.copy after:=Worksheets(Sheets.Count)
activeSheet.Name = patientName
End If

Sheets("Master").Range("A3:AS3").copy
Sheets("Active.sheet").Activate
Range("A2").Select
activeSheet.Paste
Application.CutCopyMode = False

Sheets("Master").Range("A3:AS3").copy
Sheets("Active.sheet").Activate
Range("A2").Select
Sheets("DataBAse").Paste
Application.CutCopyMode = False

Next cont

Worksheets("Master").Range("a3:as3").ClearContents

End Sub

1642333554459.png




a)
1642333884621.png



b)
1642334012263.png
 

Attachments

  • 1642333341689.png
    1642333341689.png
    52.4 KB · Views: 13
  • 1642333600904.png
    1642333600904.png
    46 KB · Views: 12

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Assuming you have the "Template" sheet hidden in this same workbook, maybe this will do the trick
VBA Code:
Sub AddToPatientSheet()
    Dim patientName As String
    Dim patientSheet As Worksheet
    Dim nextRow As Long
    
' a) Generate a new sheet called as the patient name and I want to see all the patient information
    patientName = Sheets("Master").Range("A3")
    ' check if patient already has a sheet
    On Error Resume Next    'in case sheet does not already exist
    Set patientSheet = Sheets(patientName)
    On Error GoTo 0         're-enable error notification
    If Not patientSheet Is Nothing Then
            'use this existing sheet
            'it is already set
    Else
        'will need to create the sheet from Template
        With Sheets("Template")
            .Visible = True
            .Copy after:=Sheets(Sheets.Count)
            ActiveSheet.Name = patientName
            .Visible = False
        End With
        Set patientSheet = Sheets(patientName)
    End If
    'copy from Master to patientsheet
    With patientSheet
        nextRow = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row + 1
        Sheets("Master").UsedRange.Offset(2).Copy .Range("A" & nextRow)
    End With
    
    ' b) Populate another sheet called (Database) with all the entries that I add
    With Sheets("DataBase")
        nextRow = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row + 1
        Sheets("Master").UsedRange.Offset(2).Copy .Range("A" & nextRow)
    End With
    
' c) Once I click Save entry and point a and b happened I want to clear the row 3 in Master (to be able to enter another patient)
    Sheets("Master").UsedRange.Offset(2).Delete

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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