Cannot execute userform from different sheet - excel

I have this userform code that cannot be execute from other sheet, so it can only be executed from sheet 6 (Activity Data). When i tried to execute it from other sheet, it shows
runtime error 1004: application-defined or object-defined error.
Any idea how to solve this problem?
Private Sub ComboBox5_Click()
Dim t As Double
With ComboBox5
t = Val(.List(.ListIndex))
.value = Format(t, "hh:mm")
End With
End Sub
Private Sub ComboBox8_Click()
Dim t As Double
With ComboBox8
t = Val(.List(.ListIndex))
.value = Format(t, "hh:mm")
End With
End Sub
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub CommandButton3_Click()
Dim activity_id As String
activity_id = ComboBox9.value
Lastrow = Sheets("Activity Data").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To Lastrow
If Sheets("Activity Data").Cells(i, 7).value = activity_id Then
Sheets("Activity Data").Cells(i, 8).value = ComboBox1.value
Sheets("Activity Data").Cells(i, 9).value = TextBox1.Text
Sheets("Activity Data").Cells(i, 11).value = ComboBox2.value
Sheets("Activity Data").Cells(i, 12).value = ComboBox3.value
Sheets("Activity Data").Cells(i, 13).value = ComboBox4.value
Sheets("Activity Data").Cells(i, 14).value = ComboBox5.value
Sheets("Activity Data").Cells(i, 15).value = ComboBox8.value
Sheets("Activity Data").Cells(i, 10).value = TextBox2.Text
End If
Next
End Sub
Private Sub CommandButton4_Click()
Dim activity_id As String
activity_id = ComboBox9.value
Lastrow = Sheets("Activity Data").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To Lastrow
If Sheets("Activity Data").Cells(i, 7).value = activity_id Then
ComboBox1.value = Sheets("Activity Data").Cells(i, 8).value
TextBox1.Text = Sheets("Activity Data").Cells(i, 9).value
ComboBox2.value = Sheets("Activity Data").Cells(i, 11).value
ComboBox3.value = Sheets("Activity Data").Cells(i, 12).value
ComboBox4.value = Sheets("Activity Data").Cells(i, 13).value
ComboBox5.value = Sheets("Activity Data").Cells(i, 14).value
ComboBox8.value = Sheets("Activity Data").Cells(i, 15).value
TextBox2.Text = Sheets("Activity Data").Cells(i, 10).value
End If
Next
End Sub
Private Sub UserForm_Initialize()
ComboBox9.RowSource = Sheets("Activity Data").Range("G2", Range("G65536").End(xlUp)).Address
End Sub

Related

Run-time 438 VBA

I have a run-time 438 error with VBA. Could you help me? I'm beginner.
Private Sub CommandButton1_Click()
Dim nwb As Workbook
Set nwb = Workbooks.Open("G:\worksheet.xlsm")
CopyLastRow = nwb.Cells(nwb.Rows.Count, "A").End(x1Up).Row + 1
Cells(CopyLastRow, 1).Value = TextBox1.Value
Cells(CopyLastRow, 2).Value = TextBox2.Value
Cells(CopyLastRow, 3).Value = ComboBox1.Value
Cells(CopyLastRow, 4).Value = TextBox3.Value
Cells(CopyLastRow, 5).Value = ComboBox2.Value
Cells(CopyLastRow, 6).Value = TextBox4.Value
Cells(CopyLastRow, 7).Value = TextBox5.Value
Unload Me
End Sub
You need to specify worksheet of opened workbook. Try below codes.
Dim nwb As Workbook
Dim sh As Worksheet
Dim CopyLastRow As Long
Set nwb = Workbooks.Open("D:\TestWorkbook.xlsx")
Set sh = nwb.Worksheets("Sheet1")
CopyLastRow = sh.Cells(sh.Rows.Count, "A").End(xlUp).Row + 1
sh.Cells(CopyLastRow, 1).Value = TextBox1.Value
sh.Cells(CopyLastRow, 2).Value = TextBox2.Value
sh.Cells(CopyLastRow, 3).Value = ComboBox1.Value
sh.Cells(CopyLastRow, 4).Value = TextBox3.Value
sh.Cells(CopyLastRow, 5).Value = ComboBox2.Value
sh.Cells(CopyLastRow, 6).Value = TextBox4.Value
sh.Cells(CopyLastRow, 7).Value = TextBox5.Value

Excel VBA Error 424 with lstDisplay.ColumnCount

I am getting a runtime error 424 in my VBA code while trying to display records. It does not seem to be acknowledging lst.Display.ColumnCount and lst.Display.RowSource. Would very much appreciate the help to mitigate the issue. Every time I run the code, it shows me the 424 runtime error message pointing to the lstDisplay.ColumnCount and lstDisplay.RowSource lines.
Private Sub CommandButton1_Click()
Dim wks As Worksheet
Dim AddNew As Range
Set wks = Sheet1
Set AddNew = wks.Range("A65356").End(xlUp).Offset(1, 0)
AddNew.Offset(0, 0).Value = TextBox1.Text
AddNew.Offset(0, 1).Value = TextBox2.Text
AddNew.Offset(0, 2).Value = TextBox3.Text
AddNew.Offset(0, 3).Value = TextBox4.Text
AddNew.Offset(0, 4).Value = TextBox5.Text
AddNew.Offset(0, 5).Value = TextBox6.Text
AddNew.Offset(0, 6).Value = TextBox7.Text
AddNew.Offset(0, 7).Value = TextBox8.Text
AddNew.Offset(0, 8).Value = TextBox9.Text
AddNew.Offset(0, 9).Value = TextBox10.Text
lstDisplay.ColumnCount = 10
lstDisplay.RowSource = "B1: J65356"**
End Sub
Private Sub CommandButton2_Click()
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
Private Sub CommandButton3_Click()
Dim i As Integer
For i = 0 To Range("A65356").End(xlUp).Row - 1
If lstDisplay.Selected(i) Then
Rows(i + 1).Select
Selection.Delete
End If
Next i
End Sub
I expect a list to be displayed in my form but it keeps on showing the error.

.CurrentRegions.Rows.Count inserts data from UserForm textboxes to 900th+ row in Worksheet

I had this code working before but I seem to have a problem with it today, it inserts data from my UserForm textboxes to the 900th+ row in the sheet. Why is this?
Dim RowCount As Long
RowCount = Worksheets("ERF Logs").Range("A2").CurrentRegion.Rows.Count
With Worksheets("ERF Logs").Range("A2")
.Offset(RowCount, 0) = TextBox8.Value
.Offset(RowCount, 1) = TextBox1.Value
.Offset(RowCount, 2) = ComboBox1.Value
.Offset(RowCount, 3) = TextBox6.Value
.Offset(RowCount, 4) = TextBox3.Value
.Offset(RowCount, 5) = TextBox4.Value
.Offset(RowCount, 6) = TextBox5.Value
.Offset(RowCount, 7) = TextBox7.Value
.Offset(RowCount, 8).Select
End With
Try this:
Sub Test2paste()
Dim lr As Long
With ThisWorkbook.Sheets("ERF Logs")
lr = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
.Cells(lr, 1).Value = TextBox8.Value
.Cells(lr, 2).Value = TextBox1.Value
.Cells(lr, 3).Value = ComboBox1.Value
.Cells(lr, 4).Value = TextBox6.Value
.Cells(lr, 5).Value = TextBox3.Value
.Cells(lr, 6).Value = TextBox4.Value
.Cells(lr, 7).Value = TextBox5.Value
.Cells(lr, 8).Value = TextBox7.Value
'And don't use .Select, there are other ways for what you try to achieve I'm sure.
End With
End Sub

Excel VBA - Runtime error 438 : Object doesn't support this property or method

I'm new to VBA and I have the following code.
I want to sum in the variable SumDef the values of all the textboxes in a frame of a userform, if the textbox is not void.
But I get
runtime error 438 : Object doesn't support this property or method
Private Sub CommandButton2_Click()
'PROBLEEEEEM
Dim SumDef As Integer
Dim txt As Control
SumDef = 0
For Each txt In Me.Frame3.Controls
If TypeName(txt) = "TextBox" And IsNumeric(txt.Value) = True Then
SumDef = SumDef + CInt(txt.Text)
End If
Next txt
If TextBox2.Value <> vbNullString And CInt(TextBox2.Text) <= SumDef Then
cell2.Offset(, 7).Value = TextBox2.Text
cell2.Offset(, 19).Value = TextBox3.Text
cell2.Offset(, 8).Value = TextBox4.Text
cell2.Offset(, 9).Value = TextBox5.Text
cell2.Offset(, 10).Value = TextBox6.Text
cell2.Offset(, 11).Value = TextBox7.Text
cell2.Offset(, 12).Value = TextBox8.Text
cell2.Offset(, 13).Value = TextBox9.Text
cell2.Offset(, 14).Value = TextBox10.Text
cell2.Offset(, 15).Value = TextBox11.Text
cell2.Offset(, 16).Value = TextBox12.Text
cell2.Offset(, 17).Value = TextBox13.Text
cell2.Offset(, 18).Value = TextBox14.Text
MsgBox "Bac enregistré avec succes", vbInformation, "ENR FAB 07"
Me.CommandButton3.Enabled = True
Me.CommandButton2.Enabled = False
Else
MsgBox "Nombre de gants défauts est invalide", vbExclamation, "ENR FAB 07"
TextBox2.Value = vbNullString
End If
End Sub
Nest your If statement like this instead as it's trying to evaluate the .Value on all controls otherwise, and not all controls have a .Value property:
If TypeName(txt) = "TextBox" Then
If IsNumeric(txt.Value) = True Then
SumDef = SumDef + CInt(txt.Text)
End If
End If

Concatenate shows the second and third value except the first value

I have this macro that concatenates values from 3 cells, the problem is that it only concatenates the 1st and 2nd values, before it only concatenated the the first value was 3, the second value was march and the third value was 2015, so the result just gives me " 3march"
this is my code:
Private Sub CommandButton1_Click()
Sheets("Info").Activate
Range("M2:Q2").Select
Selection.Copy
Range("A2").Cells(1).Select
Do While ActiveCell.Value <> ""
ActiveCell.Offset(1, 0).Select
Loop
Selection.Offset(0, 12).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Selection.Offset(0, -12).Select
Selection.Cells(1).Select
'ahora viene la parte de pegar los valores,,,
Selection.Cells(1).Value = TextBox1.Value
Selection.Cells(1, 2).Value = ComboBox1.Value
Selection.Cells(1, 3).Value = ComboBox4.Value
Selection.Cells(1, 4).Value = ComboBox5.Value
Selection.Cells(1, 5).Value = ComboBox6.Value
Selection.Cells(1, 6).Value = ComboBox9.Value
Selection.Cells(1, 7).Value = ComboBox13.Value
Selection.Cells(1, 8).Value = TextBox3.Value
Selection.Cells(1, 9).Value = TextBox4.Value
Selection.Cells(1, 10).Value = TextBox8.Value
Selection.Cells(1, 11).Value = TextBox6.Value
Selection.Cells(1, 12).Value = ComboBox12.Value
'HERE IS THE PROBLEM!!!!!!
f_env = Selection.Cells(1, 3).Value & "-" & Selection.Cells(1, 4).Value & "-" & Selection.Cells(1, 5).Value
Selection.Cells(1, 17).Value = f_env
Unload UserForm1
End Sub
Because the column (on the selection's row) is actually determined by the column that selection was in, you will get a date in column Q only if selection is in column A. That may be a minor point but I believe the destination should always be a certain column.
    
Sub gather_date()
Dim sel As Range
With Selection.Parent
For Each sel In Selection.Columns(1).Cells
If Application.CountA(sel.Resize(1, 3)) = 3 Then
.Cells(sel.Row, 17) = CDate(Join(Array(sel.Value2, Left(sel.Offset(0, 1).Value2, 3), sel.Offset(0, 2).Value2), "-"))
.Cells(sel.Row, 17).NumberFormat = "d-mmmm-yyyy"
End If
Next sel
End With
End Sub
Since you are usig a macro based upon select, I've added a loop that will process all of the cells in selection.
This could also be performed with the following worksheet formula in Q2.
=DATEVALUE(CONCATENATE(B2, "-", C2, "-", D2))
Format as a date and fill down as necessary.
Edit:
Added new code.
It is hard to run the code as nobody but you has the workbook, and we would have to try and reproduce the workbook ourselves. Here is a revised code as I have interpreted what you are trying to do. I cannot test it as I don't have your workbook.
Private Sub CommandButton1_Click()
Dim sh As Worksheet
Dim Crng As Range 'copy range
Dim LrWs As Long 'last row
Dim Prng As Range 'paste range
Dim Drng As Range
Dim f_env As String
Set sh = Worksheets("Info")
With sh
Set Crng = .Range("M2:Q2")
LrWs = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
Set Prng = .Cells(LrWs, 13)
Crng.Copy
Prng.PasteSpecial xlPasteAll
Application.CutCopyMode = 0
Set Drng = .Cells(LrWs, 1)
Drng.Value = TextBox1.Value
Drng.Offset(0, 1).Value = ComboBox1.Value
Drng.Offset(0, 2).Value = ComboBox4.Value
Drng.Offset(0, 3).Value = ComboBox5.Value
Drng.Offset(0, 4).Value = ComboBox6.Value
Drng.Offset(0, 5).Value = ComboBox9.Value
Drng.Offset(0, 6).Value = ComboBox13.Value
Drng.Offset(0, 7).Value = TextBox3.Value
Drng.Offset(0, 8).Value = TextBox4.Value
Drng.Offset(0, 9).Value = TextBox8.Value
Drng.Offset(0, 10).Value = TextBox6.Value
Drng.Offset(0, 11).Value = ComboBox12.Value
End With
f_env = ComboBox4.Value & "-" & ComboBox5.Value & "-" & ComboBox6.Value
Drng.Offset(0, 16) = f_env
End Sub
Link us up to a sample workbook, if you can't get this working properly.

Resources