简体   繁体   中英

Assigning date to a cell in excel vba

I am just trying to enter the current date into a cell through excel vba macro.

Following is the code for the same.

Cells(i,j).value = Format(Date, "mm-dd-yyyy")

Its giving me

Application-defined OR Object defined error

Cells(i,j).value = Format(Now, "mm-dd-yyyy")

Your code seems nice. However, the problem is probably that you do not declare the activesheet. Or see what are the values of i and j :

debug.print i; j
ActiveSheet.Cells(i,j).value = Format(Date, "mm-dd-yyyy")

or

Worksheets(1).Cells(i,j).value = Format(Date, "mm-dd-yyyy")

See a bit about the range object here: https://msdn.microsoft.com/en-us/library/office/ff838238.aspx

由于建议的方法对我不起作用,因此我找到了另一个解决方案:

ActiveSheet.Cells(i,j).Value2 = Date

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