Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 49956

new to vba, need some help with if code

$
0
0
Hi there, i am new to vba coding and i need some help with a code i made.
I am trying to build a code that will add a new row to a table automatically if the current month is greater then the newest month that is currently in the table
for example if the latest row shows info for December 2019 than the code will add a new row for January 2020. here is the code i made:
Code:

Sub NewMonthChck()
   
    Dim sh As Worksheet
    Set sh = ThisWorkbook.Sheets("T.value")
    Dim n As Long
    n = sh.Range("A" & Application.Rows.Count).End(xlUp).Row
    If (Month(Now) > Month(Cells("n", 1).Value)) Then
        sh.Range("A" & n + 1).Value = Date
        sh.Range("B" & n + 1).Value = Range("B4").Formula
        sh.Range("C" & n + 1).Value = Range("C4").Formula
        sh.Range("D" & n + 1).Value = Range("D4").Formula
        sh.Range("E" & n + 1).Value = Range("E4").Formula
        sh.Range("F" & n + 1).Value = Range("F4").Formula
        MsgBox "A new month has started"
    End If

End Sub

When i try to run it, it says
"run time error 13:
type mismatch"

and highlights the if function line...

Please if anyone can tell me what i am doing wrong it will be of great value to me!!:confused:

Viewing all articles
Browse latest Browse all 49956

Trending Articles