vba 更改不同工作簿中单元格中的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10769638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:18:09  来源:igfitidea点击:

Changing the value in a cell in a different workbook

excel-vbavbaexcel

提问by AndroidDev

I'm a little stuck trying to store a value in a cell in another open workbook. I'm getting a subscript out of range error", so clearly I'm not pointing to this cell correctly. Here is what I have:

我在尝试将值存储在另一个打开的工作簿的单元格中时遇到了一些困难。我得到一个下标超出范围错误”,所以很明显我没有正确指向这个单元格。这是我所拥有的:

    Workbooks(wbSource).Activate
    ActiveWorkbook.Sheet32.Range("currMth").Value = Me.cboCurrMth.Value

wbSource is a string that holds the name of the target workbook ("myfile.xls" for example). I'm confident that that is defined and set correctly.

wbSource 是一个包含目标工作簿名称的字符串(例如“myfile.xls”)。我相信这是正确定义和设置的。

I've tried lots of different ways to store this value, but nothing works. This is just one of my attempts. I'd prefer to not even activate this workbook if I can just populate it from the workbook where the code is written.

我尝试了很多不同的方法来存储这个值,但没有任何效果。这只是我的尝试之一。如果我可以从编写代码的工作簿中填充它,我什至不希望激活该工作簿。

Can anybody see what I am doing wrong?

谁能看到我做错了什么?

Thanks.

谢谢。

回答by JohnB

1.) What is Sheet32? 2.) I am not sure that you can index Ranges by name.

1.) 什么是 Sheet32?2.) 我不确定您是否可以按名称索引范围。

The following works:

以下工作:

Dim wb As Workbook
Set wb = Workbooks ("t1.xlsx")
wb.Names("MyRange").RefersToRange.Value2 = 5