VBA在另一个工作簿中更改值 [英] VBA change value in another workbook

查看:63
本文介绍了VBA在另一个工作簿中更改值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助来更改另一个工作簿中的变量.

I need some help changing variables in another workbook.

首先,我使用 Workbooks.Open("test.xlsx")

当我尝试使用 Workbooks("test.xlsx").Worksheets("Sheet").Cells(1,1).Value = VariableX 更改单元格值时,出现错误9:下标超出范围.我不明白为什么它不起作用.有人可以帮我吗?

When I try to change a cell value with Workbooks("test.xlsx").Worksheets("Sheet").Cells(1, 1).Value = VariableX it gives me error 9: subscript out of range. I don't see why it won't work. Can anyone help me out on this?

推荐答案

Workbooks.open 返回Workbook对象.使用它来引用您要操作的工作簿:

Workbooks.open Returns a Workbook-object. Use this to reference the Workbook you want to manipulate:

dim wb as Workbook
set wb = Workbooks.Open("test.xlsx")
wb.Worksheets("Sheet").Cells(1,1).Value = variableX

' Close the workbook afterwards and save the changes
wb.Close True

这篇关于VBA在另一个工作簿中更改值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆