Populating a range of cells in row with dates of the current month VBA, then save data and clear sheet at the first of the next month

StackOverflow https://stackoverflow.com/questions/19821586

  •  04-07-2022
  •  | 
  •  

문제

I am looking for a way to write VBA script that will automatically populate sheet17.range("f14:aj14") with dates of whatever the current month is. The date format needs to be mm/d/yyyy and I need to be able to use "FoundCell" method to find "Date" using this range. I found this code and adapted to my needs but it populates the next column to the right starting with the cell on the next row. I have tried changing up the "Offset" numbers and cannot get it to populate my range of cells, and as I am a novice with VBA, I have a macro to copy the sheet to new sheet, save new sheet and then clear contents of range(f15:AN73) of the original, I don't know the code for making it happen automatically at the first of each month at 12:00am. Here is the code I have for date population:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim Sht As Worksheet
    Dim intDaysInMonth As Integer
    Dim i As Integer

    Set Sht = Worksheets("MAR") 'change to suit
    Target = Sht.Range("f14:aj14")
    intDaysInMonth = Day(DateSerial(Year(Now()), Month(Now()) + 1, 0))

    Target.Resize(intDaysInMonth, 1).ClearContents
    For i = 1 To intDaysInMonth
        Target.Offset(i - 0, 1) = DateSerial(Year(Now()), Month(Now()), i)
    Next i
    Set Sht = Nothing
End Sub

I just need the dates to populate my sheet17.range("f14:aj14") instead of the next column to the right beginning at 1 cell down, and then the code to automatically save sheet to new sheet and clear the contents of the original sheet.range("f15:an73"). Any help would be greatly appreciated! Thank you in advance.

Michele

도움이 되었습니까?

해결책

Here's the answer to the date fill part of your question. And, you need to place this in a regular module, NOT in a worksheet module.

Sub myDates()

Dim Sht As Worksheet
Dim intDaysInMonth As Integer
Dim i As Integer
Dim Target As Range

Set Sht = Worksheets("MAR")   'change to suit
intDaysInMonth = Day(DateSerial(Year(Now()), Month(Now()) + 1, 0))

Set Target = Sht.Range("f14").Resize(, intDaysInMonth) 'set target to days count
Target(1).Resize(, 31).ClearContents 'clear all previous

For i = 1 To intDaysInMonth
    Target(i).Value = DateSerial(Year(Now()), Month(Now()), i)
Next i
Set Sht = Nothing

End Sub

But to make this more useful, you should call this from another sub, with the worksheet name as an argument, so you can access the correct month's worksheet. Then it would look like this:

Sub myDates(ShtName As String)

Dim Sht As Worksheet
Dim intDaysInMonth As Integer
Dim i As Integer
Dim Target As Range

Set Sht = Worksheets(ShtName)   'change to suit
intDaysInMonth = Day(DateSerial(Year(Now()), Month(Now()) + 1, 0))

Set Target = Sht.Range("f14").Resize(, intDaysInMonth) 'set target to days count
Target(1).Resize(, 31).ClearContents 'clear all previous

For i = 1 To intDaysInMonth
    Target(i).Value = DateSerial(Year(Now()), Month(Now()), i)
Next i
Set Sht = Nothing

End Sub
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top