使用VBA用户表单将信息添加到占用行之间的新行中

当占用的行之间有空行时,我需要一个代码来编程添加新行。

例如,第一个和第三个被占用的行之间有一个空行。

因此,我需要一个代码来编程,并使用VBA用户表单将信息插入占用行之间的空行。

我设法创build了一些编码,但似乎无法工作。 我希望任何人都可以帮助我。

谢谢。

Private Sub CommandAddButton1_Click() lastrow = Sheets("Programme Status Summary").Range("J" & Rows.Count).End(xlUp).Row Cells(lastrow + 1, "J").Value = TextBoxProjCode.Text Cells(lastrow + 1, "E").Value = TextBoxProjName.Text Cells(lastrow + 1, "C").Value = TextBoxSegment.Text Cells(lastrow + 1, "F").Value = TextBoxSummary.Text Cells(lastrow + 1, "G").Value = TextBoxAcc1.Text Cells(lastrow + 1, "H").Value = TextBoxAcc2.Text Cells(lastrow + 1, "I").Value = TextBoxProjM.Text Cells(lastrow + 1, "K").Value = TextBoxCountry.Text Cells(lastrow + 1, "L").Value = TextBoxRegulatory.Text Cells(lastrow + 1, "M").Value = TextBoxRiskLvl.Text Cells(lastrow + 1, "P").Value = TextBoxSchForecast.Text Cells(lastrow + 1, "R").Value = TextBoxSchPar.Text Cells(lastrow + 1, "S").Value = TextBoxImpact.Text Cells(lastrow + 1, "T").Value = TextBoxCustNonRetail.Text Cells(lastrow + 1, "U").Value = TextBoxCustRetail.Text Cells(lastrow + 1, "V").Value = TextBoxOutsourcingImp.Text Cells(lastrow + 1, "W").Value = TextBoxListImpt.Text Cells(lastrow + 1, "X").Value = TextBoxKeyStatus.Text Cells(lastrow + 1, "N").Value = TextBoxSchStart.Text Cells(lastrow + 1, "O").Value = TextBoxSchEnd.Text Cells(lastrow + 1, "Y").Value = TextBoxRagStatus.Text Cells(lastrow + 1, "Z").Value = TextBoxRagCost.Text Cells(lastrow + 1, "AA").Value = TextBoxRagBenefit.Text End Sub 

使用你的代码作为基础,你需要添加一个循环,find这些行:

 Private Sub CommandAddButton1_Click() dim lngLastRow as Long dim i as integer dim wksWork as worksheet set wksWork = thisworkbook.worksheets("Programme Status Summary") lngLastRow = wksWork.Range("J" & wksWork.Rows.Count).End(xlUp).Row for i = 1 to lngLastRow 'This starts in row 1, adjust accordingly if wkswork.cells(i,1).value="" or wksWork.cells(i,1).value = vbNullstring then 'Checks in column A if there is any value given with wksWork .Cells(i, "J").Value = TextBoxProjCode.Text .Cells(i, "E").Value = TextBoxProjName.Text .Cells(i, "C").Value = TextBoxSegment.Text .Cells(i, "F").Value = TextBoxSummary.Text .Cells(i, "G").Value = TextBoxAcc1.Text .Cells(i, "H").Value = TextBoxAcc2.Text .Cells(i, "I").Value = TextBoxProjM.Text .Cells(i, "K").Value = TextBoxCountry.Text .Cells(i, "L").Value = TextBoxRegulatory.Text .Cells(i, "M").Value = TextBoxRiskLvl.Text .Cells(i, "P").Value = TextBoxSchForecast.Text .Cells(i, "R").Value = TextBoxSchPar.Text .Cells(i, "S").Value = TextBoxImpact.Text .Cells(i, "T").Value = TextBoxCustNonRetail.Text .Cells(i, "U").Value = TextBoxCustRetail.Text .Cells(i, "V").Value = TextBoxOutsourcingImp.Text .Cells(i, "W").Value = TextBoxListImpt.Text .Cells(i, "X").Value = TextBoxKeyStatus.Text .Cells(i, "N").Value = TextBoxSchStart.Text .Cells(i, "O").Value = TextBoxSchEnd.Text .Cells(i, "Y").Value = TextBoxRagStatus.Text .Cells(i, "Z").Value = TextBoxRagCost.Text .Cells(i, "AA").Value = TextBoxRagBenefit.Text end with end if next i End Sub