I think this is a simple question but as I haven’t used excel in this way before I am struggling. I am trying to create a program based in Excel VBA. I work in a call center. We have numerous excel sheets to log calls for different schemes. I have a project to streamline this process. So far I have created a workbook that has separate sheets named to represent each scheme. I have created a userform to gather information. One of the combo boxes indicates which scheme the call is in regards to. The input from the userform needs to be logged on to a sheet in the work book that corresponds to the name of the scheme selected in the combo box. I cant seem to get this close to working. Any suggestions or advice would be greatly appreciated!
The code I have is:
Private Sub cmdClear_Click()
Call UserForm_Initialize
End Sub
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub ComboBox1_Change()
Dim ShtName As String
ShtName = ComboBox1.List(ComboBox1.ListIndex)
End Sub
Private Sub CommandButton1_Click()
Call Add_Data
End Sub
Private Sub Add_Data()
Dim Worksheets As Range
Dim ws As Worksheet
Dim UserForm As Object
Dim iRow As Long
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set ws = this.ComboBox1.GetItemText(this.ComboBox1.SelectedItem)
'find first empty row in database
iRow = ShtName.Cells(Rows.Count, 1).End(x1up).Offset(1, 0).Row
'copy the data to the database
ws.Cells(iRow, 1) = Date
ws.Cells(iRow, 2) = Me.txtName.Value
ws.Cells(iRow, 3) = Me.txtNumber.Value
ws.Cells(iRow, 4) = ComboBox2.Value
ws.Cells(iRow, 5).Value = Me.txtQuery.Value
If optYes = True Then
msgstr = "Yes"
ElseIf optNo = True Then
msgstr = "No"
End If
ws.Cells(iRow, 6).Value = msgstr
If chkMain = True Then
msgstri = "Main"
ElseIf chkExec = True Then
msgstr = "Exec"
Else
msgstri = ""
End If
ws.Cells(iRow, 7).Value = msgstri
ws.Cells(iRow, 8).Value = Me.txtTime.Value
End Sub
Private Sub UserForm_Initialize()
ComboBox1.Clear
ComboBox1.AddItem ("Sheet2")
ComboBox1.AddItem ("Sheet3")
ComboBox1.AddItem ("Sheet4")
ComboBox1.AddItem ("Sheet5")
ComboBox1.AddItem ("Sheet6")
ComboBox1.AddItem ("Sheet7")
ComboBox1.AddItem ("Sheet8")
ComboBox1.AddItem ("Sheet9")
ComboBox1.AddItem ("Sheet10")
ComboBox1.AddItem ("Sheet11")
ComboBox1.AddItem ("Sheet12")
ComboBox2.Clear
ComboBox2.AddItem ("agent1")
ComboBox2.AddItem ("agent2")
ComboBox2.AddItem ("agent3")
ComboBox2.AddItem ("agent4")
ComboBox2.AddItem ("agent5")
ComboBox2.AddItem ("agent6")
ComboBox2.AddItem ("agent7")
ComboBox2.AddItem ("agent8")
ComboBox2.AddItem ("agent9")
ComboBox2.AddItem ("agent10")
txtNumber.Value = ""
txtName.Value = ""
txtQuery.Value = ""
txtTime.Value = ""
optYes = False
optNo = False
chkMain = False
chkExec = False
ComboBox1.SetFocus
End Sub
You should be getting an error at
You are trying to set an object to a string. Try using
This will actually give you the ws object in that variable. Sheets is the collection of worksheets in the workbook. You can select by index number or by the name of the sheet.