首页 文章

将细胞复制到下一个日期并重复

提问于
浏览
0

我在一个专栏中有约会 . 每个日期由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

这是我为什么从theq 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

2 回答

  • 1

    不是很漂亮:

    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
    
  • 1

    这样的事情应该做的工作:

    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
    

    Note: 此代码不限于连续8个空单元格!

相关问题