引用另一张工作表中的单元格范围 [英] Refereing to a range of cell in another sheet

查看:94
本文介绍了引用另一张工作表中的单元格范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用按钮复制范围内的所有值,以将这些值粘贴到不同工作表中的范围内。

I am currently trying to use a button to copy all the values inside a range to paste these values inside a range in a different sheet.

Excel给了我一个此代码第二行出现错误:

Excel gives me an error with the second line of this code:

Sub COPYVALUES()
Range("Base_Copy!F15:Base_Copy!AK46").Select
Selection.Copy

Range("F15:AK46").Select
ActiveSheet.Paste
End Sub

我的两个表是 Base_Copy ,其中的值是,我想将其粘贴到 Final_Copy

My two sheets are Base_Copy where the values are, an i want to paste the into Final_Copy

推荐答案

有3种不同的方法可以引用不同的图纸。以下是从这里被盗: http://www.ozgrid.com /VBA/excel-vba-sheet-names.htm

There are 3 different methods to reference different sheets. The below was stolen from here: http://www.ozgrid.com/VBA/excel-vba-sheet-names.htm

Sheets标签名

如果您曾经在Excel中记录过一个宏,该宏引用了工作簿中的特定工作表,您将知道,只有工作表名称保持相同,该代码才能继续工作。例如,代码like; Sheets( Budget)。Select应该重新命名预算表将不再起作用。这是因为宏记录器基于工作表选项卡名称(我们在Excel中看到的名称)生成此类代码。如果让您感觉更好,许多VBA编码人员也会通过两种更好的方式使用工作表选项卡名称,因为他们知道更好。

If you have ever recorded a macro in Excel that references a specific sheet in the Workbook you will know that the code will only continue to work if the Sheet name(s) remain the same. For example, code like; Sheets("Budget").Select will no longer work should the Budget Sheet be re-named. This is because the macro recorder generates such code based on the Sheets tab name, the name we see when in Excel. If it makes you feel better, many VBA coders also use the Sheet tab names over two much better ways, as they know no better.

索引编号

一张纸索引号由其在工作簿中的位置决定。最左边的工作表将始终具有1的索引号,右边的工作表将始终为2,依此类推。 Excel VBA允许我们通过使用索引号指定任何工作表,但是不幸的是,当我们记录宏时,Excel并未使用此方法。它使用表格标签名称,例如: Sheets( Budget)。Select。如果此工作表是从左数第三的工作表,则可以使用:Sheets(3).Select。这通常比使用工作表标签名称更好,但仍然存在潜在的问题。我的意思是,如果我们添加,删除或移动工作表,工作表中的工作表位置可能会发生变化。

A sheets Index number is determined by its position in the Workbook. The left most sheet will always have an Index number of 1, the next on the right will be 2 and so on. Excel VBA allows us to specify any Sheet by using it's Index number, but unfortunately this method is not used by Excel when we record a macro. It uses the Sheets Tab name like; Sheets("Budget").Select If this sheet was the third from the left we could use: Sheets(3).Select. This is often a better option than using the sheet tab name, but still has potential problems. By this I mean, the sheets position in the Workbook could change if we add, remove or move sheets.

工作表代号

这是精明的VBA编码器使用的方法。工作簿中的每个工作表都具有唯一的CodeName,即使移动,重命名或添加其他工作表也不会更改。只能通过进入Visual Basic编辑器(工具>宏> Visual Basic编辑器Alt + F11)然后显示项目资源管理器(视图> Project Explorer Ctl + R)来查看每张工作表的CodeName

This is the method used by savvy VBA coders. Each Sheet in a Workbook is given a unique CodeName that does not change even when that sheet is moved, renamed or other sheets are added. Each sheets CodeName can only be seen by going into the Visual Basic Editor (Tools>Macro>Visual Basic Editor Alt+F11) and then displaying the Project Explorer (View>Project Explorer Ctl+R)


在上面的屏幕截图中,选项卡名称为Budget的工作表为Sheet3。在项目资源管理器中查找时,图纸CodeName始终是不在括号内的名称。我们可以使用以下方法在工作簿中使用VBA代码引用此工作表:Sheet3.Select与Sheets( Budget)。Select或Sheets(3)相对应。选择

In the screen shot above, the CodeName for the sheet with a tab name of Budget is Sheet3. A sheets CodeName is always the name not inside the parenthesis when looking in the Project Explorer. We can reference this sheet with VBA code in the Workbook by using: Sheet3.Select as apposed to Sheets("Budget").Select or Sheets(3).Select

如果您的工作簿已经充满,已记录或编写的不使用CodeName的VBA代码,则可以在项目级别(工作簿中所有模块中的所有代码)进行更改,方法是在VBE中转到编辑>替换(可视化)基本编辑器)。
一个退回

If your Workbook is already full of VBA code, recorded or written, that does not use the CodeName you can change it on a Project level (all code in all Modules in the Workbook) by going to Edit>Replace while in the VBE (Visual Basic Editor). One Draw back

唯一无法使用工作表CodeName的情况是,当您引用位于不同工作簿中的工作表与代码所在的工作表时。

The only times you cannot use a sheets CodeName is when you reference a Sheet that is in a different Workbook to the one that the code resides.

有了以上信息,您的代码现在变成:

With the above information your code now becomes:

Sub COPYVALUES()
    Sheets("Base_Copy").Range("F15:AK46").Copy
    ' OR Sheets(2).Range("F15:AK46").Select
    ' OR Sheet2.Range("F15:AK46").Select

    Sheets("Final_Copy").Range("F15:AK46").Paste
    Application.CutCopyMode = False    
End Sub

现在注意:您不必使用复制/粘贴来移动数据...

Now on a side note: You don't have to use copy/paste to move data...

尝试以下单行代码:

Sub COPYVALUES()
    Sheets("Final_Copy").Range("F15:AK46").Value = Sheets("Base_Copy").Range("F15:AK46").Value
End Sub

这篇关于引用另一张工作表中的单元格范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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