Option Explicit
Sub jkl()
'------------------ Declarations --------------------------
Const lstdtemsg = "The number of available dates (counted from the starting date)" & _
vbNewLine & "is outside the acceptable range"
Const prmpt = "Enter the start date and number of days (data separated by a semicolon)"
Const ttl = "Enter data" & " in format: dd/mm/yyyy;Number of days"
Const dflt = "04/02/2021;4"
Const cpc = "Reserve"
Dim c As Long, n As Long, nd As Long, r As Long, rw As Long
Dim cls, rws, sdat, vdat
'------------------ Input data ----------------------------
sdat = Replace(InputBox(prmpt, ttl, dflt), " ", vbNullString, 1, -1, 1)
'------------------ Verification of data ------------------
sdat = Split(sdat, ";", -1, 1)
If UBound(sdat) <> 1 Then MsgBox "Incorrect data scheme - The End": Exit Sub
If Not IsNumeric(sdat(1)) Then MsgBox "Incorrect value for the number of days - The End": Exit Sub
If Not sdat(0) Like "##/##/####" Then MsgBox "Incorrect date scheme - The End": Exit Sub
c = CLng(Left(sdat(0), 2)): n = CLng(Mid(sdat(0), 4, 2)): r = CLng(Right(sdat(0), 4))
If (c < 1 Or c > 31) Or (n < 1 Or n > 12) Or (r < 1900 Or r > 2100) Then
MsgBox "Incorrect date elements - The End": Exit Sub
End If
If Not IsDate(sdat(0)) Then MsgBox "Incorrect date - The End": Exit Sub
'------------------ Creating a data set -------------------
nd = Val(sdat(1))
sdat = DateSerial(r, n, c)
c = 0: n = 0: r = 2
On Error Resume Next
With Sheets("Sheet1").Range("A1").CurrentRegion
rw = .Columns(1).Find(cpc, , xlValues, xlPart, xlByColumns).Row
If Err.Number <> 0 Then MsgBox "No reference point - The End": Exit Sub
c = .Rows(1).Find(CStr(sdat), , xlValues, xlWhole, xlByRows).Column
If Err.Number <> 0 Then MsgBox "No date you are looking for - The End": Exit Sub
If c + (nd - 1) > .Columns.Count Then MsgBox lstdtemsg: Exit Sub
sdat = Empty: sdat = .Resize(rw - 1, .Columns.Count).Value
End With
On Error GoTo 0
rws = Evaluate("Row(1:" & rw - 1 & ")")
cls = "{1," & c
For n = c + 1 To c + (nd - 1)
cls = cls & ("," & n)
Next
cls = Evaluate(cls & "}")
vdat = Application.Index(sdat, rws, cls)
cls = Empty: rws = Empty: sdat = Empty
c = 0: rw = rw - r
ReDim sdat(1 To nd * rw, 1 To 3)
For n = 1 To nd
For r = 1 To rw
c = c + 1
sdat(c, 1) = Format(vdat(1, n + 1), "mm/dd/yyyy")
sdat(c, 2) = vdat(r + 1, 1)
sdat(c, 3) = vdat(r + 1, n + 1)
Next
Next
'------------------ Output data ---------------------------
With Sheets("Sheet3")
With .Range("A1")
.CurrentRegion.ClearContents
.Resize(1, 3).Value = Array("Date", "Number", "Value")
.Offset(1, 0).Resize(c, 3).Value = sdat: sdat = Empty
.CurrentRegion.EntireColumn.AutoFit
End With
.Select
End With
End Sub
Ps:
Bookmarks