Solved

Array question for Excel VBA

Posted on 2002-06-14
7
492 Views
Last Modified: 2012-06-21
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
Comment
Question by:Probie
  • 3
  • 2
  • 2
7 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7077560
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
 
LVL 1

Author Comment

by:Probie
ID: 7077659
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7077690
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 5

Expert Comment

by:Julian_K
ID: 7078013
You'll have some example code in 5 minutes.
0
 
LVL 5

Accepted Solution

by:
Julian_K earned 200 total points
ID: 7078048
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
 
LVL 5

Expert Comment

by:Julian_K
ID: 7078051
I write it in a hurry, there may be some bugs :-(
But I have to go home and had no time for more...
0
 
LVL 1

Author Comment

by:Probie
ID: 7080224
Great Job...

It worked perfect...




0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now