marvellosity123
marvellosity123

Reputation: 39

find month names withing for loop

It returns only saturday, for the dates it was simple because I only needed to add a plus 1 but how to i get it to loop from 1 to 7? Also how about for the months names as well

Sub netprofit()

Dim dates As Date
Dim count As Integer

Dim olweekdayname As String

olweekdayname = WeekdayName(Weekday(dates))

dates = DateSerial(2014, 9, 1)
For count = 2 To 366

Select Case Weekday(dates)

Case 1, 7

    Range("B" & count) = 0

    Range("C" & count) = 0

    Range("D" & count) = 0
    Range("E" & count) = olweekdayname


Case Else
    Range("C" & count) = Int((100 - 0 + 1) * Rnd + 0)
    Range("D" & count) = Range("B" & count) - Range("C" & count)
    Range("A" & count) = dates
    Range("E" & count) = olweekdayname

End Select
dates = dates + 1

Next
End Sub

Upvotes: 2

Views: 177

Answers (1)

Vasily
Vasily

Reputation: 5782

this line olweekdayname = WeekdayName(Weekday(dates)) must be inside of the loop, otherwise olweekdayname will not be changed, so your updated code is below, tested, works fine.

Sub netprofit()
    Dim dates As Date
    Dim count As Integer
    dates = DateSerial(2014, 9, 1)
    For count = 2 To 366
        Select Case Weekday(dates)
        Case 1, 7
            Range("B" & count) = 0
            Range("C" & count) = 0
            Range("D" & count) = 0
            Range("E" & count) = WeekdayName(Weekday(dates))
            Range("F" & count) = MonthName(Month(dates))
        Case Else
            Range("C" & count) = Int((100 - 0 + 1) * Rnd + 0)
            Range("D" & count) = Range("B" & count) - Range("C" & count)
            Range("A" & count) = dates
            Range("E" & count) = WeekdayName(Weekday(dates))
            Range("F" & count) = MonthName(Month(dates))
        End Select
        dates = dates + 1
    Next
End Sub

Upvotes: 2

Related Questions