简体   繁体   中英

Excel/VBA - Userform overwriting existing data

I have written a user form for staff to input data on items confiscated, print out copies of said form and save all data inputted.

This has worked well for the 2 years, but the universe has now supplied me a bigger and better idiot. Apparently Rows were deleted from the worksheet so that someone could return items to a person without leaving any record.

Now - any data input to the form overwrites previous lines. Each new input overwrites another line higher(older). I am using the following lines to find next empty row and enter data on work sheet. It was writing 5 rows of the same data, as the code below was now written 5 times in a row.

I can not see where the error is - or understand how deleting Rows on worksheet would have the effect it has or change the code on the userform?

' WRITE DATA TO SPREADSHEET    

emptyRow = WorksheetFunction.CountA(Range("F:F")) + 1

RowCount = Worksheets("Sheet1").Range("A2").CurrentRegion.Rows.Count
With Worksheets("Sheet1").Range("A2")
.Offset(emptyRow, 0).Value = Me.txtSurname.Value
.Offset(emptyRow, 1).Value = Me.numCRN.Value
.Offset(emptyRow, 2).Value = Me.cboUnit.Value
.Offset(emptyRow, 3).Value = DateValue(Me.txtDateConf.Value)
.Offset(emptyRow, 4).Value = Me.numBoxnum.Value
.Offset(emptyRow, 5).Value = Me.txtItem6.Value
.Offset(emptyRow, 6).Value = Me.txtItemDesc6.Value
.Offset(emptyRow, 7).Value = Me.numQTY6.Value
.Offset(emptyRow, 8).Value = Me.cboCondition6.Value
.Offset(emptyRow, 9).Value = Me.cboReason6.Value
If Me.chkHistCheck6.Value = True Then
.Offset(emptyRow, 10).Value = "Yes"
Else
.Offset(emptyRow, 10).Value = "No"
End If

If Me.chkHistCheck6.Value = "" Then
    .Offset(emptyRow, 10).Value = "No"
End If

.Offset(emptyRow, 11).Value = Format(Now, "dd/mm/yyyy hh:nn:ss")
End With

Sorry All for delayed response. We have figured that the code, whilst clunky, was still fine but there was some memory issue at play.

The work sheet being written to was at 1400 lines, with 2 years of data inputs. Discovered that if we simply archived (cut and pasted) that all out to a new file the form and worksheet ran flawlessly again.

Sure someone here would have figured it out in seconds on looking at the whole file but sharing the data or evening the whole code was a no-no. Thanks for your time all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM