+ Reply to Thread
Results 1 to 12 of 12

Macro prompts and user inputs

Hybrid View

  1. #1
    Registered User
    Join Date
    07-20-2009
    Location
    Roseburg, OR
    MS-Off Ver
    Excel 2007
    Posts
    21

    Question Macro prompts and user inputs

    Hello all,

    I am not new to excel - but I have never required the use of advanced excel features before today - so I apologize in advance. I am a health professional student (graduate level) assisting my preceptor in developing patient management mechanisms.

    I am building an excel spreadsheet to manage current medications for several patients.

    I have built the spreadsheet with the first 'sheet' being a directory and subsequent sheets being patient data files(one sheet per patient).

    I am now in the process of creating a macro to build new patient data files (a new sheet for a new patient). I recorded a macro to build the new sheet with the proper formatting of information. What I am trying to do now is automate the naming and linking of the new sheet.

    What I am really after is how can you create a "prompt" such that after activating the macro button it asks:

    Patient last name:
    Patient first name:

    Then uses this data to:

    1. Rename the newly made sheet to 'last, first'
    2. Title the newly made sheet with 'last, first'
    3. Create an entry in the directory in the 'last, first' format
    4. Finally, build the hyperlink from the directory to the newly created sheet

    Any and all help you can provide would be greatly appreciated. I understand you are all busy professionals, and I am thankful for any assistance I can get.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro prompts and user inputs

    Hello aboveliquidice,

    The attached workbook is far from being comprehensive for your needs, but it does demonstrate the basic approach to your problem. VBA UserForms are by far the best way to manipulate data in database.

    After you open the workbook, click the button on "Sheet 3" to activate the form. You can then add a new patient worksheet using the names you have entered. When the sheet prints, the patient's name is in Arial 24 Point bold type and centered on the page. You change the font and size to what you want to use. They are highlighted in red in code below.

    Here is the code behind the UserForm...
    Private Sub CommandButton1_Click()
     'CLOSE USERFORM
       Me.Hide
       Unload Me
    End Sub
    
    Private Sub CommandButton2_Click()
     'ADD NEW PATIENT WORKSHEET
     
       Dim PatientName As String
       Dim Wks As Worksheet
         
         'Patient's name is last name first
          PatientName = TextBox2 & ", " & TextBox1
          
         'Check If Patient Sheet exists
          For Each Wks In Worksheets
            If Wks.Name = PatientName Then
              MsgBox "A sheet already exists for " & PatientName
              Exit Sub
            End If
          Next Wks
          
          With Worksheets
           'Add the new worksheet
            Set Wks = .Add(After:=Worksheets(.Count))
           'Name it for the patient
            Wks.Name = PatientName
           'Set the Center Header to Arial 24 point, centered, bold
            Wks.PageSetup.CenterHeader = "&""Arial"" &C &B &24" & PatientName
          End With
          
    End Sub
    
    Private Sub UserForm_Activate()
      TextBox1.SetFocus
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    07-20-2009
    Location
    Roseburg, OR
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Macro prompts and user inputs

    Quote Originally Posted by Leith Ross View Post
    Hello aboveliquidice,

    The attached workbook is far from being comprehensive for your needs, but it does demonstrate the basic approach to your problem. VBA UserForms are by far the best way to manipulate data in database.

    After you open the workbook, click the button on "Sheet 3" to activate the form. You can then add a new patient worksheet using the names you have entered. When the sheet prints, the patient's name is in Arial 24 Point bold type and centered on the page. You change the font and size to what you want to use. They are highlighted in red in code below.

    Here is the code behind the UserForm...

    GREAT CODE
    Leith, Thank you for that great code - It really demonstrates the concept well...

    Where does the code from the UserForm live? I pull up the macro, but it only shows the command to invoke the UserForm. I can understand the coding that you have provided - just not where it lives in terms of adding it to my current spreadsheet.

    I can tell this is a newbie question - if in doubt - please point me in the right direction.

    Thanks again.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro prompts and user inputs

    Hello aboveliquidice,

    To see and change the UserForm code, you need to activate the Visual Basic Editor.

    1. Open the workbook and press Alt+F11 to open the VBE
    2. In the upper left corner of the VBE you should see a window titled Project. If not press Ctrl+R
    3. Look for the Forms folder in the Project window. If it isn't open then click on the "plus sign" to left of the folder.
    4. Left Click UserForm1 to select it.
    5. Press F7 to view the code.

  5. #5
    Registered User
    Join Date
    07-20-2009
    Location
    Roseburg, OR
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Macro prompts and user inputs

    Hey Leith,

    I did manage to find the code - I then transposed the coding for the recorded "new sheet" code into the UserForm. I had no idea code could live in the form instead. Very neat.

    I now have the codes merged such that clicking the "new patient" button on the main directory page prompts for last and first (in the same manner in which you created the UserForm). With this data - it creates the new sheet in perfect format (my pre-existing macro) with the name of the patient now in the correct place, and the sheet renamed to the patient.

    Thank you so much for helping me get this far.

    Did you have any advice for the creation of a table utilizing the PatientName variable from the input? What I am thinking is building a simple table that has a list of all sheets in the spreadsheet excluding the directory sheet. Then automatically hyperlinking to each sheet. There are other columns of the table that will require manual input - so only one column would need to be in the code / automated...

    What do you think?

    EDIT:

    I located a nice little macro from a message board to find all the sheets and output them to a message box.

    Sub List_sheets()
        Dim s As String
        s = ""
        For Each w In Worksheets
        s = s & Chr(10) & w.Name
        Next
        MsgBox (s)
    End Sub
    After the Next command, I could remove the output to MsgBox for (s) and instead define the table range and fill in correct?
    Last edited by aboveliquidice; 07-21-2009 at 02:52 PM.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro prompts and user inputs

    Hello aboveliquidice,

    Hyperlinks are fine if you want to jump to another program or have data that is stable in a document. In your case, patient sheets could be removed as well as added creating more more work to maintain the list. You can create a list of the all the patients on a UserForm and activate the selected sheet that way. The list will always reflect the worksheets that are in the workbook. Using the form to do this, provides the user with the freedom to move easily through hundreds or even thousands of patient sheets. The form can be displayed by user either using a keyboard shortcut or clicking a button.

    Here is the code for the UserForm in the attached file. The sheet "Main" is not included in the list of worksheets. You can activate the macro by clicking the button or use the shortcut Ctrl+Shift+W.
    Private Sub CommandButton1_Click()
     'CLOSE USERFORM
       Me.Hide
       Unload Me
    End Sub
    
    Private Sub CommandButton2_Click()
     'GO TO SHEET
     
       With ComboBox1
         If .ListIndex > -1 Then
           Worksheets(.List(.ListIndex)).Activate
         End If
       End With
       
    End Sub
    
    Private Sub UserForm_Initialize()
    
      Dim Wks As Worksheet
      
       'Load the ComboBox with the worksheets except for Main
        For Each Wks In Worksheets
          If Wks.Name <> "Main" Then
            ComboBox1.AddItem Wks.Name
          End If
        Next Wks
        
    End Sub
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    07-20-2009
    Location
    Roseburg, OR
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Macro prompts and user inputs

    Hey Leith - Thanks again for the great coding work...

    I implemented your code for jumping to patient profiles. I was also able to rework a bit of your code to create a "Remove Patient" UserForm. It uses the same code to load the sheets - but for CommandButton2_Click it uses .ListIndex as part of the Sheets ().delete command

    Private Sub CommandButton2_Click()
     'Remove Patient 
       With ComboBox1
         If .ListIndex > -1 Then 
          ' Application.DisplayAlerts = False
           Sheets(.List(.ListIndex)).Delete
           Application.DisplayAlerts = True
         End If
       End With
       Unload Me
    End Sub
    I also added Unload Me to remove the UserForm after execution (and applied the concept to the other UserForms). I am now working on simply providing a list of worksheets on the directory sheet - More of an FYI for whomever is using the database. With the "New Patient", "Jump To Patient", and "Remove Patient" forms built - The database is pretty close to being done.

    As always - I appreciate your advice and mentoring nature.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1