开始日期和结束日期水平填充

时间:2019-09-03 14:43:43

标签: excel vba date

无法弄清楚如何水平填充excel单元格...我在单元格“ B2”中具有“开始日期”,在单元格B3中具有“结束日期”。

当我输入2个日期时,我只用该范围内的日期填充所有单元格,但垂直填充它们。我想水平填充

这是一张图片

enter image description here

“ Data inizio”->开始日期 “数据很好”->结束日期

这是我到目前为止所做的事情

    Sub FillCal()

    ' Disable screen updates (such as warnings, etc.)
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim StartD As Date, EndD As Date
    Dim prova As Integer
    Dim rngMerge As Range, rngCell As Range, mergeVal As Range
    Dim i As Integer
    Dim wks As Worksheet
    StartD = Foglio1.Cells(2, 2)
    EndD = Foglio1.Cells(3, 2)
    For Column = 1 To EndD - StartD
        Cells(4, Column) = StartD + Column - 1
        prova = Application.WorksheetFunction.WeekNum(StartD + Column - 1, 2)
        Cells(5, Column).NumberFormat = prova
        Cells(5, Column).Value = prova
    Next Column

    Set wks = ThisWorkbook.Sheets("Foglio1") ' Change Sheet1 to your worksheet

    i = wks.Range("E1").End(xlDown).Row
    Set rngMerge = wks.Range("E1:E" & i) ' Find last row in column A

    With wks
    ' Loop through Column A
checkAgain:
    For Each rngCell In rngMerge
        ' If Cell value is equal to the cell value below and the cell is not empty then
        If rngCell.Value = rngCell.Offset(1, 0).Value And IsEmpty(rngCell) = False Then
            ' Define the range to be merged
            ' Be aware that warnings telling you that the 2 cells contain 2 differen values will be ignored
            ' If you have 2 different sums in column C, then it will use the first of those
            '  Set mergeVal = wks.Range(rngCell.Offset(0, 2), rngCell.Offset(1, 2))
            '    With mergeVal
            '    .Merge
            '    .HorizontalAlignment = xlCenter
            '    .VerticalAlignment = xlCenter
            '    End With
            Range(rngCell, rngCell.Offset(1, 0)).Merge
            rngCell.VerticalAlignment = xlCenter
            GoTo checkAgain
        End If

    Next
    End With

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

Output

2 个答案:

答案 0 :(得分:1)

那呢?

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim StartD As Date, EndD As Date
Dim prova As Integer
Dim rngMerge As Range, rngCell As Range, mergeVal As Range
Dim i As Integer
Dim wks As Worksheet
StartD = Foglio1.Cells(2, 2)
EndD = Foglio1.Cells(3, 2)
For Column = 1 To EndD - StartD
    Cells(4, Column) = StartD + Column - 1
    prova = Application.WorksheetFunction.WeekNum(StartD + Column - 1, 2)
    Cells(5, Column).NumberFormat = prova
    Cells(5, Column).Value = prova
Next Column

注意

Cells(Column,4)将浏览第4列的行

Cells(4,Column)将浏览第4行的列

答案 1 :(得分:0)

尝试使用公式获取日期和星期。但是,单元格的合并将需要一个循环。

Sub TEST_2A()
Dim dIni As Double, dEnd As Double

    Application.EnableEvents = False
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    With ThisWorkbook.Worksheets("DATA")    'change as required
        dIni = .Cells(2, 2)
        dEnd = .Cells(3, 2)

        With .Cells(3, 4).Resize(2, 1 + dEnd - dIni)
            .Cells(1).Value2 = dIni
            .Cells(2).Resize(1, -1 + .Columns.Count).FormulaR1C1 = "=+RC[-1]+1"
            .Rows(1).NumberFormat = "dd\/mm\/yyyy" 'change as required
            .Rows(2).FormulaR1C1 = "=WEEKNUM(R[-1]C,2)"
            .Value2 = .Value2

    End With: End With

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic

    End Sub