Excel VBA Userform:当我更改数据输入的第一列时,数据将被覆盖

时间:2018-06-24 12:46:12

标签: excel vba userform

我正在使用以下代码将数据从Userform输入到Excel工作表,并且工作正常。 问题在于它会覆盖同一行数据。但是如果我改变: .Cells(RowCount, 4).Value = Me.DepSectDrop.Value包含1-> .Cells(RowCount, 1).Value = Me.DepSectDrop.Value,其余部分(SiteFacOpen为2,CaseStartOpen等为3)同样不会覆盖。

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

我需要怎么做,以便为所有要输入但不能被覆盖的内容保留正确的列?谢谢

1 个答案:

答案 0 :(得分:1)

您需要更改所有开始的行

     .Cells(RowCount, 5).Value ...

收件人

     .Cells(RowCount + 1, 5).Value 

“ + 1”位表示您正在使用下一个空白行。

而且,正如塞缪尔(Samuel)指出的那样,您还应该更改为

 RowCount = ws.Cells (Rows.Count, 4).End (xlUp).Offset (1,0).Row

这样您就可以测试保证有数据的列!


对不起,我错过了补偿位...如果您要补偿1,就不需要'+1'...