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
Related
I want to add new data to a table with a form. I have it adding data at the bottom of the sheet.
I want the new info at the top.
With my code, it sends the data to two sheets, the "home" sheet and the sheet that is selected in the first combo box.
Private Sub CommandButton1_Click()
TargetSheet = ComboBox1.Value
If TargetSheet = "" Then
Exit Sub
End If
Worksheets(TargetSheet).Activate
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Cells(lastrow + 1, 1).Value = TextBox1.Value
ActiveSheet.Cells(lastrow + 1, 2).Value = TextBox2.Value
ActiveSheet.Cells(lastrow + 1, 3).Value = TextBox3.Value
ActiveSheet.Cells(lastrow + 1, 4).Value = TextBox4.Value
ActiveSheet.Cells(lastrow + 1, 5).Value = TextBox5.Value
ActiveSheet.Cells(lastrow + 1, 6).Value = TextBox6.Value
ActiveSheet.Cells(lastrow + 1, 7).Value = TextBox7.Value
ActiveSheet.Cells(lastrow + 1, 8).Value = TextBox8.Value
Worksheets("Home").Activate
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Cells(lastrow + 1, 1).Value = ComboBox1.Value
ActiveSheet.Cells(lastrow + 1, 2).Value = TextBox1.Value
ActiveSheet.Cells(lastrow + 1, 3).Value = TextBox2.Value
ActiveSheet.Cells(lastrow + 1, 4).Value = TextBox3.Value
ActiveSheet.Cells(lastrow + 1, 5).Value = TextBox4.Value
ActiveSheet.Cells(lastrow + 1, 6).Value = TextBox5.Value
ActiveSheet.Cells(lastrow + 1, 7).Value = TextBox6.Value
ActiveSheet.Cells(lastrow + 1, 8).Value = TextBox7.Value
ActiveSheet.Cells(lastrow + 1, 9).Value = TextBox8.Value
ActiveSheet.Cells(lastrow + 1, 10).Value = Date
ActiveSheet.Cells(lastrow + 1, 11).Value = TimeValue(Now)
ActiveSheet.Cells(lastrow + 1, 12).Value = TextBox9.Value
MsgBox ("Item Added Successfully.")
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
TextBox4.Value = ""
Worksheets("Home").Activate
Worksheets("Home").Cells(1, 1).Select
End Sub
How do I put the new data in the second row since I have headers on the sheet?
There are a lot of stuff to improve your my code, but I want to keep it simple
Some things to begin:
Use option explicit so you don't have unexpected behavior with undefined variables
Always indent your code (see www.rubberduckvba.com a free tool that helps you with that)
Try to separate your logic defining variables and the reusing them
Name your forms' controls
Check a great article about UserForms (when you feel you're ready to advance)
Check the code's comments, and adapt it to fit your needs
EDIT: No need for the EntireRow qualifier as we are already selecting the whole row, and added the copy format from below
Code:
Private Sub CommandButton1_Click()
' Define object variables
Dim targetSheet As Worksheet
Dim homeSheet As Worksheet
Dim targetSheetName As String
Dim homeSheetName As String
Dim targetSheetTopRow As Long
Dim homeSheetTopRow As Long
Dim textBox1Value As Variant
Dim textBox2Value As Variant
Dim textBox3Value As Variant
Dim textBox4Value As Variant
Dim textBox5Value As Variant
Dim textBox6Value As Variant
Dim textBox7Value As Variant
Dim textBox8Value As Variant
Dim textBox9Value As Variant
' Define parameters
targetSheetTopRow = 2
homeSheetTopRow = 2
homeSheetName = "Home"
' Validate if combobox has any value
If Me.ComboBox1.Value = vbNullString Then Exit Sub
' Get target sheet name
targetSheetName = Me.ComboBox1.Value
' Add a reference to sheets
Set targetSheet = ThisWorkbook.Worksheets(targetSheetName)
Set homeSheet = ThisWorkbook.Worksheets(homeSheetName)
' Store current controls values
textBox1Value = Me.TextBox1.Value
textBox2Value = Me.TextBox2.Value
textBox3Value = Me.TextBox3.Value
textBox4Value = Me.TextBox4.Value
textBox5Value = Me.TextBox5.Value
textBox6Value = Me.TextBox6.Value
textBox7Value = Me.TextBox7.Value
textBox8Value = Me.TextBox8.Value
' No need to activate stuff
With targetSheet
' Insert a row after row 2
.Range(targetSheetTopRow & ":" & targetSheetTopRow).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow
' Add cells values
.Cells(targetSheetTopRow, 1).Value = textBox1Value
.Cells(targetSheetTopRow, 2).Value = textBox2Value
.Cells(targetSheetTopRow, 3).Value = textBox3Value
.Cells(targetSheetTopRow, 4).Value = textBox4Value
.Cells(targetSheetTopRow, 5).Value = textBox5Value
.Cells(targetSheetTopRow, 6).Value = textBox6Value
.Cells(targetSheetTopRow, 7).Value = textBox7Value
.Cells(targetSheetTopRow, 8).Value = textBox8Value
End With
With homeSheet
' Insert a row after row 2
.Range(homeSheetTopRow & ":" & homeSheetTopRow).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow
' Add cells values
.Cells(homeSheetTopRow, 1).Value = textBox1Value
.Cells(homeSheetTopRow, 2).Value = textBox2Value
.Cells(homeSheetTopRow, 3).Value = textBox3Value
.Cells(homeSheetTopRow, 4).Value = textBox4Value
.Cells(homeSheetTopRow, 5).Value = textBox5Value
.Cells(homeSheetTopRow, 6).Value = textBox6Value
.Cells(homeSheetTopRow, 7).Value = textBox7Value
.Cells(homeSheetTopRow, 8).Value = textBox8Value
.Cells(homeSheetTopRow, 9).Value = Date
.Cells(homeSheetTopRow, 10).Value = TimeValue(Now)
.Cells(homeSheetTopRow, 11).Value = textBox9Value
End With
' Clear control's values
Me.TextBox1.Value = vbNullString
Me.TextBox2.Value = vbNullString
Me.TextBox3.Value = vbNullString
Me.TextBox4.Value = vbNullString
' Alert user
MsgBox ("Item Added Successfully.")
' Goto...
homeSheet.Activate
homeSheet.Cells(1, 1).Select
End Sub
Let me know if it works or you need more help
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.
How I can refer to Worksheets("Customers") without activating the worksheet in the below code?
Application.ScreenUpdating does not do the job, as there is still annoying flickering.
The code is working fine when I uncomment 'Worksheets("Customers").Activate
I want to perform all steps when Worksheets("Dashboard") is open.
I have tried "With ... End With" but no luck., also referencing Worksheets("Customers").Cells..... etc are not working. Its like code skipping through the code and goes straight to
"
If SearchRow = 2 Then
MsgBox "Customer Not Found", vbExclamation
Exit Sub
End If
"
I also have another code similar issue, that for some reason referencing not working as it should.
Just want to mention that I am using this code with the userforms and click buttons.
Any help will be appreciated.
Private Sub srCus_Click()
Application.ScreenUpdating = False
Dim RowNum As Long
Dim SearchRow As Long
RowNum = 2
SearchRow = 2
Worksheets("SearchCus").Range("A2:I100").ClearContents
'Worksheets("Customers").Activate
Do Until Worksheets("Customers").Cells(RowNum, 1).Value = ""
If InStr(1, Cells(RowNum, 3).Value, CusDB.Value, vbTextCompare) > 0 Then
Worksheets("SearchCus").Cells(SearchRow, 1).Value = Cells(RowNum, 1).Value
Worksheets("SearchCus").Cells(SearchRow, 2).Value = Cells(RowNum, 2).Value
Worksheets("SearchCus").Cells(SearchRow, 3).Value = Cells(RowNum, 3).Value
Worksheets("SearchCus").Cells(SearchRow, 4).Value = Cells(RowNum, 4).Value
Worksheets("SearchCus").Cells(SearchRow, 5).Value = Cells(RowNum, 5).Value
Worksheets("SearchCus").Cells(SearchRow, 6).Value = Cells(RowNum, 6).Value
Worksheets("SearchCus").Cells(SearchRow, 7).Value = Cells(RowNum, 7).Value
Worksheets("SearchCus").Cells(SearchRow, 8).Value = Cells(RowNum, 8).Value
Worksheets("SearchCus").Cells(SearchRow, 9).Value = Cells(RowNum, 9).Value
SearchRow = SearchRow + 1
End If
RowNum = RowNum + 1
Loop
If SearchRow = 2 Then
MsgBox "Customer Not Found", vbExclamation
Exit Sub
End If
ResultsDB.RowSource = "SearchResults"
'ThisWorkbook.Worksheets("Dashboard").Activate
Application.ScreenUpdating = True
End Sub
Thanks SJR, You were right with reference to all these Cells(RowNum, 1), I was always skipping one with InStr line. Thanks for the help and all suggestions. Reviewed code below.
Private Sub srCus_Click()
Application.ScreenUpdating = False
Dim RowNum As Long
Dim SearchRow As Long
RowNum = 2
SearchRow = 2
Worksheets("SearchCus").Range("A2:I100").ClearContents
'Worksheets("Customers").Activate
Do Until Worksheets("Customers").Cells(RowNum, 1).Value = ""
If InStr(1, Worksheets("Customers").Cells(RowNum, 3).Value, CusDB.Value, vbTextCompare) > 0 Then
Worksheets("SearchCus").Cells(SearchRow, 1).Value = Worksheets("Customers").Cells(RowNum, 1).Value
Worksheets("SearchCus").Cells(SearchRow, 2).Value = Worksheets("Customers").Cells(RowNum, 2).Value
Worksheets("SearchCus").Cells(SearchRow, 3).Value = Worksheets("Customers").Cells(RowNum, 3).Value
Worksheets("SearchCus").Cells(SearchRow, 4).Value = Worksheets("Customers").Cells(RowNum, 4).Value
Worksheets("SearchCus").Cells(SearchRow, 5).Value = Worksheets("Customers").Cells(RowNum, 5).Value
Worksheets("SearchCus").Cells(SearchRow, 6).Value = Worksheets("Customers").Cells(RowNum, 6).Value
Worksheets("SearchCus").Cells(SearchRow, 7).Value = Worksheets("Customers").Cells(RowNum, 7).Value
Worksheets("SearchCus").Cells(SearchRow, 8).Value = Worksheets("Customers").Cells(RowNum, 8).Value
Worksheets("SearchCus").Cells(SearchRow, 9).Value = Worksheets("Customers").Cells(RowNum, 9).Value
SearchRow = SearchRow + 1
End If
RowNum = RowNum + 1
Loop
If SearchRow = 2 Then
MsgBox "Customer Not Found", vbExclamation
Exit Sub
End If
ResultsDB.RowSource = "SearchResults"
'ThisWorkbook.Worksheets("Dashboard").Activate
Application.ScreenUpdating = True
End Sub
I am getting all the time error 381.. What is there wrong? If i use only 1 column it works, if i add 2nd and more it stops working.
I try to populate my rows which compile with "if statement".
it stops each time to work at 2nd column.
UserForm + some Data:
https://drive.google.com/open?id=1hfCAu2m7C4kISSPJSvyjWc-TvxBr-fOO
2nd Version of code:
Sub PopulateList2()
Dim rngName As Range
Dim ws As Worksheet
Dim i As Integer
Dim LastRow As Long
Set ws = E1G
With ListBoxAbg
.Clear
.ColumnCount = 2
LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).row
For i = 1 To LastRow
If ws.Cells(i, 6).Value < Now() _
And ws.Cells(i, 6).Value <> vbNullString Then
.AddItem
.List(i - 1, 0) = ws.Cells(i, 1).Value
.List(i - 1, 1) = ws.Cells(i, 3).Value
End If
Next i
End With
End Sub
....
Sub PopulateList2()
Dim rngName As Range
Dim ws As Worksheet
Dim i As Integer
Dim LastRow As Long
Set ws = E1G
AbgeListField.Clear
AbgeListField.ColumnCount = 7
LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).row
For i = 1 To LastRow
If ws.Cells(i, 6).Value < Now() _
And ws.Cells(i, 6).Value <> vbNullString Then
AbgeListField.AddItem ws.Cells(i, 1).Value
AbgeListField.List(i - 1, 1) = ws.Cells(i, 2).Value
AbgeListField.List(i - 1, 2) = ws.Cells(i, 3).Value
AbgeListField.List(i - 1, 3) = ws.Cells(i, 4).Value
AbgeListField.List(i - 1, 4) = ws.Cells(i, 5).Value
AbgeListField.List(i - 1, 5) = ws.Cells(i, 6).Value
AbgeListField.List(i - 1, 6) = ws.Cells(i, 7).Value
End If
Next i
End Sub
i found the answer in that post:
https://social.msdn.microsoft.com/Forums/office/en-US/f5619db9-be72-41e3-a353-54ebb021f936/runtime-error-381-could-not-set-the-list-property-invalid-property-array-index?forum=exceldev
i added new dim nxtItme As Long. it works perfect now:
Sub PopulateList2()
Dim rngName As Range
Dim ws As Worksheet
Dim i As Integer
Dim LastRow As Long
Dim nxtItem As Long
Set ws = E1G
nxtItem = 0
With ListBoxAbg
.Clear
.ColumnCount = 6
LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).row
For i = 1 To LastRow
If ws.Cells(i, 6).Value < Now() _
And ws.Cells(i, 6).Value <> vbNullString Then
.AddItem
.List(nxtItem, 0) = ws.Cells(i, 1).Value
.List(nxtItem, 1) = ws.Cells(i, 3).Value
.List(nxtItem, 2) = ws.Cells(i, 4).Value
.List(nxtItem, 3) = ws.Cells(i, 5).Value
.List(nxtItem, 4) = ws.Cells(i, 6).Value
nxtItem = nxtItem + 1
End If
Next i
End With
End Sub
I am trying to put together some codes that I found here and there to build up a small inventory, sales program. I am stuck at a point where the customer basket is finalized and sold items in the basket should be saved in relevant sheets.
As an example,basket data is in sheet1 (A4:g22), needs to be written to sheet2 and sheet3 with finding the first empty cell in column A. Thank you very much for your help in advance.
Private Sub EKSKAYDET_Click()
If Not IsNumeric(Me.eksmiktartxt.Value) Then
MsgBox "Miktari Kontrol Ediniz!"
Me.eksmiktartxt.SetFocus
Exit Sub
End If
If Not IsNumeric(Me.eksreznobox.Value) Then
MsgBox "ÜRÜN KODUNU Kontrol Ediniz!"
Me.eksreznobox.SetFocus
Exit Sub
End If
If eksreznobox.Value = "" Then
MsgBox "ÜRÜN KODU Seçmelisiniz!"
Me.eksreznobox.SetFocus
Exit Sub
End If
If TextBox23 = 0 And TextBox19 = 0 And TextBox20 = 0 And TextBox21 = 0 And TextBox22 = 0 Then
MsgBox "ÖDEME MİKTARI Girmelisiniz!": Exit Sub
Me.TextBox19.SetFocus
End If
If TextBox25.Value = 0 Then
MsgBox "SEPET BOŞ!"
Exit Sub
End If
If TextBox19 = "" And TextBox20 = "" And TextBox21 = "" And TextBox22 = "" And TextBox23 = "" Then
MsgBox "Tutar Girmelisiniz!":
Exit Sub
End If
If eksreznobox.ListCount = 0 Then Exit Sub
ry_bul = eksreznobox.ListIndex + 3
eksadI = Sheets("STOKKARTLARI").Range("D" & ry_bul).Value
EKSSOYADI = Sheets("STOKKARTLARI").Range("E" & ry_bul).Value
textbox12 = Sheets("STOKKARTLARI").Range("h" & ry_bul).Value
TextBox15 = Sheets("STOKKARTLARI").Range("F" & ry_bul).Value
ekstutartxt.Value = eksmiktartxt.Value * textbox12.Value
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("SATISHAREKETLERİ")
lRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
If Trim(Me.eksreznobox.Value) = "" Then
Me.ekreznobox.SetFocus
MsgBox "Lütfen ÜRÜN KODUNU Girin!"
Exit Sub
End If
With ws
.Cells(lRow, 3).Value = Me.eksreznobox.Value
.Cells(lRow, 1).Value = Me.ekstarihtXT.Value
.Cells(lRow, 4).Value = Me.eksadI.Value
.Cells(lRow, 7).Value = Me.eksmiktartxt.Value
.Cells(lRow, 9).Value = Me.ekstutartxt.Value
.Cells(lRow, 8).Value = Me.textbox12.Value
.Cells(lRow, 5).Value = Me.EKSSOYADI.Value
.Cells(lRow, 6).Value = Me.TextBox15.Value
.Cells(lRow, 2).Value = Me.TextBox26.Value
Dim llRow As Long
Dim ws1 As Worksheet
Set ws1 = Worksheets("STOK")
llRow = ws1.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
If Trim(Me.eksreznobox.Value) = "" Then
Me.ekreznobox.SetFocus
MsgBox "Lütfen ÜRÜN KODUNU Girin!"
Exit Sub
End If
With ws1
.Cells(llRow, 3).Value = Me.eksreznobox.Value
.Cells(llRow, 1).Value = Me.ekstarihtXT.Value
.Cells(llRow, 4).Value = Me.eksadI.Value
.Cells(llRow, 7).Value = Me.eksmiktartxt.Value
.Cells(llRow, 9).Value = Me.ekstutartxt.Value
.Cells(llRow, 8).Value = Me.textbox12.Value
.Cells(llRow, 5).Value = Me.EKSSOYADI.Value
.Cells(llRow, 6).Value = Me.TextBox15.Value
.Cells(llRow, 2).Value = Me.TextBox26.Value
.Cells(llRow, 11).Value = Me.TextBox27.Value
ekstutartxt.Value = eksmiktartxt.Value * textbox12.Value
End With
Dim lllRow As Long
Dim ws2 As Worksheet
Set ws2 = Worksheets("kasa")
lllRow = ws2.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
If Trim(Me.eksreznobox.Value) = "" Then
Me.ekreznobox.SetFocus
MsgBox "Lütfen ÜRÜN KODUNU Girin!"
Exit Sub
End If
Me.TextBox52.Value = "SATIŞ"
With ws2
.Cells(lllRow, 1).Value = Me.ekstarihtXT.Value
.Cells(lllRow, 5).Value = Me.TextBox19.Value
.Cells(lllRow, 6).Value = Me.TextBox20.Value
.Cells(lllRow, 7).Value = Me.TextBox21.Value
.Cells(lllRow, 9).Value = Me.TextBox23.Value
.Cells(lllRow, 3).Value = Me.TextBox51.Value
.Cells(lllRow, 2).Value = Me.TextBox26.Value
.Cells(lllRow, 4).Value = Me.TextBox52.Value
ekstutartxt.Value = eksmiktartxt.Value * textbox12.Value
End With
With kayit_formu.ListBox6
.BackColor = vbWhite
.ColumnCount = 9
.ColumnWidths = "50;33;45;55;60;55;42;43;60"
.ForeColor = vbBlack
If Sheets("SATISHAREKETLERİ").Range("A1") = Empty Then
.RowSource = Empty
Else
.RowSource = "SATISHAREKETLERİ!a1:i" & [SATISHAREKETLERİ!A1048500].End(3).Row
End If
End With
MsgBox "Bir Kayit Yapildi!"
End With
Me.TextBox25.Text = CStr(ThisWorkbook.Sheets("SEPET").Range("G1").Value)
Me.TextBox24.Text = CStr(ThisWorkbook.Sheets("SEPET").Range("G2").Value)
End Sub
You can try this code.
Worksheets(“Sheet1″).Range(“A1:G22″).Copy _
Destination:=Worksheets(“Sheet2″).Range(“E5″)