[英]copy cells down to next date and repeat
我在一欄中有日期。 每個日期由8個單元格分隔,這8個單元格為空。 我想用第一個日期填充8個空單元格到達下一個日期時,我想用下一個日期填充接下來的8個空單元格,然后重復直到直到最后一個日期為止,之后再填充以下8個空單元格最后日期。
我認為我的問題還不夠清楚,因為我沒有正確的答案
05/06/2015
06/06/2015
07/06/2015
應該變成
05/06/2015
05/06/2015
05/06/2015
05/06/2015
05/06/2015
05/06/2015
05/06/2015
05/06/2015
05/06/2015
06/06/2015
06/06/2015
06/06/2015
06/06/2015
06/06/2015
06/06/2015
06/06/2015
06/06/2015
06/06/2015
07/06/2015
07/06/2015
07/06/2015
07/06/2015
07/06/2015
07/06/2015
07/06/2015
07/06/2015
07/06/2015
這是我從whytheq quickandfun方法獲得的輸出
05/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
06/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
07/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
08/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
09/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
10/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
11/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
12/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
13/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
14/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
15/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
16/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
17/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
18/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
19/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
20/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
21/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
22/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
23/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
24/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
25/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
26/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
27/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
28/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
29/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
30/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
31/07/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
01/08/2015
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
00/01/1900
我也嘗試將公式更改為
.Value = "=R[-1]C1"
解決了下面的答案
lastRow1 = Sheets(myValue).Cells(Sheets(myValue).Rows.Count, "B").End(xlUp).Row + 7
For r = 2 To lastRow1
If Sheets(myValue).Cells(r, 2) = vbNullString Then
Sheets(myValue).Cells(r, 2) = Sheets(myValue).Cells(r, 2).Offset(-1, 0)
End If
Next
像這樣的事情應該做的工作:
Dim wsh As Worksheet
Dim i As Integer, sCol As String
Dim cd Date, dd As Date
'column name which stores dates
sCol = "A"
cd = Date 'get current date
'Replace "Sheet1" with proper sheet name
Set wsh = ThisWorkbook.Worksheets("Sheet1")
'starting row
i = 1 'if the first date is in 1. row
'get first date
dd = CDate(wsh.Range(sCol & i))
'till the date from cell is less then current date
Do While dd<cd
'till the next cell is empty
Do While wsh.Range(sCol & i+1)=""
wsh.Range(sCol & i) = dd
i = i + 1
Loop
'get next date
dd = CDate(wsh.Range(sCol & i))
Loop
'here add another loop, you have to define condition to recognize last empty cell - as whytheq mentioned in the comment to the my answer
Set wsh = Nothing
注意: 此代碼不限於連續8個空單元格!
不是很漂亮:
Sub eightAtATime()
dim colLetter as string
colLetter = "A"
Dim r As Integer
With ThisWorkbook.ActiveSheet
r = .Cells(.Rows.Count, 1).End(xlUp).Row
End With
Do Until r < 1
Range(colLetter & r + 1 & ":" & colLetter & r + 8) = Range(colLetter & r)
r = r - 9
Loop
End Sub
盡管類似下面的內容更有趣,並且無論是否填充8個單元格都將填補空白:盡管它假定在最后一個單元格之后填充8個單元格:
Sub quickAndFun()
Dim lastRow As Integer
With ThisWorkbook.ActiveSheet
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
End With
With Range("A1:A" & lastRow + 8).SpecialCells(xlCellTypeBlanks)
.Value = "=R[-1]C1"
End With
End Sub
如果第一個單元格是B3
嘗試以下操作:
Sub quickAndFun()
Dim lastRow As Integer
With ThisWorkbook.ActiveSheet
lastRow = .Cells(.Rows.Count, 2).End(xlUp).Row
End With
With Range("B3:B" & lastRow + 8).SpecialCells(xlCellTypeBlanks)
.Value = "=R[-1]C2" '<<<<now ok as it need to reference Column 2
End With
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.