I have an issue with my VBA code, maybe someone can explain where I made a mistake and how can it be fixed.
My goal is to create 7 columns without skipping cells.
I have 7 text boxes but not all of them has to be filled every time data is entered.
At the moment when data is added only in Name text box it creates new row but when only surname is added it skips first empty lines and enters diagonally under Name entry(as in attachment) Excel img
Also there is a mistake when I want to add entry in other text boxes, entered data is just moved around.
Private Sub CommandButton1_Click()
Dim LR As Long
LR = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(LR, 1).Value = Name.Value
Cells(LR, 2).Value = Surname.Value
Cells(LR, 3).Value = Address.Value
Cells(LR, 4).Value = Phone.Value
Cells(LR, 5).Value = City.Value
Cells(LR, 6).Value = Car.Value
Cells(LR, 7).Value = Job.Value
Name.Value = ""
Surname.Value = ""
Address.Value = ""
Phone.Value = ""
City.Value = ""
Car.Value = ""
Job.Value = ""
End Sub
You are always looking for the last row in column A, thats why it adds the surname one row below the last row of column A. If you want to know the last row of another column, you need to change the 1 after Rows.Count into the corresponding number. You can also do this in one line, have a gander:
Private Sub CommandButton1_Click()
Cells(Cells(Rows.Count, 1).End(xlUp).Row + 1, 1).Value = Name.Value
Cells(Cells(Rows.Count, 2).End(xlUp).Row + 1, 2).Value = Surname.Value
Cells(Cells(Rows.Count, 3).End(xlUp).Row + 1, 3).Value = Address.Value
Cells(Cells(Rows.Count, 4).End(xlUp).Row + 1, 4).Value = Phone.Value
Cells(Cells(Rows.Count, 5).End(xlUp).Row + 1, 5).Value = City.Value
Cells(Cells(Rows.Count, 6).End(xlUp).Row + 1, 6).Value = Car.Value
Cells(Cells(Rows.Count, 7).End(xlUp).Row + 1, 7).Value = Job.Value
Name.Value = ""
Surname.Value = ""
Address.Value = ""
Phone.Value = ""
City.Value = ""
Car.Value = ""
Job.Value = ""
End Sub
Related
I am a complete novice at Excel VBA and I am currently attempting a project on Excel VBA. I have created a UserForm that would allow the user to enter data onto the Excel Sheet by completing the fields in the UserForm. I have tested all the codes individually and they have worked fine.
For the user to access the UserForm, I have added an ActiveX Command Button on a separate sheet on the same workbook. However, when accessing the UserForm from the ActiveX Command Button, some of the codes do not run (mainly the code that flags out the duplicate entry, as well as the code that generates serial numbers).
Where did I go wrong in my code?
This is my code to adding new data as well as the code to flag out duplicate entries. When opening the UserForm from the ActiveX Command Button, adding new data works fine but it does not flag out duplicate entries in the data. (However, testing the code itself in VBA works perfectly fine).
Private Sub cmdAddNewCustomer_Click()
Dim count As Long
Dim lastrow As Long
Dim lCustomerID As String
Dim ws As Worksheet
Set ws = Worksheets("Customer Data")
'find first empty row in database
lrow = ws.Cells.Find(what:="*", searchorder:=xlRows, _
Searchdirection:=xlPrevious, LookIn:=xlValues).Row + 1
lCustomerID = txtCustomerID
count = 0
With ws
For currentrow = 1 To lrow
If lCustomerID = Cells(currentrow, 1) Then
count = count + 1
End If
If count > 1 Then
.Cells(currentrow, 1).Value = ""
.Cells(currentrow, 2).Value = ""
.Cells(currentrow, 3).Value = ""
.Cells(currentrow, 4).Value = ""
.Cells(currentrow, 5).Value = ""
.Cells(currentrow, 6).Value = ""
.Cells(currentrow, 7).Value = ""
.Cells(currentrow, 8).Value = ""
.Cells(currentrow, 9).Value = ""
.Cells(currentrow, 10).Value = ""
.Cells(currentrow, 11).Value = ""
.Cells(currentrow, 12).Value = ""
.Cells(currentrow, 13).Value = ""
.Cells(currentrow, 14).Value = ""
MsgBox ("CustomerID already exists!")
End If
If count = 0 Then
.Cells(lrow, 1).Value = Me.txtCustomerID.Value
.Cells(lrow, 2).Value = Me.txtCustomerName.Value
.Cells(lrow, 3).Value = Me.cboCustomerStatus.Value
.Cells(lrow, 4).Value = Me.txtContactPerson.Value
.Cells(lrow, 5).Value = Me.cboDepartment.Value
.Cells(lrow, 6).Value = Me.txtPosition.Value
.Cells(lrow, 7).Value = Me.cboRoleType.Value
.Cells(lrow, 8).Value = Me.txtofficeHP1.Value
.Cells(lrow, 9).Value = Me.txtOfficeHP2.Value
.Cells(lrow, 10).Value = Me.txtMobileHP1.Value
.Cells(lrow, 11).Value = Me.txtMobileHP2.Value
.Cells(lrow, 12).Value = Me.txtEmail1.Value
.Cells(lrow, 13).Value = Me.txtEmail2.Value
.Cells(lrow, 14).Value = Me.txtEmail3.Value
End If
Next currentrow
End With
'clear the data
Me.txtCustomerName.Value = ""
Me.cboCustomerStatus.Value = ""
Me.txtContactPerson.Value = ""
Me.cboDepartment.Value = ""
Me.txtPosition.Value = ""
Me.cboRoleType.Value = ""
Me.txtofficeHP1.Value = ""
Me.txtOfficeHP2.Value = ""
Me.txtMobileHP1.Value = ""
Me.txtMobileHP2.Value = ""
Me.txtEmail1.Value = ""
Me.txtEmail2.Value = ""
Me.txtEmail3.Value = ""
End Sub
This is the code to generate serial numbers. (Same problem, does not work when accessed via ActiveX Command Button but works fine when tested individually in VBA).
Sub FindCustomerID()
Dim lastrow
Dim lastnum As Long
Dim ws As Worksheet
Set ws = Worksheets("Customer Data")
If Me.cboCountry = "" Or Me.txtCustomerName = "" Then
Exit Sub
End If
serialno = 1
lastrow = ws.Cells(Rows.count, 1).End(xlUp).Row
CountryCode = UCase(Left(Me.cboCountry, 3))
CustomerCode = UCase(Left(Me.txtCustomerName, 10))
'assemble them into CustomerID
CustomerID = CountryCode & CustomerCode & serialno
For currentrow = 2 To lastrow
If CustomerID = Cells(currentrow, 1) Then
'find last number that applies
serialno = serialno + 1
End If
're-assign customerID with new serial number
CustomerID = CountryCode & CustomerCode & serialno
Next currentrow
Me.lblCustomerID = CustomerID
End Sub
And lastly, this is the code from the ActiveX Command Button that brings out the UserForm.
Private Sub cmdNCustomerData_Click()
frmCustomerdata.Show
End Sub
The cause of the problem you described is a missing . to qualify Cells(currentrow, 1). Because you added the ActiveX button to a different sheet, the line
If lCustomerID = Cells(currentrow, 1) Then
accesses Cells(currentrow, 1) of that sheet. To fix this the range needs to be qualified with a . to become
If lCustomerID = .Cells(currentrow, 1) Then
I would also take
If count = 0 Then
.
.
.
End If
outside the loop. You are repeating these lines many times unnecessarily.
The first block of code then becomes:
Private Sub cmdAddNewCustomer_Click()
Dim count As Long
Dim lastrow As Long
Dim lCustomerID As String
Dim ws As Worksheet
Set ws = Worksheets("Customer Data")
'find first empty row in database
lrow = ws.Cells.Find(what:="*", searchorder:=xlRows, _
Searchdirection:=xlPrevious, LookIn:=xlValues).Row + 1
lCustomerID = txtCustomerID
count = 0
With ws
' Count backward to delete rows completely
For currentrow = lrow - 1 To 1 Step -1
If lCustomerID = .Cells(currentrow, 1) Then
count = count + 1
End If
If count > 1 Then
.Cells(currentrow, 1).Resize(1, 14).ClearContents
' Uncomment the following line to delete the whole row completely
'.Rows(currentrow).Delete
End If
Next currentrow
If count > 1 Then
MsgBox (count - 1 " duplicates of CustomerID found and cleared!")
ElseIf count = 0 Then
.Cells(lrow, 1).Value = Me.txtCustomerID.Value
.Cells(lrow, 2).Value = Me.txtCustomerName.Value
.Cells(lrow, 3).Value = Me.cboCustomerStatus.Value
.Cells(lrow, 4).Value = Me.txtContactPerson.Value
.Cells(lrow, 5).Value = Me.cboDepartment.Value
.Cells(lrow, 6).Value = Me.txtPosition.Value
.Cells(lrow, 7).Value = Me.cboRoleType.Value
.Cells(lrow, 8).Value = Me.txtofficeHP1.Value
.Cells(lrow, 9).Value = Me.txtOfficeHP2.Value
.Cells(lrow, 10).Value = Me.txtMobileHP1.Value
.Cells(lrow, 11).Value = Me.txtMobileHP2.Value
.Cells(lrow, 12).Value = Me.txtEmail1.Value
.Cells(lrow, 13).Value = Me.txtEmail2.Value
.Cells(lrow, 14).Value = Me.txtEmail3.Value
End If
End With
'clear the data
Me.txtCustomerName.Value = ""
Me.cboCustomerStatus.Value = ""
Me.txtContactPerson.Value = ""
Me.cboDepartment.Value = ""
Me.txtPosition.Value = ""
Me.cboRoleType.Value = ""
Me.txtofficeHP1.Value = ""
Me.txtOfficeHP2.Value = ""
Me.txtMobileHP1.Value = ""
Me.txtMobileHP2.Value = ""
Me.txtEmail1.Value = ""
Me.txtEmail2.Value = ""
Me.txtEmail3.Value = ""
End Sub
In the FindCustomerID subroutine you have exactly the same problem with the line
If CustomerID = Cells(currentrow, 1) Then
as Cells(currentrow, 1) is not qualified and therefore, should become
If CustomerID = ws.Cells(currentrow, 1) Then
You are also reassigning the CustomerID many times unnecessarily. I would take the reassignment inside the If statement and the loop will become
For currentrow = 2 To lastrow
If CustomerID = ws.Cells(currentrow, 1) Then
'find last number that applies
serialno = serialno + 1
're-assign customerID with new serial number
CustomerID = CountryCode & CustomerCode & serialno
End If
Next currentrow
This way CustomerID is only reassigned if and only if serialno changes.
I have a nested for loop. Essentially what I'm doing is summing the values in column 14 and 16 when 2 or more rows match with each other in columns 5, 6, 7, 10, and 11.
It may not be the most efficient code but the gist of my code is that with each iteration of k2 (reference row), I want the inner loop to compare k2 to each row inside the table aside from row k2.
Now, the problem is that with my current setup, it is possible for k2 to be compared to its own row when the value of i2 - 1 = k2 thus adding an extra value to my sumnla and sumweight.
What would be the best way for me to overcome this problem? And as you can probably tell, I am fairly new to VBA with about a month of experience so far so any tips of coding this more efficiently would be much appreciated.
'To consolidate materials, adding nla and weight together
For k2 = ThisWorkbook.Sheets("Summary").UsedRange.Rows.Count To 2 Step -1
sumnla = ThisWorkbook.Sheets("Summary").Cells(k2, 14).Value
sumweight = ThisWorkbook.Sheets("Summary").Cells(k2, 16).Value
valuek = k2
For i2 = ThisWorkbook.Sheets("Summary").UsedRange.Rows.Count To 2 Step -1
If _
ThisWorkbook.Sheets("Summary").Cells(k2, 5).Value = ThisWorkbook.Sheets("Summary").Cells(i2 - 1, 5).Value _
And ThisWorkbook.Sheets("Summary").Cells(k2, 6).Value = ThisWorkbook.Sheets("Summary").Cells(i2 - 1, 6).Value _
And ThisWorkbook.Sheets("Summary").Cells(k2, 7).Value = ThisWorkbook.Sheets("Summary").Cells(i2 - 1, 7).Value _
And ThisWorkbook.Sheets("Summary").Cells(k2, 10).Value = ThisWorkbook.Sheets("Summary").Cells(i2 - 1, 10).Value _
And ThisWorkbook.Sheets("Summary").Cells(k2, 11).Value = ThisWorkbook.Sheets("Summary").Cells(i2 - 1, 11).Value _
Then
sumnla = sumnla + ThisWorkbook.Sheets("Summary").Cells(i2 - 1, 14).Value
sumweight = sumweight + ThisWorkbook.Sheets("Summary").Cells(i2 - 1, 16).Value
valuek = valuek & " " & i2
End If
Next
Worksheets("Procurement Table").Select
'add a row at the end of the table
Dim newrow As ListRow
Set newrow = ListObj.ListRows.Add
With newrow
.Range(1) = Worksheets("Summary").Cells(k2, 10).Value
.Range(2) = Worksheets("Summary").Cells(k2, 6).Value
.Range(3) = Worksheets("Summary").Cells(k2, 7).Value
.Range(4) = Worksheets("Summary").Cells(k2, 11).Value
.Range(5) = Worksheets("Summary").Cells(k2, 12).Value
.Range(6) = Worksheets("Summary").Cells(k2, 13).Value
.Range(7) = 1
.Range(8) = sumnla
.Range(9) = "=VALUE([#EngCF])*VALUE([#BaseNLA])"
.Range(10) = sumweight
.Range(11) = "=VALUE([#EngCF])*VALUE([#BaseWeight])"
.Range(12) = 1
.Range(13) = "=VALUE([#EngFactoredWeight])*VALUE([#FabAllowance])"
.Range(14) = valuek
End With
Next
You can use goto method to go to next loop. you can write as below.
if k2 = i2-1 then goto nextLoop
and write nextLoop: just before the Next at the end.
Also, my suggestion is about a better way to refer your worksheets. you can use a variable to refer to your worksheets. This way, you don't need to write Worksheets("Summary") all the time. you can do this by defining a new variable as worksheet.
Dim ws as Worksheet
set ws = Thisworkbook.Worksheets("Summary")
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 have made a simple form to allow me to input data and store it on an excel table. Before adding simple formulas I was able to submit and the data and populate the next line of the excel table when I hit submit on my form.
I have now added simple formulas that will be used on the data I submit, however when I try to submit data, the formula and inputted data split over 2 separate line in my Excel table (as you can see in the attached image). Can you help me populate the data into the same line as the formulas.
Apologies for any bad code as I haven't tidied it yet, just getting the basics right.
Private Sub CommandButton1_Click()
lastrow = ThisWorkbook.Worksheets("Sheet1").Cells(Rows.Count, 2).End(xlUp).Row
ThisWorkbook.Worksheets("Sheet1").Cells(lastrow + 1, 1).Value = TextBox1.Text
ThisWorkbook.Worksheets("Sheet1").Cells(lastrow + 1, 3).Value = TextBox9.Text
ThisWorkbook.Worksheets("Sheet1").Cells(lastrow + 1, 8).Value = TextBox12.Text
ThisWorkbook.Worksheets("Sheet1").Cells(lastrow + 1, 10).Value = TextBox11.Text
ThisWorkbook.Worksheets("Sheet1").Cells(lastrow + 1, 11).Value = TextBox10.Text
ThisWorkbook.Worksheets("Sheet1").Cells(lastrow + 1, 16).Value = TextBox15.Text
ThisWorkbook.Worksheets("Sheet1").Cells(lastrow + 1, 18).Value = TextBox14.Text
ThisWorkbook.Worksheets("Sheet1").Cells(lastrow + 1, 19).Value = TextBox13.Text
Sheets("Sheet1").Range("A4").Select
ActiveCell.EntireRow.Insert shift:=xlDown
Sheets("Sheet1").Range("A4:V4").Select
Selection.Borders.Weight = xlThin
Sheets("Sheet1").Range("b4").Select
ActiveCell.Formula = "=(a4*1440)"
Sheets("Sheet1").Range("d4").Select
ActiveCell.Formula = "=(b4*(c4/100))"
Sheets("Sheet1").Range("e4").Select
ActiveCell.Formula = "=((d4/b4)*100)"
Sheets("Sheet1").Range("f4").Select
ActiveCell.Formula = "=(100-e4)"
Sheets("Sheet1").Range("i4").Select
ActiveCell.Formula = "=(h4*1440)"
Sheets("Sheet1").Range("l4").Select
ActiveCell.Formula = "=(i4*(((j4+k4)/2)/100))"
Sheets("Sheet1").Range("m4").Select
ActiveCell.Formula = "=((l4/i4)*100)"
Sheets("Sheet1").Range("n4").Select
ActiveCell.Formula = "=(100-m4)"
Sheets("Sheet1").Range("q4").Select
ActiveCell.Formula = "=(p4*1440)"
Sheets("Sheet1").Range("t4").Select
ActiveCell.Formula = "=(q4*(((r4+s4)/2)/100))"
Sheets("Sheet1").Range("u4").Select
ActiveCell.Formula = "=((t4/q4)*100)"
Sheets("Sheet1").Range("v4").Select
ActiveCell.Formula = "=(100-u4)"
End Sub
Don't use .Select. You might benefit from reading
How to avoid using Select in Excel VBA.
Use a variable InsertRow instead of LastRow and add + 1 there. So the calculation is done only once and not in every line you use it.
Use the InsertRow for your values and formulas, and adjust the formulas from row dependent formulas A4 to row independent A:A so the work in every row and not only row 4.
You should name your Button and TextBoxes properly (meaningful). Numbering them is the worst solution as you can easily mix them up and end up in a mess of controls.
So you end up with:
Option Explicit
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim InsertRow As Long
InsertRow = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row + 1
With ws
.Cells(InsertRow, 1).Value = TextBox1.Text
.Cells(InsertRow, 3).Value = TextBox9.Text
.Cells(InsertRow, 8).Value = TextBox12.Text
.Cells(InsertRow, 10).Value = TextBox11.Text
.Cells(InsertRow, 11).Value = TextBox10.Text
.Cells(InsertRow, 16).Value = TextBox15.Text
.Cells(InsertRow, 18).Value = TextBox14.Text
.Cells(InsertRow, 19).Value = TextBox13.Text
.Cells(InsertRow, "B").Formula = "=(A:A*1440)"
.Cells(InsertRow, "D").Formula = "=(B:B*(C:C/100))"
.Cells(InsertRow, "E").Formula = "=((D:D/B:B)*100)"
.Cells(InsertRow, "F").Formula = "=(100-E:E)"
.Cells(InsertRow, "I").Formula = "=(H:H*1440)"
.Cells(InsertRow, "L").Formula = "=(I:I*(((J:J+K:K)/2)/100))"
.Cells(InsertRow, "M").Formula = "=((L:L/I:I)*100)"
.Cells(InsertRow, "N").Formula = "=(100-M:M)"
.Cells(InsertRow, "Q").Formula = "=(P:P*1440)"
.Cells(InsertRow, "T").Formula = "=(Q:Q*(((R:R+S:S)/2)/100))"
.Cells(InsertRow, "U").Formula = "=((T:T/Q:Q)*100)"
.Cells(InsertRow, "V").Formula = "=(100-U:U)"
End With
' not sure if this is needed
' ws.Cells(InsertRow, "A").EntireRow.Insert shift:=xlDown
ws.Range("A" & InsertRow & ":V" & InsertRow).Borders.Weight = xlThin
End Sub
Sheets("Sheet1").Range("A4").Select
ActiveCell.EntireRow.Insert shift:=xlDown
The code above might move down your data. Why not you create the formula directly at the excel sheet?
I am using the following code to enter data from Userform to Excel sheet and works fine.
The problem is that it overwrites the same row of data. But if I change:
.Cells(RowCount, 4).Value = Me.DepSectDrop.Value to contain a 1 --> .Cells(RowCount, 1).Value = Me.DepSectDrop.Value, and likewise for the rest (2 fore SiteFacOpen, 3 for CaseStartOpen, etc), it does not overwrite.
Private Sub cmdAdd_Click()
'Copy input values to sheet.
Dim RowCount As Long
Dim ws As Worksheet
Set ws = Worksheets("TRACK")
RowCount = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
.Cells(RowCount, 4).Value = Me.DepSectDrop.Value
.Cells(RowCount, 5).Value = Me.SiteFacOpen.Value
.Cells(RowCount, 6).Value = Me.CaseStartOpen.Value
.Cells(RowCount, 7).Value = Me.TypeDrop.Value
.Cells(RowCount, 8).Value = Me.ProcessDrop.Value
.Cells(RowCount, 9).Value = Me.CompNameOpen.Value
.Cells(RowCount, 10).Value = Me.CompEIDOpen.Value
.Cells(RowCount, 11).Value = Me.RespNameOpen.Value
.Cells(RowCount, 12).Value = Me.RespEIDOpen.Value
.Cells(RowCount, 13).Value = Me.DescOpen.Value
End With
'Clear input controls.
Me.DepSectDrop.Value = ""
Me.SiteFacOpen.Value = ""
Me.CaseStartOpen.Value = ""
Me.TypeDrop.Value = ""
Me.ProcessDrop.Value = ""
Me.CompNameOpen.Value = ""
Me.CompEIDOpen.Value = ""
Me.RespNameOpen.Value = ""
Me.RespEIDOpen.Value = ""
Me.DescOpen.Value = ""
End Sub
What do I need to do to so I maintain the right columns for it all to be entered, but not be overwritten? Thank you
You need to change all lines that start
.Cells(RowCount, 5).Value ...
To
.Cells(RowCount + 1, 5).Value
The '+1' bit means you're using the next blank line.
Also, as Samuel pointed out, you should also change to
RowCount = ws.Cells (Rows.Count, 4).End (xlUp).Offset (1,0).Row
so that you're testing a column that's guaranteed to have data in it!
Sorry, I missed the offset bit ... No need to '+1' if you're offsetting by 1 ... It amounts to the same thing.