Hi guys, any help or new way to do the trick will be welcome
I have a userform that when initialized, it goes to an excel workbook and gets data from a sheet to populate a combobox. No big deal.
When the button is clicked, I want, in that same workbook, different sheet, to add the data that was typed in the userform.
Code for Sub Initialize is:
Private Sub UserForm_Initialize()
Dim Area
Dim mycoll As Collection, cell As Range
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.Workbooks.Open Filename:="Path\File.xlsx"
Area = ufLogin.lblArea.Caption
Dim lRow As Long
Dim ws As Worksheet
Set ws = xlApp.Worksheets("Tables")
On Error Resume Next
Set mycoll = New Collection
With Ram1
.Clear
For Each cell In Range("A3:A" & Cells(Rows.Count, 1).End(xlUp).Row)
If Len(cell) <> 0 Then
Err.Clear
mycoll.Add cell.Value
If Err.Number = 0 Then Me.cmbMachine.AddItem cell.Value
End If
Next cell
End With
Ram1.ListIndex = 0
Me.Caption = "Input Data for " & Area
Me.lblTitle.Caption = "Input Data for " & Area
End Sub
My troubles start in the Click Event, do I need to close the application when the Initialize event ends, or leave it idle and call it back in the Click Event? If so, how can I do that?
I have this...
Private Sub cmdAddData_Click()
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.Workbooks.Open Filename:="Path\File.xlsx"
Dim lRow As Long
Dim ws As Worksheet
Set ws = xlApp.Worksheets("Data")
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
.Cells(lRow, 1).Value = Me.lblDate.Caption
.Cells(lRow, 2).Value = Me.cmbMachine.Value
End With
End Sub
Help!
Thanks in advance.
Bookmarks