vba 用于将命名范围从一个工作表复制到另一个工作表的宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12209005/
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
Macro to copy a Named Range from one Worksheet to another
提问by user1637631
I have several Named Ranges that contain constant data in one worksheet. I have a target range where one of the Named Ranges will be copied to, on another worksheet.
我在一个工作表中有几个包含常量数据的命名范围。我有一个目标范围,其中一个命名范围将被复制到另一个工作表上。
The Named Range that will be copied is selected based on user input into other cells. I have managed to create the name of the relevant Named Range in a single cell.
根据用户输入到其他单元格中选择将复制的命名范围。我设法在单个单元格中创建了相关命名范围的名称。
What I can't do (as I'm a VBA Noob who thought he could do all this without using VBA!), is create a Macro that reads the relevant cell, and then copies whatever Name Range it reads, into the target range.
我不能做的(因为我是一个 VBA 菜鸟,他认为他可以在不使用 VBA 的情况下完成所有这些!),是创建一个读取相关单元格的宏,然后将它读取的任何名称范围复制到目标范围中.
Any assistance most humbly and gratefully accepted.
最谦虚和感激地接受任何帮助。
回答by Siddharth Rout
Let's say, the name of your range is MyRange
假设您的范围的名称是 MyRange
So to copy the range you have to do this
所以要复制范围,你必须这样做
Range("MyRange").Copy
Now let's assume that Cell A1 of Sheet1
has the word MyRange
. In such a scenario, you can retrieve the value of the cell A1 using Range("A1").Value
现在让我们假设 Cell A1 ofSheet1
有单词MyRange
。在这种情况下,您可以使用检索单元格 A1 的值Range("A1").Value
So
所以
Range("MyRange").Copy
becomes
变成
Range(Range("A1").Value).Copy
Here is a complete example. I am assuming that you want to copy to say Cell A1 of Sheet2
这是一个完整的例子。我假设你想复制说 Sheet2 的 Cell A1
Sub Sample()
Dim wsI As Worksheet, wsO As Worksheet
Set wsI = ThisWorkbook.Sheets("Sheet1")
Set wsO = ThisWorkbook.Sheets("Sheet2")
wsI.Range(wsI.Range("A1").Value).Copy wsO.Range("A1")
End Sub
回答by Ganzert
i am not sure if thats what you need, but, if you need just to copy the content of the A1 cell from sheet1 to sheet2 for example, just do this:
我不确定这是否是您需要的,但是,例如,如果您只需要将 A1 单元格的内容从 sheet1 复制到 sheet2,请执行以下操作:
Plan2.Cells(1, 1).Value = Plan1.Cells(1, 1).Value
you may wnat to encapsulate the code into a sub as well:
您也可以将代码封装到子文件中:
Sub copyvalues()
Plan2.Cells(1, 1).Value = Plan1.Cells(1, 1).Value
End Sub
...and finally, you must to insert a button, using onclick() event to fire the sub(which you must to allocate into a module)
...最后,您必须插入一个按钮,使用 onclick() 事件来触发 sub(您必须将其分配到模块中)
sorry my bad english, hope it helps you.
抱歉我的英语不好,希望对您有所帮助。
回答by user2811701
Public Function copyNamevalues(srcName As Name, destName As Name)
srcName.RefersToRange.Cells.Copy (destName.RefersToRange.Cells)
End Function