Sub Button()
Dim i As Integer
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet
Dim LastRow As Long
Dim wsName As String
wsName = FormSelectSht.ComboBoxMachines.Value
For Each ws In wb.Sheets
If ws.Name = wsName Then ws.Activate
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
Next
'énter data into active sheet
'ws.Range("A" & LastRow) = Date
With ws
.Range("B" & LastRow).Value = FormSelectSht.ComboBoxProduct.Value
' .Cells(LastRow, 2).Value = FormSelectSht.ComboBoxProduct.Value
'.Cells("B" & LastRow).Value = FormSelectSht.ComboBoxProduct.Value
'ws.Range("C" & LastRow) = juliandt
'ws.Range("D" & LastRow).Value = FormSelectSht.TextBoxOperator1.Value
'ws.Range("E" & LastRow).Value = FormSelectSht.TextBoxOperator2.Value
'ws.Range("F" & LastRow).Value = FormSelectSht.TextBoxLot.Value
End With
End Sub
With ws
Sub Button()
Dim ws As Worksheet
Dim LastRow As Long
Dim wsName As String
wsName = FormSelectSht.ComboBoxMachines.Value
Sheets(wsName).Activate
LastRow = Range("A" & Rows.Count).End(xlUp).Row
'énter data into active sheet
Range("B" & LastRow).Value = FormSelectSht.ComboBoxProduct.Value
End Sub
Hi Peter,When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags to your last post for you this time.
Also, when asking the forum about vba errors, as well as telling us which line caused the problem you should also tell us exactly what the error message was.
In this case it is obvious though: The line before the red one isWith ws
You have declared ws as Worksheet but you have not set it to any particular worksheet so when the vba gets to the red line it does not know which worksheet to look for Range("B" & LastRow) on.
I also don't know why you are looping through all the worksheets, even continuing to do so after you come across the one called wsName.
I don't know your exact goal and I don't have your form to work with but I'm guessing that something like this might be what you are trying to do.
Test with a copy of your workbook.
VBA Code:Sub Button() Dim ws As Worksheet Dim LastRow As Long Dim wsName As String wsName = FormSelectSht.ComboBoxMachines.Value Sheets(wsName).Activate LastRow = Range("A" & Rows.Count).End(xlUp).Row 'énter data into active sheet Range("B" & LastRow).Value = FormSelectSht.ComboBoxProduct.Value End Sub
At what point of the instructions to you run into a problem and what exactly is that problem?for some reason i can't download mini sheet.
yes i tried the code it still giving me run time error 91.At what point of the instructions to you run into a problem and what exactly is that problem?
Not sure that a mini sheet will help here though as the form will not come with the mini sheet.
Did you try that last code?
Unless you changed the code or combined it with other code I cannot see how what I posted could produce that error.yes i tried the code it still giving me run time error 91.
"object variable or with block variable not set."
This time you gave us the second part but not the first.... telling us which line caused the problem you should also tell us exactly what the error message was.
Unless you changed the code or combined it with other code I cannot see how what I posted could produce that error.
Hi Pete, you latest code works if that sheet "wsname" was exists.wsName = FormSelectSht.ComboBoxMachines.Value
Sheets(wsName).Activate
Hi KingGoku,For Each ws In wb.Sheets
If ws.Name = wsName Then ws.Activate
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
Next
With ws
.Range("B" & LastRow).Value = FormSelectSht.ComboBoxProduct.Value
Dim c as boolean
For Each ws In wb.Sheets
If ws.Name = wsName Then
c= true ' to confirm that wsName was exists
ws.Activate
end if
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
Next
If c then Range("B" & LastRow).Value = FormSelectSht.ComboBoxProduct.Value