English 中文(简体)
Excel VBA: 传递一个区域名称作为函数参数
原标题:Excel VBA: Passing a range name as a function argument
  • 时间:2012-05-23 23:46:22
  •  标签:
  • excel
  • vba

我写一个宏来做一个复杂的复制/ 粘贴练习。 它在概念上相当直截了当, 但我被困在一个地方。 所有的数据区块都与不同的命名范围相匹配。 我需要通过这个名称列表进行循环, 将每个名称作为参数传递到函数( 实际上是一个子例程, 但想法相同 ) 。 数据来源在同一个工作簿中, 而目的地则在另一个工作簿中 。

以下是我拥有的(仅一整块数据):

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

不幸的是,我在这上面有一个运行时间错误。 我认为有一个类型不匹配, 但我不确定我错过了什么。 有人能看到为什么失败吗? (我使用Excel 2010 ) 。

谢谢!

问题回答

你的代码会用一些小节奏来操作

首先,您需要用 < code> Call 这个词前缀您要复制 Paste 的电话。 (如果您不想,请在下面注明 。 )

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

其次,在 后面添加一个 .Address

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

我不想费心创建一个 someworkbook 变量,所以我删除了该部分。

注: >如果您要求采用括号中附有引号的程序程序,则必须使用 Call 关键词。

我的回答是:

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

只要复制和粘贴的范围是相同的维度,此功能就有效。 如果维度不同, 您需要将工作 SheetRange 修改为条件, 只选择区域的第一个单元格进行粘贴。 PAX





相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...