I have created a simple user form which perfectly work data entry to excel sheet. Once data entry is done, I save both excel sheet(sheet 1) and VBA file(micro enable) by clicking save button. However, my excel file is empty when I reopen it.
I have created a simple user form which perfectly work data entry to excel sheet. Once data entry is done, I save both excel sheet(sheet 1) and VBA file(micro enable) by clicking save button. However, my excel file is empty when I reopen it.
Noted. thank you
Problem solved by removing auto save mode in my workbook and userform.
Your code is already writing the $value to column G. Therefore only an additional entry in column K would be required. I have added this to your 'Submit2_Click' code below.
Private Sub Submit2_Click()
Dim LastRow As Long
' Application.DisplayAlerts = False
' Range("B3").Select
' ActiveCell.End(xlDown).Select ' xlDown isn't recommended
LastRow = Cells(Rows.Count, "B").End(xlUp).Row + 1
Cells(LastRow, 2).Value = txtdate2.Text
Cells(LastRow, 3).Value = txtexplanation2.Text
Cells(LastRow, 5).Value = txtReseller2.Text
Cells(LastRow, 4).Value = TxtProduct2.Text
Cells(LastRow, 6).Value = txtTypeOfCost2.Text
Cells(LastRow, 7).Value = txtvalue2.Text
If InStr(1, "cake,bread", TxtProduct2.Text, vbTextCompare) Then
Cells(LastRow, "K").Value = txtvalue2.Text
End If
Cells(LastRow, 1).Formula = "=Oops(ROW())"
' ActiveWorkbook.SaveAs "userformdata_12Nov2017"
' Application.DisplayAlerts = True
End Sub
I wonder if the extra entry in column K would upset your formula in column A. You will see that the above code writes a formula in column A, "=Oops(ROW())". 'Oops' is a UDF I have written for you to do the job of the formula you have so that you don't have to keep extra blank rows with formulas in your worksheet. Here is the UDF.
Option Explicit
Function Oops(R As Long) As String
' 15 Nov 2017
Dim Rng As Range
Dim Amt As Double
With Application
.Volatile
Set Rng = .Union(Range(Cells(R, "I"), Cells(R, "J")), _
Range(Cells(R, "L"), Cells(R, "AH")))
Amt = .Sum(Cells(R, "H").Value, Cells(R, "K").Value)
Amt = Round(Amt - .Sum(Rng), 2)
End With
Oops = IIf(Round(Cells(R, "G").Value, 2) = Amt, "OK", "Oops")
End Function
If you want to use it, paste this procedure in a standard code module (by default 'Module1').
Cells(LastRow, 2).Value = txtdate2.Text
writing to the last row, but in your code it was Cells(LastRow + 1, 2).Value = txtdate2.Text
which is writing one row lower.