VBA Excel - 在写入之前检查单元格内容

时间:2013-04-21 23:17:55

标签: excel-vba vba excel

我是VBA的新手,并且在使用此UserForm的输出时出现问题。

Private Sub btnSubmit_Click()
Dim RowCount As Long

...

RowCount = Worksheets("Assignments").Range("A3").CurrentRegion.Rows.Count
With Worksheets("Assignments").Range("A3")
    .Offset(RowCount, 0).Value = Me.txtReportAddress.Value
    .Offset(RowCount, 1).Value = Me.cmbCityCounty.Value
    .Offset(RowCount, 2).Value = Me.txtFee.Value
    .Offset(RowCount, 3).Value = Me.txtDate.Value
    .Offset(RowCount, 4).Value = Me.cmbPropertyType.Value
    .Offset(RowCount, 5).Value = Me.txtComments.Value
End With

它提供了正确的输出,但任何后续条目都会覆盖过去的输入。

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

不熟悉'CurrentRegion',但看起来你在计算行数,然后覆盖最后一行;你算5行然后写进第5行,这是包含数据的最后一行,就像在RowCount中加1一样简单吗?

或者我对这样的事情所做的是:

RowCount = Application.CountA(Worksheets("Assignments").Columns("A"))
    ' Depending on what is in rows 1 and 2 of colomn A you may need to adjust
    ' ie if row 2 is blank then the count will be 1 higher than needed so you 
    ' can adjust accordingly
Dim c as Range
Set c = Worksheets("Assignments").Range("A" & RowCount + 1)
With c
    .value = Me.txtReportAddress.Value
    .Offset(0,1).Value... etc as per your code

希望这能回答你的问题。