Home » excel » excel – How to update date n times for n number of rows?

excel – How to update date n times for n number of rows?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m currently working on a 16 000 rows excel file

  • Column B is the date I want to update
  • Column H is the starting date
  • Column I is the ending date
  • Column K is the number of times date need to be updated (updated on n rows)

The idea is to display all dates from starting date to ending date in column B (one date per column).

You’ll find below my current codes. I’m a beginner, it probably includes a lot of mistakes. Can you please provide me with some help, I keep getting minor error: 91, 1004, 5…

Sub Dates()
    Dim i As Long
    Dim k As Long
    Dim MyDate As Long
    Dim EndDate As Long
    Dim EndRowA As Long
    Dim EndRowB As Long
    Dim EndRowH As Long
    Dim StartDate As Long
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")

    StartDate = ws.Cells(ws.Rows.Count, "H").Value
    MyDate = ws.Cells(ws.Rows.Count, "B").Value
    EndDate = ws.Cells(ws.Rows.Count, "I").Value
    EndRowA = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    EndRowB = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row
    EndRowH = ws.Cells(ws.Rows.Count, 8).End(xlUp).Row

    Do While (i <= EndRowH And i <= EndRowA And i <= EndRowB)
        If ws.Cells(i, "H").Value = ws.Cells(i, "I").Value Then
            GoTo Continue
        ElseIf ws.Cells(i, "H").Value = ws.Cells(i, "I").Value Then
            For k = 1 To ws.Cells(i, k).Value
                ws.Cells(i + 1, "B").Select
                ws.Cells(i, "B").Value = ws.Cells(i - 1, "H").Value + 1
            Exit For
Continue:
        Next k
    End If
Loop
End Sub

Main columns in the spreadsheet should look as follows:

Type    Date     Start date   End Date   #

A   01/01/2018  01/01/2018  01/10/2018  10
A   01/02/2018  01/01/2018  01/10/2018  10
A   01/03/2018  01/01/2018  01/10/2018  10
A   01/04/2018  01/01/2018  01/10/2018  10
A   01/05/2018  01/01/2018  01/10/2018  10
A   01/06/2018  01/01/2018  01/10/2018  10
A   01/07/2018  01/01/2018  01/10/2018  10
A   01/08/2018  01/01/2018  01/10/2018  10
A   01/09/2018  01/01/2018  01/10/2018  10  
A   01/10/2018  01/01/2018  01/10/2018  10
B   02/06/2018  02/06/2018  02/10/2018  5
B   02/07/2018  02/06/2018  02/10/2018  5
B   02/08/2018  02/06/2018  02/10/2018  5
B   02/09/2018  02/06/2018  02/10/2018  5
B   02/10/2018  02/06/2018  02/10/2018  5

I thank you in advance

How to&Answers:

Just try this code and you will see the error you are making:

Sub Dates()
    Dim ws As Worksheet
    Dim i As Long
    Dim k As Long
    StartDate = ws.Cells(ws.Rows.Count, "H").Value
    Date = ws.Cells(ws.Rows.Count, "B").Value
    EndDate = ws.Cells(ws.Rows.Count, "I").Value
    EndRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row

    MsgBox ws.Cells(ws.Rows.Count, "H").Address
    MsgBox ws.Cells(ws.Rows.Count, "B").Address

End Sub

I guess that you have no value at the last row of column “H”. This is $H$1048576.
In general, the way you pass EndRow with End(xlUp) is a good option, try to mimic it everywhere.


Some improvement ideas:

  • write Option Explicit on the top of the module. It forces you to declare variables and makes a quick check before execution.
  • you should assign ws to a worksheet. The easiest way is Set ws = Worksheets("NameOfTheWorksheet")
  • the loops you are doing should end on the expected way. E.g., for every Do While there should be a Loop written. "Do While" "Loop" and "While" "Wend" Loop. What's the difference?
  • GoTo statement in VBA is good to be used only in the sentence On Error GoTo ErrHandler. In general, if you remove GoTo Continue from your code, it will still avoid the second loop.