+ Reply to Thread
Results 1 to 8 of 8

Enter User Input Data into Next Free Cell based on Column Name

Hybrid View

  1. #1
    Registered User
    Join Date
    04-08-2015
    Location
    America
    MS-Off Ver
    Office 2013
    Posts
    29

    Enter User Input Data into Next Free Cell based on Column Name

    Hi everyone,
    I am trying to create a user friendly macro for entering new meeting information based on the contacts name.
    For example i have a sheet (attached), and i want to be able to click a button and then the user inputs a name, and also inputs a meeting note.

    Name: John <- user input
    New Meeting Note: JD 1/1/11 <- user input

    I hoped to create a macro to search for the name input in column A and then input the "New Meeting Note" in the next available cell.
    I was thinking it could find the column titled "Meetings" then search for the next blank cell to the right of it as there could already be data in the Meeting column, and possible a number of columns to the right as well.

    See the attached for a visual

    Cheers for any help.
    Attached Files Attached Files
    Last edited by nKife; 04-21-2015 at 08:46 PM.

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Enter User Input Data into Next Free Cell based on Column Name

    Hi nKife and welcome to ExcelForum,

    See the attached file (based on your original file) which uses a UserForm to select an existing name, and entry and posting of a Meeting Note.

    There are 3 modules that contain code:
    Sheet1 module:
    Option Explicit
    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
      Dim iRow As Long
      Dim sUserName As String
    
      'Return Control to Excel
      Cancel = True
    
      'Put the name from Column 'A' in the UserForm 'ComboBox' if the Name is NOT BLANK
      iRow = Target.Row
      sUserName = Trim(Cells(iRow, "A").Value)
      If Len(sUserName) > 0 Then
        UserForm1.ComboBox1.Value = sUserName
      End If
    
      'Display the UserForm
      UserForm1.Show vbModeless
      
    End Sub
    UserForm1 module:
    Option Explicit
    
    Private Sub UserForm_Initialize()
    
      Dim iRow As Long
      Dim bNeedMore As Boolean
      Dim sName As String
    
      
      '''''''''''''''''''''''''''''''''
      'Initialize the Multi-Column ComboBox
      'The First Column Contains the 'User Name'
      'The Second Column is HIDDEN and contains the spreadsheet row number for the User Name
      '''''''''''''''''''''''''''''''''
      iRow = 1             'Initialize the row number to one row before the first data row
      bNeedMore = True
      While bNeedMore = True
        iRow = iRow + 1
        sName = Trim(Sheets("Sheet1").Cells(iRow, "A"))   'Get name (remove leading/trailing spaces)
        If Len(sName) > 0 Then
          ComboBox1.AddItem sName
          ComboBox1.List(ComboBox1.ListCount - 1, 1) = iRow
        Else
          bNeedMore = False       'Exit on first blank line
        End If
      Wend
      
    End Sub
    
    Private Sub ComboBox1_Change()
      'This clears the TextBox1 value each time the ComboBox value changes
      UserForm1.TextBox1.Value = ""
    End Sub
    
    Private Sub TextBox1_Change()
      'This displays the 'Save' Command Button only if 'TextBox1' is NOT BLANK
    
      Dim sComboBoxValue As String
      Dim sTextBoxValue As String
      
      sComboBoxValue = Trim(ComboBox1.Value)
      sTextBoxValue = Trim(TextBox1.Value)
      If Len(sComboBoxValue) > 0 And Len(sTextBoxValue) > 0 Then
        CommandButtonSave.Visible = True
      Else
        CommandButtonSave.Visible = False
      End If
    End Sub
    
    Private Sub CommandButtonExit_Click()
      Unload Me
    End Sub
    
    Private Sub CommandButtonSave_Click()
    
      Call AddMeetingNoteToCurrentUser
    
    End Sub
    Ordinary Code Module (ModUserForm1):
    Option Explicit
    
    Sub DisplayUserForm1()
      UserForm1.Show vbModeless
    End Sub
    
    Sub AddMeetingNoteToCurrentUser()
    
      Dim iColumn As Long
      Dim iRowInListBox As Long
      Dim iRowInSpreadSheet As Long
      Dim bNeedMore As Boolean
      Dim sTextBoxValue As String
      Dim sValue As String
      
      'Get the value in the TextBox
      sTextBoxValue = Trim(UserForm1.TextBox1.Value)
      
      'Get the 'ComboBox' Row Number that contains the current 'User Name'
      'Get the 'SpreadSheet' Row Number that contains the current 'User Name'
      '    from the HIDDEN value in the ComboBox
      iRowInListBox = UserForm1.ComboBox1.ListIndex
      iRowInSpreadSheet = UserForm1.ComboBox1.List(iRowInListBox, 1)
      
      'Initialize the Column Number to one Column Before the First Column Used ('C' = 3)
      iColumn = 3
      
      'Find the First Empty Column (starting at Column 'D') in the row
      'Put the Meeting Notes in the Column
      bNeedMore = True
      While bNeedMore = True
        iColumn = iColumn + 1
        sValue = Trim(Sheets("Sheet1").Cells(iRowInSpreadSheet, iColumn))   'Get the value (remove leading/trailing spaces)
        If Len(sValue) = 0 Then
          Sheets("Sheet1").Cells(iRowInSpreadSheet, iColumn) = sTextBoxValue
          bNeedMore = False       'Exit on first blank line
        End If
      Wend
    
    End Sub
    ---------------------
    The following may help access the code and learn what is going on:

    To access a Module in VBA:
    a. 'Left Click' on any cell in the Excel Spreadsheet.
    b. ALT-F11 to get to VBA.
    c. CTRL-R to get project explorer (if it isn't already showing).
    d. 'Double Click' on the module you want to access in the 'Project Explorer'.
    e. Insert code into the module if needed. 'Option Explicit' should only appear ONCE at the top of the module.

    NOTE: To access UserForm code, 'Right Click' on the UserForm module, then select 'View Code'. To access the UserForm itself, 'Double Click' on the UserForm module.

    Debugger Secrets:
    a. Press 'F8' to single step (goes into subroutines and functions).
    b. Press SHIFT 'F8' to single step OVER subroutines and functions.
    c. Press CTRL 'F8' to stop at the line where the cursor is.
    d. 'Left Click' the margin to the left of a line to set (or clear) a BREAKPOINT.
    e. Press CTRL 'G' to open the IMMEDIATE WINDOW. 'debug.print' statements send their
    output to the IMMEDIATE WINDOW.
    f. Select View > Locals to see all variables while debugging.
    g. To automatically set a BREAKPOINT at a certain location put in the line:
    'Debug.Assert False'
    h. To conditionally set a BREAKPOINT at a certain location put in lines similar to:
    if i >= 20 and xTV20 > 99.56 then
    Debug.Assert False
    endif
    i. A variable value will be displayed by putting the cursor over the variable name.
    ---------------------

    Please let me know if you have any problems or questions.

    Lewis
    Attached Files Attached Files
    Last edited by LJMetzger; 04-21-2015 at 08:11 PM.

  3. #3
    Registered User
    Join Date
    04-08-2015
    Location
    America
    MS-Off Ver
    Office 2013
    Posts
    29

    Re: Enter User Input Data into Next Free Cell based on Column Name

    Works great thanks so much. Now to apply this to a larger spreadsheet!

  4. #4
    Registered User
    Join Date
    04-08-2015
    Location
    America
    MS-Off Ver
    Office 2013
    Posts
    29

    Re: Enter User Input Data into Next Free Cell based on Column Name

    The only issue I am having is that if the data is moved down some rows, i.e. row A and B are blank the code does not work. Is there a way around this. For example I may want a heading or some logos in the first 2, 3 or 4 rows, then the data below it.
    Cheers

  5. #5
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Enter User Input Data into Next Free Cell based on Column Name

    The only issue I am having is that if the data is moved down some rows, i.e. row A and B are blank the code does not work. Is there a way around this. For example I may want a heading or some logos in the first 2, 3 or 4 rows, then the data below it.
    Thanks for the rep points.

    See the line in the UserForm1 module above in post #2 that I just changed to red:
    iRow = 1             'Initialize the row number to one row before the first data row
    The value of iRow is set to 1 because the first name is in Row2. If the first name is in row 6, set the value of iRow to 5.

    Lewis

  6. #6
    Registered User
    Join Date
    04-08-2015
    Location
    America
    MS-Off Ver
    Office 2013
    Posts
    29

    Re: Enter User Input Data into Next Free Cell based on Column Name

    In section 3 "Ordinary Code Module"
    I receive an error;

    2015-04-22 10_29_46-Microsoft Visual Basic.png

    iRowInSpreadSheet = UserForm1.ComboBox1.List(iRowInListBox, 1)
    I am unsure what would cause this error.

    Cheers

  7. #7
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Enter User Input Data into Next Free Cell based on Column Name

    I am not able to duplicate your error. To receive further help, you will have to upload a sample workbook that demonstrates the error. Please include detailed instructions on what to do to make the error appear.

    Lewis

  8. #8
    Registered User
    Join Date
    04-08-2015
    Location
    America
    MS-Off Ver
    Office 2013
    Posts
    29

    Re: Enter User Input Data into Next Free Cell based on Column Name

    Never mind mate I fixed it.
    Appreciate all your help you are a fantastic person +1 rep!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Create User Input Dialog Box to Enter Keywords for Word Search in a Cell
    By vcs1161 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-06-2013, 01:36 PM
  2. Using text box input to lookup number and replace based on user input into new column
    By harl3y412 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-28-2011, 03:15 PM
  3. Replies: 1
    Last Post: 08-27-2010, 02:59 AM
  4. Enter Text Into Cells Based on User Input
    By jdmay in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-23-2010, 11:50 AM
  5. [SOLVED] Copy cell data from workbook based on user input
    By Michael A in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-31-2005, 11:10 AM

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