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
Bookmarks