简体   繁体   English

对象全局失败vba的方法单元格

[英]Method cells of object global failed vba

I'm using the code below to reference a range in excel and put a value into them.我正在使用下面的代码在 excel 中引用一个范围并将一个值放入其中。 The code works fine first time round but fails if i dot it again.该代码第一次运行良好,但如果我再次加点就会失败。 I get the error message (see title).我收到错误消息(见标题)。 Below is my code, can anyone see any issues?下面是我的代码,有人能看到任何问题吗?

xlApp.Workbooks("JDE1.xlsx").Sheets("TempHours").Range(Cells(2, 18), Cells(Cells(ws.Rows.Count, 1).End(xlUp).Row, 18)).Value = Batch

I know it is an old question and I happen to land on this but I will still go ahead an answer it.我知道这是一个老问题,我碰巧遇到了这个问题,但我仍然会继续回答它。

You need to fully qualify your cells object.您需要完全限定您的单元格对象。 Notice the DOT before cells() ?注意cells()之前的 DOT 吗? One should always fully qualify the objects like you did for Sheets("TempHours") .人们应该始终像您为Sheets("TempHours")所做的那样完全限定对象。

With xlApp.Workbooks("JDE1.xlsx").Sheets("TempHours")
    .Range(.Cells(2, 18), .Cells(.Cells(ws.Rows.Count, 1).End(xlUp).Row, 18)).Value = Batch
End With

Hope this helps future visitors.希望这对未来的游客有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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