Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 501
  • Last Modified:

Array question for Excel VBA

I need to build an array that only should contain unique strings that must not exist more the once in the array. (even if the range can include duplicates of the string)

I have a range A1:A100 to conatins strings (names to other sheets in a Excel workbook).

I need to filll an array with all the strings in this range, but there must NOT be any duplicates of the strings. And I also need to check if the string in each cell of the range is a name to a worksheet that exist.

Does anyone know how to do this in VBA?
I am a delphi coder, and how not found any good VBA reference.


Example:

Lets say I have a range in Excel that looks like this:

    A        B        C        D
1  Sheet1
2  Sheet2
3  Sheet1
4  Sheet4
5  Sheet3
6  Sheet2
7  MySheet
8  TestSheet
9  Sheet2


Lets say that my Excel Workbook has a coresponding sheet for each of this strings, except for "TestSheet" in cell A8. I now need to build an array that looks like:

 Array ("Sheet1","Sheet2","Sheet4","Sheet3","MySheet")

A unique string should only exist once in the array and if there is no worksheet with the same name as the string, it should also not be included in the array.

Can all this be done with VBA in Excel?


Best Regards
Magnus Flysjö

0
Probie
Asked:
Probie
  • 3
  • 2
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Check out the following menu of Excel:

Data->Filter->Advanced Filter
*Choose the range you want to filter (ie A1:A100),
*choose the destination (eventually same range...)
*choose unique records only
click OK

CHeers
0
 
ProbieAuthor Commented:
is it possible to put the result into an array instead of in cells?

Range("A1:A100").AdvancedFilter Action:=xlFilterCopy, Range:="B1:B100", Unique:=True

Like this (into MyArray):

Range("A1:A100").AdvancedFilter Action:=xlFilterCopy, Range:=MyArray, Unique:=True
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
No, this isn't possible. you have first to Filter to a Range (maybe you can use a temporary workbook only for that), and then build your array from the results.
CHeers
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Julian_KCommented:
You'll have some example code in 5 minutes.
0
 
Julian_KCommented:
Option Explicit

Public Sub Test()
        Dim Result As Variant, I As Long
    Result = TestNames(ActiveWindow.RangeSelection)
   
    For I = 1 To UBound(Result)
        Debug.Print Result(I)
    Next I
   
    Erase Result
End Sub

Public Function TestNames(ByVal SheetNameRange As Range) As Variant
        'Declare some Variables for operating the cycles.
        'There are better decisions, but this is just an example
        Dim I As Long, L As Long, J As Long, K As Long
        Dim strSheetName As String
       
        'Return Value: an array of string
        Dim retArr() As String
       
    'Initialize the array
    ReDim Preserve retArr(0)
   
    'Get how many rows are there in the range being sent.
    L = SheetNameRange.Rows.Count
   
    'For each Row in the Range
    '(This cycle can be substituted with for each... next)
    For I = 1 To L
       
        'Get the cell content (expected to be a sheet name)
        strSheetName = SheetNameRange.Cells(I, 1)
       
        'Check if this sheet Exists
        If SheetExists(strSheetName) Then
       
            'If exists, then verify if it has been added to result array, or not
            K = UBound(retArr)
            For J = 1 To K
                If retArr(J) = strSheetName Then Exit For
            Next J
           
            'If the cycle has finished, that means this name has not been found.
            'Then add it to the result array
            If J > K Then
                ReDim Preserve retArr(J)
                retArr(J) = strSheetName
            End If
           
        End If
   
        'next cell
    Next I
   
    'Return the result
    TestNames = retArr
   
    'Clear the memory
    Erase retArr
   
End Function


Private Function SheetExists(ByVal strSheetName As String) As Boolean
    On Error Resume Next
    SheetExists = False
    SheetExists = Not (ThisWorkbook.Sheets.Item(CStr(strSheetName)) Is Nothing)
    On Error GoTo 0
End Function
0
 
Julian_KCommented:
I write it in a hurry, there may be some bugs :-(
But I have to go home and had no time for more...
0
 
ProbieAuthor Commented:
Great Job...

It worked perfect...




0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now