2015-08-17 50 views
-1

我想要一個幫助我在空行添加信息的用戶表單。但是,在被填充的行之間找到空行。例如,第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 

我希望任何人都可以幫助我。會真的很感激它。謝謝。

回答

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