Come creare un calendario nel tuo foglio di lavoro Excel con lo script VBA

Condividi ora:

Excel ha una vasta gamma di utilizzo. In questo articolo, ti mostreremo come creare un calendario in un foglio di lavoro utilizzando una macro.

Fatta eccezione per l'archiviazione e l'analisi dei dati nel tuo Excel, puoi anche usarlo per completare altre attività. Ad esempio, è possibile raccogliere informazioni utilizzando Excel oppure annotare la pianificazione nel foglio di lavoro. Oggi abbiamo trovato un nuovo utilizzo. Sei in grado di creare un calendario in un foglio di lavoro. Puoi vedere l'effetto nell'immagine qui sotto.Calendario

Puoi inserire alcune attività in questo calendario. E la funzione è la stessa di un memorandum. Utilizzando Excel, tali attività saranno più chiare. Ora segui i passaggi nella parte seguente e guarda come funziona.

Crea un calendario

  1. Premere il pulsante "Alt + F11" sulla tastiera per aprire il foglio di lavoro.
  2. E quindi inserisci un nuovo modulo nell'editor di Visual Basic.
  3. Ora copiando i seguenti codici nel nuovo modulo:
Sub Create_Monthly_Calender()
    Dim firstweekday As Integer, EndDay As Integer, _
    FirstWeekColumnIndex As Integer, AssignmentDate As Integer, _
    FirstCountNumber As Integer, SecondCountNumber As Integer, _
    LastDay As Range, objRange As Range, RowIndexofLastday As Integer, FirstCountforTargetRange As Integer, SecondCountforTargetRange As Integer

    firstday = InputBox("Input the year, month and the first day with this format: year/month/day")
    If firstday = "" Then Exit Sub


    Range("A1:G1").Merge
    Range("A1") = Year(firstday) & "." & Month(firstday)
    Range("A2") = "Sunday"
    Range("A2").AutoFill Destination:=Range("A2:G2"), Type:=xlFillDefault
    firstweekday = Application.WorksheetFunction.Weekday(firstday)
    Cells(3, firstweekday) = 1

    Select Case Month(firstday)
        Case 1, 3, 5, 7, 8, 10, 12
            EndDay = 31
        Case 4, 6, 9, 11
            EndDay = 30
        Case 2
            If (Year(firstday) Mod 4) = 0 And (Year(firstday) Mod 100) <> 0 Or ((Year(firstday) Mod 400) = 0) Then
                EndDay = 29
            Else
                EndDay = 28
            End If
    End Select

    For FirstWeekColumnIndex = 1 To (7 - firstweekday)
        Cells(3, firstweekday).Offset(0, FirstWeekColumnIndex) = Cells(3, firstweekday).Offset(0, FirstWeekColumnIndex - 1) + 1
    Next FirstWeekColumnIndex

    AssignmentDate = Range("G3") + 1
    For FirstCountNumber = 2 To 10 Step 2
        For SecondCountNumber = 0 To 6
            Cells(3, firstweekday).Offset(FirstCountNumber, 1 - firstweekday + SecondCountNumber) = AssignmentDate
            AssignmentDate = AssignmentDate + 1
            If Cells(3, firstweekday).Offset(FirstCountNumber, 1 - firstweekday + SecondCountNumber) = EndDay Then
                Exit For
            End If
        Next SecondCountNumber
        If Cells(3, firstweekday).Offset(FirstCountNumber, 1 - firstweekday + SecondCountNumber) = EndDay Then
            Exit For
        End If
    Next FirstCountNumber

    ’set format for the range
    With Range("A1")
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .Font.Size = 16
        .Font.Bold = True
        .Interior.Color = RGB(196, 202, 201)
    End With

    With Range("A2:G2")
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .Font.Bold = True
    End With

    For Each LastDay In ActiveSheet.UsedRange
        If LastDay = EndDay Then
            RowIndexofLastday = LastDay.Row
        End If
    Next

    For FirstCountforTargetRange = RowIndexofLastday To 3 Step -2
            With Range("A" & FirstCountforTargetRange, "G" & FirstCountforTargetRange)
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlCenter
                .RowHeight = 20
            End With
    Next FirstCountforTargetRange

        For SecondCountforTargetRange = RowIndexofLastday + 1 To 4 Step -2
            With Range("A" & SecondCountforTargetRange, "G" & SecondCountforTargetRange)
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlCenter
                .Font.Bold = True
                .RowHeight = 50
                .ColumnWidth = 12
            End With
    Next SecondCountforTargetRange

    Set objRange = Range("A1", "G" & (RowIndexofLastday + 1))
    With objRange.Borders
        .Color = vbBlack
        .Weight = xlThin
        .LineStyle = xlContinuous
    End With

    ActiveWindow.DisplayGridlines = False
    Cells(3, firstweekday).Offset(1, 0).Select
End Sub

Quindi è una macro molto solitaria. Ma puoi usarlo direttamente. E non è necessario modificarlo.

  1. Quindi premere il pulsante "F5" sulla tastiera.
  2. Nella finestra pop-up, inserisci la data del primo giorno con il formato “Anno/Mese/Giorno”. Il "Giorno" può essere qualsiasi giorno di questo mese, qui inseriremo "1".Data di inserimento
  3. E quindi fare clic sul pulsante "OK". Successivamente tornerai all'editor. Per controllare il risultato, puoi tornare al foglio di lavoro.

Il calendario è già apparso nel foglio di lavoro. E il cursore sarà posizionato nella cella vuota del primo giorno.Risultato

D'altra parte, se non sei soddisfatto del formato delle celle, puoi anche modificare i codici in base alle tue preferenze. Con questo calendario, migliorerai sicuramente la tua efficienza lavorativa.

Excel non è meno incline al fallimento

Se usi Excel frequentemente, non sarà difficile per te scoprire che Excel si corromperà sempre. Most del tempo, esci da Excel e poi restart può risolvere il problema. Tuttavia, a volte incontrerai una grave corruzione di Excel. E in questo momento, puoi utilizzare il nostro potente strumento per riparare l'errore di dati Excel xlsx. Utilizzando questo strumento, almost tutti gli errori possono essere risolti facilmente.

Introduzione dell'autore:

Anna Ma è un'esperta di recupero dati in DataNumen, Inc., che è il leader mondiale nelle tecnologie di recupero dati, tra cui riparare il file docx di Word e prodotti software di riparazione di Outlook. Per maggiori informazioni visita www.datanumen.com

Condividi ora:

I commenti sono chiusi.