简体   繁体   English

在VBA中跨两个子函数使用数组

[英]Using an Array Across two SubFunctions in VBA

I'm writing a macro that compares two columns of data and then identifies the rows where there is duplicate data found across both columns. 我正在编写一个宏,该宏将两列数据进行比较,然后标识在两列中都存在重复数据的行。 That part of my program works. 我程序的那部分工作。 However, I dont know how to use arrays across two separate "Subs" in VBA. 但是,我不知道如何在VBA中的两个单独的“子”之间使用数组。 It's easier to explain if you first see my code. 如果您第一次看到我的代码,就更容易解释了。

Function DuplicateFinder(SheetName1 As String, SheetName2 As String)

Dim D As Object, C
Dim nda As Long, ndb As Long
Dim test As Range
Dim StorageArray(1000)
Dim increment
increment=0   

Set D = CreateObject("scripting.dictionary")
Sheets(SheetName2).Select
ndb = Range("O" & Rows.count).End(xlUp).Row
Sheets(SheetName1).Select
nda = Range("O" & Rows.count).End(xlUp).Row

For Each C In Range("O2:O" & nda)
    D(C.Value) = 1
    C.Select
Next C

Sheets(SheetName2).Select
For Each C In Range("O2:O" & ndb)
    If D(C.Value) = 1 Then
        C.Select

        StorageArray(increment) = C.Value ' this is where i want to store the C value.
    End If
    If Len(C) = 0 Then
        C.Interior.Color = vbRed
        MsgBox "Macro terminated at the blank red cell," & Chr(10) & _
            "as per instructions"

    End If
Next C

End Function

Sub MainFunction()

Dim A As String
Dim B As String
Dim C As String
Dim D As String

A = "Sheet 1 Name"
B = "Sheet 2 Name"
C = "Sheet 3 Name"
D = "Sheet 4 Name"
increment = 0


Call DuplicateFinder(Sheet 1 Name, Sheet 2 Name)
'I would then call the function 5 more times to compare each column in each sheet to one another

End Sub

The first function is used to compare the data across column '1' and column '2', and then identify the cells where there is duplicate data across each column. 第一个函数用于比较列“ 1”和列“ 2”中的数据,然后标识每个列中有重复数据的单元格。 Again, that part works. 同样,该部分起作用。 The second sub is just the main function used to run the code. 第二个子项只是用于运行代码的主要功能。 What I want to do, and don't know how to, is every time the DuplicateFinder finds a duplicate, it saves that 'data' in an array. 我想做的,但不知道怎么做,是每当DuplicateFinder找到一个重复项时,它将“数据”保存在一个数组中。 However, I need to run the DuplicateFinder Function 6 times to compare the data across each sheet in my workbook. 但是,我需要运行DuplicateFinder Function 6次以比较工作簿中每张纸上的数据。 For example, if the sheets name's were A, B, C, and D. I need to run the function that compares A to B, A to C, A to D, B to C, B to D, and finally C to D. However, the data saved in the array is only available in the DuplicateFinder Function. 例如,如果工作表名称为A,B,C和D。我需要运行将A与B,A与C,A与D,B与C,B与D以及最后C与D进行比较的函数但是,保存在阵列中的数据仅在DuplicateFinder Function中可用。

I was thinking maybe the solution was to have the function return the value, but I don't understand how that works. 我当时在想解决方案是让函数返回值,但我不知道它是如何工作的。 I would appreciate anyone's input. 我会很感激任何人的投入。

You can return an array from a function by using this notation as function return type: 您可以使用以下符号从函数中返回数组作为函数返回类型:

Public Function MyFunction(param1 As String, param2 As String) As String() 公共函数MyFunction(param1作为字符串,param2作为字符串) 作为String()

For example: 例如:


Option Explicit

Sub MainFunction()

    Const WS_NAMES As String = "Sheet1, Sheet2, Sheet3"

    Dim ws() As String, dups() As Variant, i As Integer, totalWS As Long

    ws = Split(WS_NAMES, ", ")
    totalWS = UBound(ws)
    ReDim dups(totalWS)

    dups(0) = DuplicateFinder(ws(0), ws(1))
    dups(1) = DuplicateFinder(ws(0), ws(2))
    dups(2) = DuplicateFinder(ws(1), ws(2))

    MsgBox dups(0)(1)
    MsgBox dups(1)(1)
    MsgBox dups(2)(0)

End Sub

Function DuplicateFinder(SheetName1 As String, SheetName2 As String) As String()

    Dim StorageArray(1) As String

    StorageArray(0) = SheetName1
    StorageArray(1) = SheetName2

    DuplicateFinder = StorageArray

End Function

You can avoid passing the array by using a module-level variable. 您可以避免使用模块级变量来传递数组。

Private Duplicates() As String
Private NumDups As Long

Sub MainFunction()

Dim A As String
Dim B As String
Dim C As String
Dim D As String

A = "Sheet 1 Name"
B = "Sheet 2 Name"
C = "Sheet 3 Name"
D = "Sheet 4 Name"

NumDups = 0
ReDim Duplicates(NumDups)

Call DuplicateFinder(A, B)
Call DuplicateFinder(A, C)
Call DuplicateFinder(A, D)
Call DuplicateFinder(B, C)
Call DuplicateFinder(B, D)
Call DuplicateFinder(C, D)

End Sub

Function DuplicateFinder(SheetName1 As String, SheetName2 As String)

Dim D As Object
Dim C As Range
Dim nda As Long, ndb As Long

Set D = CreateObject("scripting.dictionary")
Sheets(SheetName2).Select
ndb = Range("O" & Rows.Count).End(xlUp).Row
Sheets(SheetName1).Select
nda = Range("O" & Rows.Count).End(xlUp).Row

For Each C In Range("O2:O" & nda)
    D(C.Value) = 1
Next C

Sheets(SheetName2).Select
For Each C In Range("O2:O" & ndb)
    If D(C.Value) = 1 Then
        NumDups = NumDups + 1
        ReDim Preserve Duplicates(NumDups)
        Duplicates(NumDups - 1) = C.Value
    End If
    If Len(C.Value) = 0 Then
        C.Interior.Color = vbRed
        MsgBox "Macro terminated at the blank red cell," & Chr(10) & _
            "as per instructions"
    End If
Next C

End Function

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

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