在填充行之间的新行中添加信息

时间:2015-08-17 03:20:24

标签: excel vba userform

我想要一个帮助我添加空行信息的用户表单。但是,在填充的行之间找到空行。例如,第2行是空的,而第1行和第3行是填充的。因此,我需要一个能够遍历所有行的代码,无论哪一行是空的,它都将由用户表单输入填充。 我只设法提出这个代码,但它只适用于列的每一行。

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

我希望有人能帮助我。真的很感激。谢谢。

1 个答案:

答案 0 :(得分:0)

这应该遍历您的行并检查A列中的单元格是否为空,从第1行开始。也许您需要根据需要调整这些参数。

Private Sub CommandAddButton1_Click()
Dim lngLastRow As Long
Dim i As Integer
Dim wksWork As Worksheet

Set wksWork = ThisWorkbook.Worksheets("Programme Status Summary")
With wksWork
    lastrow = .Range("J" & .Rows.Count).End(xlUp).Row
    For i = 1 To lnglstwor 'starts at Row 1, adjust accordingly
        If .Cells(i, 1).Value = "" Or .Cells(i, 1).Value = vbNullString Then 'Checks if the first cell of the row isempty
            .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 If
    Next i
End With
End Sub