简体   繁体   English

将一系列列作为常量参数传递

[英]Passing a range of columns as a constant parameter

I am trying to pass a range of columns as a parameter for a function.我正在尝试将一系列列作为 function 的参数传递。

For sr = 1 To srCount 
    If IsEmpty(Data(sr, 4)) Then
        
        dr = dr + 1
        Data(dr, 4) = Data(sr, 1) 'instead of just the first column, a range of columns
        
    End If
Next sr

I thought I could define a range ("A:C") and pass its reference as a parameter, but VBA doesn't seem to accept anything but (references to) long variables/constants as parameters for the Data() function. What is the correct syntax for such a thing?我以为我可以定义一个范围(“A:C”)并将其引用作为参数传递,但 VBA 似乎只接受(引用)长变量/常量作为 Data() function 的参数。什么这种事情的正确语法是什么?

Edited 01/26 for clarification: Data is an array.编辑 01/26 以澄清: Data是一个数组。 The goal is to copy a row of a range of columns based on the condition that another cell in that row is empty ( If IsEmpty(Data(sr, 4)) ).目标是根据该行中的另一个单元格为空的条件 ( If IsEmpty(Data(sr, 4)) ) 复制一系列列中的一行。 Eg if cell(7,4) is empty, then row 7 of columns AC should be copied to another area of the worksheet (K2:M2).例如,如果单元格 (7,4) 为空,则应将 AC 列的第 7 行复制到工作表的另一个区域 (K2:M2)。 If (13,4) is empty, then row 13, columns AC to K3-M3, and so on.如果 (13,4) 为空,则为第 13 行,列 AC 到 K3-M3,依此类推。

As per @Cameron's tip I used Collections to store the ranges instead.根据@Cameron 的提示,我使用 Collections 来存储范围。

Dim users As New Collection
Dim cell As Range

With Worksheets(2) 
    users.Add .Range("A:C"), "Users"    
    users.Add .Range("K:M"), "FinalList"

End With


For Each cell In users.Item("Users")
   For sr = 1 to srCount
    If IsEmpty(Data(sr, 4)) Then
        
        dr = dr + 1
        FinalList = Users

    End If
Next sr
   
Next

Despite the research I can't find how I can manipulate Collections for this objective.尽管进行了研究,但我找不到如何为此目标操纵 Collections。 Once I have all the necessary values stored in FinalList, how can I copy them to the goal Range ("K:M")?一旦我将所有必要的值存储在 FinalList 中,我如何将它们复制到目标范围(“K:M”)?

Using a Single Array to Extract Matching Data使用单个数组提取匹配数据

Option Explicit

Sub Test()
    
    Const SRC_CRITERIA_COLUMN As Long = 4
    Const DST_WORKSHEET_ID As Variant = 2 ' using the tab name is preferable!
    Const DST_FIRST_CELL As String = "K2"
    ' The following could be calculated from the source variables.
    Const DST_COLUMNS_COUNT As Long = 3
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    ' whatever...
    
    Dim Data() ' ?
    Dim srCount As Long ' ?
    
    ' whatever...
    
    Dim sr As Long, dr As Long, c As Long
    
    ' Write the matching values to the top of the array
    ' When using a single array, the result needs to be in the top-left
    ' of the array. The data of interest is already left-most
    ' so there is no column offset.
    For sr = 1 To srCount
        If IsEmpty(Data(sr, SRC_CRITERIA_COLUMN)) Then
            dr = dr + 1
            For c = 1 To DST_COLUMNS_COUNT
                Data(dr, c) = Data(sr, c)
            Next c
        End If
    Next sr

    ' Reference the destination range.
    Dim dws As Worksheet: Set dws = wb.Worksheets(DST_WORKSHEET_ID)
    Dim dfCell As Range: Set dfCell = dws.Range(DST_FIRST_CELL)
    ' Note how you're using just the 'dr' number of rows
    ' and 'DST_COLUMNS_COUNT' number of columns.
    Dim drg As Range: Set drg = dfCell.Resize(dr, DST_COLUMNS_COUNT)
    
    ' Write the result from the top-left of the array to the destination range.
    drg.Value = Data
    ' Clear below.
    drg.Resize(dws.Rows.Count - drg.Row - dr + 1).Offset(dr).ClearContents

    MsgBox "Data copied.", vbInformation

End Sub

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

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