使用VBA从其他工作表获取值 [英] Get values from other sheet using VBA

查看:526
本文介绍了使用VBA从其他工作表获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从其他工作表中获取价值.

I want to get values from other sheets.

例如,我在Excel(sheet2)中有一些值:

I have some values in Excel (sheet2) for example:

    A  B  C  D
    -  -  -  -  
1 | 2  5  9  12
2 | 5  8  4  5
3 | 3  1  2  6

我对第4行的每一列求和.

I sum each column in row 4.

我正在使用sheet2中的这些值,但是我想在sheet1中获得结果.

I'm working with these values in sheet2 but I want to get the result in sheet1.

在sheet2中使用我的代码时,我得到了正确的答案,但是当我尝试在另一张工作表中使用它时,我得到的结果是与当前工作表单元格相对应的值,而不是与sheet2对应的值.

When using my code in sheet2 I get the correct answer but when I try to use it in a different sheet I get the result of the values corresponding to the current sheet cells and not to sheet2.

我正在使用With Application.WorksheetFunction.

如何将sheet2设置为活动工作表?

How can I set sheet2 as the active sheet?

推荐答案

尝试

 ThisWorkbook.Sheets("name of sheet 2").Range("A1")

可以访问工作表2中的范围,而与代码所在的位置或当前处于活动状态的工作表无关.要将工作表2设为活动工作表,请尝试

to access a range in sheet 2 independently of where your code is or which sheet is currently active. To make sheet 2 the active sheet, try

 ThisWorkbook.Sheets("name of sheet 2").Activate

如果只需要另一张纸中的一行的总和,则根本不需要使用VBA.在工作表1中输入这样的公式:

If you just need the sum of a row in a different sheet, there is no need for using VBA at all. Enter a formula like this in sheet 1:

=SUM([Name-Of-Sheet2]!A1:D1)

这篇关于使用VBA从其他工作表获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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