Excel VBA: 用户窗体代码替换行内容而不是添加新内容
Excel VBA: Userform code replaces row content instead of adding a new one
我是初学者,在 VBA 中学习编码,这是我的第一个 post。我最近一直在为如何让我的用户窗体工作而苦苦挣扎。
这是我的用户窗体中 "Add" 按钮的代码,每当我 运行 该宏(通过按添加按钮)时,它都会占据我的 [=33] 的顶行=] 并替换单元格的内容。
我想要实现的实际上是在 sheet 中添加一个新行,其中包含我输入的所有信息。这几天我一直在苦苦挣扎,所以这就是为什么(经过密集的 google 搜索)我终于决定向你们寻求帮助 :) 希望我已经足够清楚了。
祝你有愉快的一天,
Dim L As Integer
If MsgBox("Are you sure to create a new row with those information ?", vbYesNo, "Confirmation") = vbYes Then
L = Sheets("BDD").Range("A65536").End(xlUp).Row + 1 'Adding the new line to the sheet
Range("B" & L).Value = CboResp.Text
Range("C" & L).Value = CboStat.Text
Range("D" & L).Value = CboNat.Text
Range("E" & L).Value = CboPrio.Text
Range("F" & L).Value = CboAff.Text
Range("G" & L).Value = CboLea.Text
Range("H" & L).Value = txtDateRD.Text
Range("I" & L).Value = txtDateEnv.Text
Range("J" & L).Value = CboIni.Text
Range("K" & L).Value = CboISAV.Text
Range("L" & L).Value = txtVin.Text
Range("M" & L).Value = txtContrat.Text
Range("N" & L).Value = txtClientFinal.Text
Range("O" & L).Value = txtImmat.Text
Range("P" & L).Value = txtNomCon.Text
Range("Q" & L).Value = txtPrenomCon.Text
Range("R" & L).Value = txtTelCon.Text
Range("S" & L).Value = txtMotif.Text
Range("T" & L).Value = txtDateRDV.Text
Range("U" & L).Value = txtDateInter.Text
Range("V" & L).Value = CboVH.Text
Range("W" & L).Value = txtComment.Text
Range("X" & L).Value = CboCon.Text
End If
这样会更稳健:
Dim L As Long
If MsgBox("Are you sure to create a new row with those information ?", vbYesNo, "Confirmation") = vbYes Then
With Sheets("BDD")
L = .Range("A65536").End(xlUp).Row + 1 'Adding the new line to the sheet
.Range("B" & L).Value = CboResp.Text
.Range("C" & L).Value = CboStat.Text
.Range("D" & L).Value = CboNat.Text
.Range("E" & L).Value = CboPrio.Text
.Range("F" & L).Value = CboAff.Text
.Range("G" & L).Value = CboLea.Text
'etc etc
End with
End If
假设每行添加的数据在 ColA 中始终有一个值。
我是初学者,在 VBA 中学习编码,这是我的第一个 post。我最近一直在为如何让我的用户窗体工作而苦苦挣扎。
这是我的用户窗体中 "Add" 按钮的代码,每当我 运行 该宏(通过按添加按钮)时,它都会占据我的 [=33] 的顶行=] 并替换单元格的内容。
我想要实现的实际上是在 sheet 中添加一个新行,其中包含我输入的所有信息。这几天我一直在苦苦挣扎,所以这就是为什么(经过密集的 google 搜索)我终于决定向你们寻求帮助 :) 希望我已经足够清楚了。
祝你有愉快的一天,
Dim L As Integer
If MsgBox("Are you sure to create a new row with those information ?", vbYesNo, "Confirmation") = vbYes Then
L = Sheets("BDD").Range("A65536").End(xlUp).Row + 1 'Adding the new line to the sheet
Range("B" & L).Value = CboResp.Text
Range("C" & L).Value = CboStat.Text
Range("D" & L).Value = CboNat.Text
Range("E" & L).Value = CboPrio.Text
Range("F" & L).Value = CboAff.Text
Range("G" & L).Value = CboLea.Text
Range("H" & L).Value = txtDateRD.Text
Range("I" & L).Value = txtDateEnv.Text
Range("J" & L).Value = CboIni.Text
Range("K" & L).Value = CboISAV.Text
Range("L" & L).Value = txtVin.Text
Range("M" & L).Value = txtContrat.Text
Range("N" & L).Value = txtClientFinal.Text
Range("O" & L).Value = txtImmat.Text
Range("P" & L).Value = txtNomCon.Text
Range("Q" & L).Value = txtPrenomCon.Text
Range("R" & L).Value = txtTelCon.Text
Range("S" & L).Value = txtMotif.Text
Range("T" & L).Value = txtDateRDV.Text
Range("U" & L).Value = txtDateInter.Text
Range("V" & L).Value = CboVH.Text
Range("W" & L).Value = txtComment.Text
Range("X" & L).Value = CboCon.Text
End If
这样会更稳健:
Dim L As Long
If MsgBox("Are you sure to create a new row with those information ?", vbYesNo, "Confirmation") = vbYes Then
With Sheets("BDD")
L = .Range("A65536").End(xlUp).Row + 1 'Adding the new line to the sheet
.Range("B" & L).Value = CboResp.Text
.Range("C" & L).Value = CboStat.Text
.Range("D" & L).Value = CboNat.Text
.Range("E" & L).Value = CboPrio.Text
.Range("F" & L).Value = CboAff.Text
.Range("G" & L).Value = CboLea.Text
'etc etc
End with
End If
假设每行添加的数据在 ColA 中始终有一个值。