0

I'm having an issue trying to integrate a condition inside a Loop. This condition populates the first 5 cells in a Row (Weekdays) and then skips the next 2 (the Weekend). This goes on depending on the number of the Days. This happens while reading a various number of rows, with different Days, Values and Start Dates (outer loop? ) The Data should be populated depending on said Start Date. To solve thi,s I compare the difference in Start Dates from the current data with the previous one, getting the difference in number of Days. Then offset the new Data for those Days in the columns. Loop all Rows and Read Start My issue is getting to skip the 6th and 7th column/value part. Thanks in advance. I tried a related Solution posted from "karma" which works for one single row. But get some errors when trying to loop through all the data (reading all my Data). The error in this case, only one Row gets populated until the very last Column in Excel, ignoring the number of Days as condition, and not itirating through the next rows. And there I'd still need to populate the information depending on the Start Date. error-1 error-2 error-3

For the first Code missing the skip part :

Sub xt4_LoopDate()
Dim ii1, jj1, osi1, osj1 As Integer
Dim in1Days As Integer, in2Value As Integer
Dim xiStart, in3Datum As Date
Dim xiDiff As Long
Dim jbound As Integer
Range("F3:XFD7").ClearContents

osi1 = 2  'Row
osj1 = 5  'Column
jbound = 5  'Upper Bound
xiStart = Range("C3") 'Start Date

For ii1 = 1 To jbound
    in1Days = Range("A" & ii1 + osi1)   'Get Dauer
    in2Value = Range("B" & ii1 + osi1)   'Get Leistung
    in3Datum = Range("C" & ii1 + osi1)   'Get Start
    xiDiff = DateDiff("D", xiStart, in3Datum)  'DifferenceDates

    For jj1 = 1 To in1Days  'Loop the Length
        Cells(ii1 + osi1, jj1 + osj1 + xiDiff) = in2Value
    Next jj1
Next ii1
End Sub

For the Second Code with the Loop Errors, missing the condition to start on a certain date :

Sub LSATURDAY_V1()
Dim in1Days As Integer, in2Value As Integer
Dim osii1, osjj1 As Integer
Dim ii1 As Integer, NumberRows As Integer
Dim i As Integer, f As Integer, s As Integer, oFill As Range

Range("F3:XFD6").ClearContents

osii1 = 2  'Offset Row
osjj1 = 5  'Offset Column
f = 5: s = 2

NumberRows = 2
For ii1 = 1 To NumberRows
    in1Days = Range("A" & ii1 + osii1)
    in2Value = Range("B" & ii1 + osii1)
    Debug.Print "D&L -> "; in1Days & "  "; in2Value

Set oFill = Range("F" & ii1 + osii1)
    Do
        For i = 1 To f
            oFill.Value = in2Value
            Set oFill = oFill.Offset(0, 1)
            'works for one Row -> Range("N3:XFD3")
            If Application.CountA(Range("F" & ii1 & ":XFD" & ii1)) = in1Days Then Exit Sub
        Next i
        Set oFill = oFill.Offset(0, s)
    Loop
Next ii1
End Sub
jjjhhh
  • 15
  • 5
  • 1
    The problem to me is : what you say is as if _any date of any month of any year is always the first day of the week_. In your image, cell F1 is 1 ---> is this what you mean the first day of the week? Then cell F2 is 03.4, assuming that is 3-Apr-2024 so I googled it, 3-Apr-2024 is Wednesday. So, Wednesday is not the first day of the week, well at least in my country, the first day of the week is Monday, so our weekend is always Saturday and Sunday (the 6th and the 7th). So to me the logical data is : if cell F2 3-Apr-2024 Wednesday, then cell F1 value should be 3, not 1. (continue) – karma Apr 12 '23 at 18:40
  • 1
    Another example, say cell F2 value is 1-Dec-2023. I google it, it's Friday. So in my mind cell F1 should be 5 (the fifth day of the week). But then which more confusing me, your row 1 value keep on going from 1 to whatever - which to my mind, if it's to represent each day of the week, then it should repeat to 1 after it reach 7. For example : cell F2 value 1-Dec-2023 Friday, then F1=5, G1=6 (the column to skip because it's saturday weekend), H1=7 (the column to skip as it's Sunday weekend). Then in i1 the value back to 1, and so on. Please CMIIW. – karma Apr 12 '23 at 18:40
  • I assume that the column you give grey color (L, S, Z, AG) is the column to skip. Now IF C3 value is 9-Apr-2024, since 9-Apr-2024 is in cell L2 the grey column - then what to expect ? don't fill L3. fill the next "free" column, in this case M3 as the starting cell ? Please CMIIW. – karma Apr 12 '23 at 19:09

1 Answers1

1

Again, I'm not sure if I understand you correctly.
Anyway, the image below is the one I thought as your expected result.

Before running the sub:
enter image description here

After running the sub:
enter image description here

Sub test()
Dim in1Days As Integer, in2Value As Integer, i As Integer, oFill As Range
Dim dt As Date

With ActiveSheet
.Range("F3:XFD7").ClearContents
Set rg = .Range("A3", .Range("A3").End(xlDown))
End With

i = 6
Do
    txt = txt & "," & i & "," & i + 1
    i = i + 7
Loop Until i >= 50

For Each cell In rg
    in1Days = cell.Value
    in2Value = cell.Offset(0, 1).Value
    dt = cell.Offset(0, 2).Value

    Set c = ActiveSheet.Rows(2).Find(dt)
    If c Is Nothing Then Exit Sub

    Set oFill = Cells(cell.Row, c.Column)
    Set rgCnt = Range(oFill, oFill.End(xlToRight))
    i = c.Offset(-1, 0)

    Do
        If InStr(txt, "," & i) = False Then oFill.Value = in2Value
        Set oFill = oFill.Offset(0, 1): i = i + 1
    Loop Until Application.CountA(rgCnt) = in1Days
Next

End Sub

It set the rg variable as the range where in1Days value are. (column A)

it use 6 as the first restriction number, then it loop to create all restriction number as txt variable. So after the loop finish, the txt value are the numbers in yellow of row 1.

Then it loop to each cell in rg, create the needed variable. (in1Days, in2Value and dt)
It try to find where is the cell in row 2 which has dt value as c variable. If not found it just exit the sub, if found then it create i which value is from c.offset(-1,0), set the oFill as the starting cell to be filled, and set the rgCnt as the range to be countA.

Then it do-loop, if the i is not instring of the txt, then it put in2Value into oFill. Then it set oFill (0,1) and make the i = i+1. It loop until the countA of rgCnt = in1Days.

Please note that it will put the in2Value to the "allowed" column if the found date is in the yellow column. For example is in the last data, 8-Apr-2024 is in yellow (cell K1), 9-Apr also yellow (cell L1), so it start to fill the value in M7, the 10-Apr-2024.

The value in row 2 are dates formatted as dd.m, so it's not a string "03.4", but "3-Apr-2024".

Step run the code, so you know what happen.


Another example
In the data, row 1 blank (so there is no number). Please ignore the number and the yellow color, as it used just to show that the yellow is the week end and the number is the nth day of the week based on the date in row 2.

enter image description here

Expected result:
enter image description here

if the dt (the looped cell.offset(0,3) value) fall on the 6th or 7th day of the week, then it will start to the next 1st day of the week. In the example image above, the first dt is 7-Apr-2024 falls on the 7th day of the week, so it start to fill on cell K3.

Sub test2()
Dim in1Days As Integer, in2Value As Integer, i As Integer, oFill As Range
Dim dt As Date

With ActiveSheet
.Range("F3:XFD7").ClearContents
Set rg = .Range("A3", .Range("A3").End(xlDown))
End With

For Each cell In rg
    in1Days = cell.Value
    in2Value = cell.Offset(0, 1).Value
    dt = cell.Offset(0, 2).Value

    Set c = ActiveSheet.Rows(2).Find(dt)
    If c Is Nothing Then Exit Sub

    Set oFill = Cells(cell.Row, c.Column)
    Set rgCnt = Range(oFill, oFill.End(xlToRight))
    i = Weekday(dt, vbMonday)

    Do
        If i < 6 Then oFill.Value = in2Value
        Set oFill = oFill.Offset(0, 1)
        If i = 7 Then i = 1 Else i = i + 1
    Loop Until Application.CountA(rgCnt) = in1Days
Next

End Sub

The skipping cell take place by using weekday funtion of the looped cell.offset(0,2) value, the dt variable. So the i will be the nth day of the week from the given date.

In the loop, as long as i < 6 it will write to oFill.
Then it offset 0,1 the oFill, and if i = 7 it reset i to 1 else it add i + 1.

karma
  • 1,999
  • 1
  • 10
  • 14
  • I get where the confusion comes from. (1.)-There’s a mistake on my screenshot. I intended originally to begin on the 03.04.2023, which is a Monday. This was only to simplify the first steps while working on the code. (2.)- Both your analogies with 2024 are correct. If F2 is 03.04.24, cell F1 value should be 3, not 1. (3.)- And you’re right again. on the first row I want to represent the number of days of the week 1-7. And start again 1-7 every week. – jjjhhh Apr 12 '23 at 20:44
  • (4.)- the grey columns are to be skipped. Now my question is, how to populate each row while skipping those grey columns (Saturday&Sunday / 6&7) but starting to fill the range of the cells depending on a given Date. (it could be the year 2023,2024….2027) (5.)- The start of a month doesn’t have to be the first day of the week. – jjjhhh Apr 12 '23 at 20:46
  • (6.)- and I’d like to do it while looping/reading all the rows with data. In my first screenshot *Range (“A3:C7”)* …as an example – jjjhhh Apr 12 '23 at 20:48
  • And thanks again for you patience and your time. – jjjhhh Apr 12 '23 at 20:52
  • Awesome, I'll check your suggestion tomorrow early! – jjjhhh Apr 12 '23 at 21:55
  • A question to your Code regarding the restriction as txt. If I have more than 50 values on Row 1 (and Row 2 also), lets say from 1 to 1500. I want to cover something around 4 years (1500/365). I have a problem when I update the "Loop Until >=1500" line. The Data from Range A doesn't start anymore on the defined StartDate. Instead, all values are populated/filled a couple of columns further. The bigger the Value in "loop Until i> ### " is, the bigger the offset difference. It works fine around until "Loop Until i >= 80" – jjjhhh Apr 13 '23 at 12:47
  • 1
    AFAIK, it's because there is a limitation for string variable, I think max 255 chars. _The bigger the Value in "loop Until i> ### " is, the bigger the offset difference_, because it seems that I coded wrongly. I should code it `If InStr(txt, "," & i & ",")`. Anyway, if you say that you will change the data in row 1 with repeating number from 1 to 7 to show as the nth day of the week based on the date in row 2, then you don't need to create txt. Please have a look to my updated answer. – karma Apr 14 '23 at 03:48
  • Amazing! Thanks for the last updates, the explanation and once again for your time! Really appreciate it. Have a nice weekend – jjjhhh Apr 14 '23 at 13:06