简体   繁体   English

Excel VBA:将范围名称作为函数参数传递

[英]Excel VBA: Passing a range name as a function argument

I'm writing a macro to do a complex copy/paste exercise. 我正在编写一个宏来进行复杂的复制/粘贴练习。 It's pretty straightforward conceptually, but I'm stuck in one spot. 这在概念上非常简单,但我被困在一个地方。 All of the various blocks of data are identified with various named ranges. 所有各种数据块都使用各种命名范围进行标识。 I need iterate through this list of names, passing each name as an argument to a function (actually a subroutine, but same idea). 我需要遍历这个名称列表,将每个名称作为参数传递给一个函数(实际上是一个子程序,但同样的想法)。 The source of the data is in one workbook while the destination is in another workbook. 数据源位于一个工作簿中,而目标位于另一个工作簿中。

Here is what I have (for just one block of data): 这就是我所拥有的(仅用于一个数据块):

Private Sub copyABU()
   copyPaste(ThisWorkbook.Names("myRange1").RefersToRange)
   copyPaste(ThisWorkbook.Names("myRange2").RefersToRange)
   copyPaste(ThisWorkbook.Names("myRange3").RefersToRange)
   //etc
End Sub

Private Sub copyPaste(thisRange As Range)
    Windows(someworkbook).Range(thisRange).Copy
    Range(thisRange).PasteSpecial Paste:=xlPasteValues
End Sub

Unfortunately, I get a run-time error on this. 不幸的是,我在这上面遇到了运行时错误。 I think that there is a type mismatch, but I'm not sure about this and can't figure out what I am missing. 我认为存在类型不匹配,但我不确定这一点,也无法弄清楚我错过了什么。 Can anyone see why this fails? 有人能看出为什么这会失败吗? (I'm using Excel 2010). (我正在使用Excel 2010)。

Thanks! 谢谢!

here my answer: 我的回答是:

Sub init()
Windows("Book1.xlsx").Activate
Call copyPaste(2, Range(Cells(1, 1), Cells(10, 10)), "copy")
Windows("Book2.xlsx").Activate
Call copyPaste(1, Range(Cells(1, 1), Cells(10, 10)), "paste")
End Sub

Function copyPaste(wksInt As Integer, thisRange As Range, copyPasteStr As String)
    Dim workSheetRange As Range
    With Worksheets(wksInt)
        Set workSheetRange = thisRange
    End With
    workSheetRange.Select
    If copyPasteStr = "copy" Then
        Selection.Copy
    Else
        Worksheets(wksInt).Paste
    End If
End Function

This works as long as the Ranges for copy and paste are the same dimension. 只要复制和粘贴的范围是相同的维度,这就可以工作。 If the dimension varies, you need to change workSheetRange.Select into the condition and only select the first cell of the range for pasting. 如果尺寸变化,则需要将workSheetRange.Select更改为条件,并仅选择粘贴范围的第一个单元格。 PAX PAX

Your code will work with a couple small tweaks. 您的代码将使用几个小调整。

First, you need to prefix your call to copyPaste with the word Call . 首先,您需要使用单词Call调用copyPaste前缀。 (See note below if you don't want to.) (如果您不想,请参阅下面的注释。)

Private Sub copyABU()
   Call copyPaste(ThisWorkbook.Names("myRange1").RefersToRange)
   Call copyPaste(ThisWorkbook.Names("myRange2").RefersToRange)
   Call copyPaste(ThisWorkbook.Names("myRange3").RefersToRange)
   ' //etc
End Sub

Second, add a .Address after thisRange . 其次,在.Address之后添加一个thisRange

Private Sub copyPaste(thisRange As Range)
    Range(thisRange.Address).Copy
    thisRange.PasteSpecial Paste:=xlPasteValues
End Sub

I didn't want to bother with creating a someworkbook variable, so I just deleted that part. 我不想打扰创建一个someworkbook变量,所以我只是删除了那部分。

Note: You have to use the Call keyword if you are calling a procedure with an argument list enclosed in parentheses. 注意:如果要调用括在括号中的参数列表的过程,则必须使用Call关键字。 https://stackoverflow.com/a/7715070/138938 https://stackoverflow.com/a/7715070/138938

If you don't want to use the Call keyword, omit the parens: 如果您不想使用Call关键字,请省略parens:

copyPaste ThisWorkbook.Names("myRange1").RefersToRange

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM