【问题标题】:Start Date and End Date fill horizontal开始日期和结束日期填充水平
【发布时间】:2019-09-03 14:43:43
【问题描述】:

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

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

这是一张照片

"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

【问题讨论】:

  • 你能发布一个预期输出的例子吗?
  • 是的,当然对不起我忘了
  • @MiteNikolov 我提出了Answer,但粘贴您的完整代码将帮助我改进它..
  • 我现在肯定会过去的! @Dorian
  • @MiteNikolov 我建议对您的帖子进行编辑,让 issue 出现,这样可以帮助其他用户;)。最诚挚的问候。

标签: excel vba date


【解决方案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行的列

【讨论】:

  • 你是法师!我觉得很愚蠢......非常感谢!喜欢 StackOverFlow
  • @MiteNikolov 很高兴我能帮助你!如果问题解决了,可以请你接受我的回答吗?
【解决方案2】:

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

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

【讨论】:

  • 是的!这也有效!但在我原来的帖子中,还有天数下的周数(合并)。你能更新你的帖子吗?
  • 是的,没有循环并且只使用公式是个好主意!
猜你喜欢
  • 1970-01-01
  • 2019-12-10
  • 2012-08-19
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多