Variable not being stored in the code when sheet changes

萝らか妹 提交于 2019-11-28 14:08:51

The problem is that you do not define the worksheet correctly. See the points(dots) here:

With Trade_Sheet
    lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    Date_range = .Cells(lastrow, 1).Offset(-27, 0).Value
    .Cells(lastrow + 2, 1).Value = Date_range + 7
End With

In your code, you are missing 2 of them:

Thus, the Cells() refers to the ActiveSheet, and not to the Trade_Sheet. In general, Activate and Select are considered a bad practice in , thus it is a good idea avoid them:


For what is worth, this is probably the most common error in , thus you can be proud of yourself for reaching it. I guess that almost every VBA person has experienced it at least once.

Perhaps problem is you did not have dots in this part of code?

 lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
 Date_range = .Cells(lastrow, 1).Offset(-27, 0).Value
 .Cells(lastrow + 2, 1).Value = Date_range + 7
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!