简体   繁体   中英

runtime error 1004 in excel vba

I am new to vba and I do not know how to solve this issue-

run time error 1004

and num value is found to be empty on debugging. I want to get the num value in my code in the line, Sheet1.Range("C" & num & ":C18")

Enum MonthOfYear
April = 7
May = 8
June = 9
July = 10
August = 11
September = 12
October = 13
November = 14
December = 15
January = 16
February = 17
March = 18
End Enum
Private Sub GenerateTDS_Click()
Dim month As String

month = Sheet2.Cells(31, "J")


num = myfunction(month)



Sheet1.Range("C" & num & ":C18") = 34
End Sub
Function myfunction(month)

If (month = "April") Then
myfunction MonthOfYear.April
End If
If (month = "May") Then
myfunction MonthOfYear.May
End If
If (month = "June") Then
myfunction MonthOfYear.June
End If
If (month = "July") Then
myfunction MonthOfYear.July
End If
If (month = "August") Then
myfunction MonthOfYear.August
End If
If (month = "September") Then
myfunction MonthOfYear.September
End If
If (month = "October") Then
myfunction MonthOfYear.October
End If
If (month = "November") Then
myfunction MonthOfYear.November
End If
If (month = "December") Then
myfunction MonthOfYear.December
End If
If (month = "January") Then
myfunction MonthOfYear.January
End If
If (month = "February") Then
myfunction MonthOfYear.February
End If
If (month = "March") Then
myfunction MonthOfYear.March
End If
End Function

Change this:

month = Worksheet(2).Cells(31, "J")

to this:

month = Worksheet(2).Cells(31, "J").Value

And this:

Sheet1.Range("C" & num & ":C18") = 34

To this:

For Each c in Worksheet(1).Range("C" & num & ":C18") 
 c.Value = 34
Next c

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM