Excel VBA - 从开始日期到结束日期每周创建多个数据字符串
Excel VBA - Create multiple data strings on a weekly basis from start date to ending date
如何让一些代码将一行中的一系列数据(从 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:未设置对象变量或 With 块变量。
但是我把它设置为Range("A:A")。有没有更好的方法来编写该代码?
关于原因有什么建议吗?
谢谢。
试试这个:
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
如何让一些代码将一行中的一系列数据(从 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:未设置对象变量或 With 块变量。
但是我把它设置为Range("A:A")。有没有更好的方法来编写该代码?
关于原因有什么建议吗?
谢谢。
试试这个:
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