Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

populate message on form based on missed current month

0

Hello,

I search for way when there is missed month before current month, then will not copy  for current month  before  copy the prevoius month but first of all should informe me by message" there is missed month NOVE-24  should copy first of all" after is gone the message then will copy data for the prevoius month NOVE-24 , if there is no missed month before current month , then will not do anything.

example : inside sheet I have OCTOBER month and the current month is DECEMBER,so the code will copy data based on current month is DECEMBER but there is missed month  for prevouis month is NOVEMEBER . in this case  will show message and copy. as to current month DECEMBER will copy based on condition inside code  for days 27,28,29,30,31 .

Private Sub CommandButton1_Click()
    Dim i As Long
    Dim lastrow As Long
    Dim sheet2 As Worksheet
    Dim currentMonth As String
    Dim copyDate As Date
    Dim remainingDays As Long
    Dim headersWritten As Boolean

    Set sheet2 = ThisWorkbook.Sheets("FORM") ' Replace with the correct sheet name if necessary

    ' lastrow line moved down

    ' Check if headers are already written
    headersWritten = (sheet2.Cells(1, 1).Value = "MONTH" And sheet2.Cells(1, 2).Value = "ITEM" _
                      And sheet2.Cells(1, 3).Value = "NAME" And sheet2.Cells(1, 4).Value = "BALANCE")

    ' Write headers if not already present
    If Not headersWritten Then
        sheet2.Cells(1, 1).Value = "MONTH"
        sheet2.Cells(1, 2).Value = "ITEM"
        sheet2.Cells(1, 3).Value = "NAME"
        sheet2.Cells(1, 4).Value = "BALANCE"
    End If

    ' Determine the last row
    lastrow = sheet2.Cells(sheet2.Rows.Count, 1).End(xlUp).Row

    ' Check if this month has already been copied
    currentMonth = Format(Date, "mmm-yy")
    Dim alreadyCopied As Boolean
    alreadyCopied = False
    For i = 2 To lastrow
        If sheet2.Cells(i, 1).Value = currentMonth Then
            alreadyCopied = True
            Exit For
        End If
    Next i

    If alreadyCopied Then
        MsgBox "The data has already been copied for this month!", vbExclamation
        Exit Sub
    End If

    ' Check if today is within the allowed days
    copyDate = Date
    If Day(copyDate) < 27 Then
        remainingDays = 27 - Day(copyDate)
        MsgBox "Warning: You need to wait " & remainingDays & " more days to copy the data.", vbExclamation
        Exit Sub
    ElseIf Day(copyDate) >= 27 And Day(copyDate) <= 31 Then
        ' Copy data from ListBox1 to Sheet2
        With Me.ListBox1
            ' write headers if not already done
            If headersWritten Then
                sheet2.Cells(lastrow + 1, 1).Value = "MONTH"
                sheet2.Cells(lastrow + 1, 2).Value = "ITEM"
                sheet2.Cells(lastrow + 1, 3).Value = "NAME"
                sheet2.Cells(lastrow + 1, 4).Value = "BALANCE"
                Else
                ' adjust lastrow to avoid gap for first data set
                lastrow = 0
            End If
            ' write month data
            For i = 1 To .ListCount - 1
                sheet2.Cells(lastrow + i + 1, 1).Value = currentMonth
                sheet2.Cells(lastrow + i + 1, 2).Value = .List(i, 0)
                sheet2.Cells(lastrow + i + 1, 3).Value = .List(i, 1)
                sheet2.Cells(lastrow + i + 1, 4).Value = .List(i, 2)
                ' lastrow = lastrow + 1
            Next i
            ' refresh the screen
            Application.ScreenUpdating = True
        End With

        MsgBox "Data successfully copied for " & currentMonth, vbInformation
    Else
        MsgBox "Warning: You can only copy data between the 27th and 31st of the month.", vbExclamation
        Exit Sub
    End If
End Sub

I hope finding help  for this requirements.

Answer
Discuss

Discussion

Hi again Ali.

Please clarify- if November is NOT found, should the code give the warning message but copy the data for December anyway?
John_Ru (rep: 6607) Dec 13, '24 at 4:14 am
but copy the data for December anyway?
should copy NOVEMBER and DECEMBER , but as you know based on original code the DECEMBER is current month then  will copy until  reach days(27,28,29,30,31) so date today is 13/12/2024 in this case will not copy DECEMBER  . will copy NOVEMBER  because is missed .
Ali M (rep: 36) Dec 13, '24 at 5:26 am
I have to agree with John that this is quite confusing. I understand that if November data is not found then don't copy December. With your sample file being very limited in its contents, I am confused as to what data you want to copy and copied to where. It seems you have over complicated the process, but you must have your reasons.
Sorry, but I won't be contributing an answer.
WillieD24 (rep: 657) Dec 13, '24 at 5:56 pm
Hi willie
why you're confused?!
the code will copy data from form to sheet for each current month based on 27,28,29,30,31 otherwise will not copy until reach to one of theses for days
but for some reason the user forget  to copy some data for current month. after leave the prevouis month and enter in new month so should copy data for  missed month . as to data for current month will depends on days 27,28,29,30,31 when reach to theses days then will copydata for  the current month.
the user suppose copying data for current month but if the user forgot and enter new current month, then should copy data for missed month .
when you asked where copy the missed month ?
as the original code copy to bottom and based on the code when try copy again and the month is lready existed will inform you the month is existed without repeat copy again.
the only thing what I look for  the missed month should copy when there is missed month before current month .
Ali M (rep: 36) Dec 14, '24 at 2:58 am
Ali, please see my Answer but note that unfortunately the wording of your question and comments is quite confusing to both Willie and me. I'm guessing that you're translating from Arabic or similar (where the syntax is different to UK and US English), Please be patient while we question you to check what you mean.
John_Ru (rep: 6607) Dec 14, '24 at 7:21 am
Add to Discussion

Answers

0
Selected Answer

Ali

Hope I got this right (sorry but the language of your question is a bit confusing)...

In the attached. revised file, data for a missed previous month (e.g. for November 2024 in that file during December 2024) will be copied.

I've modified your code so:

  1. there are two new variables: previousCopied and prevMonth
  2. both currentMonth and prevMonth variables are now VBA dates (not strings) and recorded in the worksheet as dates- this makes the checks easier
  3. I use the Excel function EOMONTH (end of month) to check and write dates (e.g. 01/10/2024  for 01 October 2024) - see the Tutorial section for information on EOMONTH if you don't know it)
  4. the loop now checks if the current and previous month data is recorded
  5. if the previous month is not there, it jumps to a placeholder DoPrevious in the code (to skip your copy date < 27 test) and overwrites the currentMonth variable (so the same code can be used to write the previous (or current) month data.

See changes (and comments) in bold:

Option Base 1

Private Sub CommandButton1_Click()
    Dim i As Long
    Dim lastrow As Long
    Dim sheet2 As Worksheet
    Dim currentMonth As Date
    Dim copyDate As Date
    Dim remainingDays As Long
    Dim headersWritten As Boolean

    Set sheet2 = ThisWorkbook.Sheets("FORM") ' Replace with the correct sheet name if necessary

    ' lastrow line moved down

    ' Check if headers are already written
    headersWritten = (sheet2.Cells(1, 1).Value = "MONTH" And sheet2.Cells(1, 2).Value = "ITEM" _
                      And sheet2.Cells(1, 3).Value = "NAME" And sheet2.Cells(1, 4).Value = "BALANCE")

    ' Write headers if not already present
    If Not headersWritten Then
        sheet2.Cells(1, 1).Value = "MONTH"
        sheet2.Cells(1, 2).Value = "ITEM"
        sheet2.Cells(1, 3).Value = "NAME"
        sheet2.Cells(1, 4).Value = "BALANCE"
    End If

    ' Determine the last row
    lastrow = sheet2.Cells(sheet2.Rows.Count, 1).End(xlUp).Row

    ' Check if this month has already been copied
    currentMonth = WorksheetFunction.EoMonth(Date, -1) + 1
    Dim alreadyCopied As Boolean, previousCopied As Boolean, prevMonth As Date

    prevMonth = WorksheetFunction.EoMonth(Date, -2) + 1
    alreadyCopied = False
    previousCopied = False
    For i = 2 To lastrow
'        If sheet2.Cells(i, 1).Value = currentMonth Then
'            alreadyCopied = True
'            Exit For
'        End If
        ' check if previous month or current month was copied
        If sheet2.Cells(i, 1).Value = prevMonth Then previousCopied = True
        If sheet2.Cells(i, 1).Value = currentMonth Then alreadyCopied = True
    Next i

    If alreadyCopied Then
        MsgBox "The data has already been copied for this month!", vbExclamation
        Exit Sub
    End If

    If previousCopied = False Then
        MsgBox "The data for " & Format(prevMonth, "Mmm-yy") _
        & " cannot be found." & vbCr & "This will be copied first of all", vbExclamation
        ' jump to placeholder to insert data
        GoTo DoPrevious
        'Exit Sub
    End If

    ' Check if today is within the allowed days
    copyDate = Date
    If Day(copyDate) < 27 Then
        remainingDays = 27 - Day(copyDate)
        MsgBox "Warning: You need to wait " & remainingDays & " more days to copy the data.", vbExclamation
        Exit Sub
    ElseIf Day(copyDate) >= 27 And Day(copyDate) <= 31 Then
' placeholder for copying missed previous month
DoPrevious:

        ' Copy data from ListBox1 to Sheet2
        With Me.ListBox1
            ' write headers if not already done
            If headersWritten Then
                sheet2.Cells(lastrow + 1, 1).Value = "MONTH"
                sheet2.Cells(lastrow + 1, 2).Value = "ITEM"
                sheet2.Cells(lastrow + 1, 3).Value = "NAME"
                sheet2.Cells(lastrow + 1, 4).Value = "BALANCE"
                Else
                ' adjust lastrow to avoid gap for first data set
                lastrow = 0
            End If
            ' determine which month to record
            If previousCopied = False Then
                currentMonth = prevMonth
            End If
            ' write month data
            For i = 1 To .ListCount - 1
                sheet2.Cells(lastrow + i + 1, 1).Value = currentMonth
                sheet2.Cells(lastrow + i + 1, 2).Value = .List(i, 0)
                sheet2.Cells(lastrow + i + 1, 3).Value = .List(i, 1)
                sheet2.Cells(lastrow + i + 1, 4).Value = .List(i, 2)
                ' lastrow = lastrow + 1
            Next i
            ' refresh the screen
            Application.ScreenUpdating = True
        End With

        MsgBox "Data successfully copied for " & Format(currentMonth, "Mmm-yy"), vbInformation
    Else
        MsgBox "Warning: You can only copy data between the 27th and 31st of the month.", vbExclamation
        Exit Sub
    End If
End Sub

Note that if you have no data in the sheet, it will copy last month's data anyway- don't know if this presents you with a problem (e.g. at the start of a year or new file).

Hope this fixes your problem. If so, please remember to mark this Answer as Selected

Discuss

Discussion

Thanks john 
as to this 
Note that if you have no data in the sheet, it will copy last month's data anyway- don't know if this presents you with a problem (e.g. at the start of a year or new file).
shouldn't do this  because I will select current month from combobox ,in this case will not accept my selection from combobox !
Ali M (rep: 36) Dec 14, '24 at 9:23 am
Thanks for selecting my Answer, Ali. 

Regarding your last comment, do you mean that you have a problem with that behaviour (or that your ComboBox selection avoids the problem) ? 
John_Ru (rep: 6607) Dec 14, '24 at 11:02 am
Regarding your last comment, do you mean that you have a problem with that behaviour 
yes I will select  current month from combobox , and this prevents me from copy selected month from combobox . for instance if I will start from OCT  should copy OCT but the code will ignore OCT and copy NOV !
Ali M (rep: 36) Dec 14, '24 at 11:41 am
Thanks Ali but I'm still not completely clear... 

If you pick October 24 in the UserForm,  do you want to check that September 24 is recorded? 

Likewise, if you pick December 24 in the UserForm (not yet in the file),  do you want to check (only) that November 24 is recorded?
John_Ru (rep: 6607) Dec 14, '24 at 5:03 pm
If you pick October 24 in the UserForm,  do you want to check that September 24 is recorded?
no,when there is no data should allow me copy selected month from combobox ignoring previous month data when copy from the first time
Likewise, if you pick December 24 in the UserForm (not yet in the file),  do you want to check (only) that November 24 is recorded?
will not do that as long in current month DEC 24 doesn't reach to days 27,28...  because DEC 24 doesn't leave current month DEC .so the code will track DEC 24 until reach days 27,28... and if Exceeds days  then should copy DATA for DEC 24.
EX:  if current month is JAN 25  and DEC 24 is not existed then of course will check DEC 24  and copy .
Ali M (rep: 36) Dec 15, '24 at 2:45 am
Sorry Ali but it may be several days before I can try to understand this and respond to this (I have run out of time to fix this since I'm going overseas for a few days and will be busy once I get back).
John_Ru (rep: 6607) Dec 16, '24 at 4:43 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login