Excel VBA - 从开始日期到结束日期每周创建多个数据字符串

时间:2016-03-02 17:08:21

标签: excel-vba ms-office vba excel

如何让一些代码将一行数据(从B列到G列)重复到以下行,直到A行中有空单元格?

我拉到一起的代码如下所示。

Sub AddFlight_Click()

Dim NextRow As Long, LastRow As Long, ws As Worksheet, ColumnA As Range

Set ws = Sheets("JetAir Flight Plan")
NextRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
ColumnA = ws.Range ("A:A")

 'Data inserted in a userform are assigned to specific cells in a sheet. 

    ws.Range("A" & NextRow).Value = StartingFlightDateComboBox.Text
    ws.Range("P" & "2").Value = EndingFlightDateComboBox.Text
    ws.Range("B" & NextRow).Value = DayOfWeekComboBox.Text
    ws.Range("D" & NextRow).Value = ETATextBox.Text
    ws.Range("E" & NextRow).Value = TourOperatorTextBox.Text
    ws.Range("F" & NextRow).Value = FlightNumberTextBox.Text
    ws.Range("G" & NextRow).Value = FromToTextBox.Text
    ws.Range("H" & NextRow).Value = AllotmentTextBox.Text

     'A series of dates is created from a starting date 
     '    to an ending date in column A.

   ws.Range("A" & NextRow).Select
   Selection.DataSeries Rowcol:=xlColumns, Type:=xlChronological, Date:= _
       xlDay, Step:=7, Stop:=ws.Range("P" & "2").Value, Trend:=False

  'The data filled in the last row with the userform data through
  ' the first part of the macro will be copied and pasted in 
  ' the next row until there is a blank cell in column A.

Do While ColumnA(i) <> ""
ws.Range("B" & LastRow).Resize(, 6).Copy

Loop

End Sub

我不能让最后一部分工作。我收到的一个错误是ColumnA错误:运行时错误91:对象变量或未设置块变量。

但是我把它设置为Range(“A:A”)。有没有更好的方法来编写代码?

有关原因的任何建议吗?

感谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

Sub AddFlight_Click()

    Const RNG_END_DT As String = "P2"
    Dim NextRow As Long, LastRow As Long, ws As Worksheet

    Set ws = Sheets("JetAir Flight Plan")
    LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
    NextRow = LastRow + 1

    'Data inserted in a userform are assigned to specific cells in a sheet.
    ws.Range("A" & NextRow).Value = StartingFlightDateComboBox.Text
    ws.Range("B" & NextRow).Value = DayOfWeekComboBox.Text
    ws.Range("D" & NextRow).Value = ETATextBox.Text
    ws.Range("E" & NextRow).Value = TourOperatorTextBox.Text
    ws.Range("F" & NextRow).Value = FlightNumberTextBox.Text
    ws.Range("G" & NextRow).Value = FromToTextBox.Text
    ws.Range("H" & NextRow).Value = AllotmentTextBox.Text
    ws.Range(RNG_END_DT).Value = EndingFlightDateComboBox.Text

    'A series of dates is created from a starting date
    '    to an ending date in column A.
    ws.Range("A" & NextRow).DataSeries Rowcol:=xlColumns, _
        Type:=xlChronological, Date:=xlDay, Step:=7, _
        Stop:=ws.Range(RNG_END_DT).Value, Trend:=False

    'The data filled in the last row with the userform data through
    ' the first part of the macro will be copied and pasted in
    ' the next row until there is a blank cell in column A.
    LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
    ws.Range(ws.Range("B" & NextRow), ws.Cells(LastRow, "H")).FillDown

End Sub