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

x
?
Solved

Excel Macro pt. 2

Posted on 2010-09-06
7
Medium Priority
?
349 Views
Last Modified: 2012-06-21
Hi,

This question is really for matthewspatrick, as he wrote the original code to which it pertains. If anyone want's to pitch in check out the question this was related to.

I just realized that I need to add a 3rd column into the mix -- The State(s) column, column B. So there will need to be a sheet for every 3-combination. Full Name, State, Plan Type.

Also, it does kick up dupes, like you said it would. Even in this case though, I cannot name them right. Could we truncate the name down to make sure that the -[state]-[Plan Type] fits? The word "commercial" is the longest plan type, and state is 2 letters, so we need to make sure that -NJ-Commercial will fit at the end, which is 14 chars)

So can it be named something equivalent to=Concatenate(left([Full Name],16),"-",[state],"-",[Plan Type])

Thanks a ton!

Andrey
0
Comment
Question by:andreyman3d2k
  • 4
  • 3
7 Comments
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 2000 total points
ID: 33615008
This seems to be working.  The exception handling on the worksheet name is probably superfluous now, but leaving it in will not harm anything.

It's an extension of the "dictionary of dictionaries" approach outlined in my article http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/A_3391-Using-the-Dictionary-Class-in-VBA.html
Sub BreakItUp()
    
    Dim dic1 As Object, dic2 As Object, dic3 As Object
    Dim LastR As Long, LastC As Long
    Dim arr As Variant
    Dim Source As Worksheet
    Dim Dest As Worksheet
    Dim Counter As Long
    Dim FullName As String
    Dim PlanType As String
    Dim State As String
    Dim arr2 As Variant, arr3 As Variant
    Dim Counter2 As Long, Counter3 As Long
    Dim rng As Range
    Dim WsName As String
    
    Set Source = ThisWorkbook.Worksheets("Soucre")
    With Source
        .[a1].AutoFilter
        LastR = .Cells(.Rows.Count, "a").End(xlUp).Row
        LastC = .Cells(1, .Columns.Count).End(xlToLeft).Column
        Set rng = .Range("a1", .Cells(LastR, LastC))
        arr = rng.Value
        Set dic1 = CreateObject("Scripting.Dictionary")
        dic1.CompareMode = vbTextCompare
        For Counter = 2 To LastR
            FullName = arr(Counter, 1)
            State = arr(Counter, 2)
            PlanType = arr(Counter, 3)
            If dic1.Exists(FullName) Then
                Set dic2 = dic1.Item(FullName)
                If dic2.Exists(State) Then
                    Set dic3 = dic2.Item(State)
                    dic3.Item(PlanType) = PlanType
                Else
                    Set dic3 = CreateObject("Scripting.Dictionary")
                    dic3.CompareMode = vbTextCompare
                    dic3.Add PlanType, PlanType
                End If
            Else
                Set dic2 = CreateObject("Scripting.Dictionary")
                dic2.CompareMode = vbTextCompare
                Set dic3 = CreateObject("Scripting.Dictionary")
                dic3.CompareMode = vbTextCompare
                dic3.Add PlanType, PlanType
                dic2.Add State, dic3
                dic1.Add FullName, dic2
            End If
        Next
        arr = dic1.Keys
        For Counter = 0 To UBound(arr)
            FullName = arr(Counter)
            Set dic2 = dic1.Item(FullName)
            arr2 = dic2.Keys
            For Counter2 = 0 To UBound(arr2)
                State = arr2(Counter2)
                Set dic3 = dic2.Item(State)
                arr3 = dic3.Keys
                For Counter3 = 0 To UBound(arr3)
                    PlanType = arr3(Counter3)
                    .[a1].AutoFilter 1, FullName
                    .[a1].AutoFilter 2, State
                    .[a1].AutoFilter 3, PlanType
                    With ThisWorkbook
                        Set Dest = .Worksheets.Add(After:=.Worksheets(.Worksheets.Count))
                    End With
                    On Error Resume Next
                    WsName = "-" & State & "-" & PlanType
                    WsName = Left(FullName, 31 - Len(WsName)) & WsName
                    Do
                        Dest.Name = WsName
                        If Err <> 0 Then
                            Err.Clear
                            WsName = InputBox("Bad worksheet name.  Please enter replacement", "Invalid Entry", WsName)
                        Else
                            Exit Do
                        End If
                    Loop
                    rng.SpecialCells(xlCellTypeVisible).Copy Dest.[a1]
                Next
            Next
        Next
        .[a1].AutoFilter
        .Select
    End With
    
    Set dic1 = Nothing
    Set dic2 = Nothing
    Set dic3 = Nothing
    
    MsgBox "Done"
    
End Sub

Open in new window

0
 
LVL 6

Author Closing Comment

by:andreyman3d2k
ID: 33626748
Awesome, thanks again. I voted up your Dictionaries article. Looks interesting, wish I had a real brain instead of straw!
0
 
LVL 6

Author Comment

by:andreyman3d2k
ID: 33631055
Hi matthewspatrick,

I just noticed and issue with the code -- for some reason it is only generating one sheet per full name, but not the rest! So for example, if there is a 'Angela Smith-AR-Commercial' sheet generated, it will not make a 'Angela Smith-MO-Commercial'. That data just vanishes...

It is super-urgent! could you help?

Thanks a ton,

Andrey
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 33631783
Please post a sample file, and I will have a look.
0
 
LVL 6

Author Comment

by:andreyman3d2k
ID: 33632534
Thanks. Attached is a workbook, which contains sample data layed out the way I have it, your macro, and the sheets that result when I run you macro. As you can see there are many other name-state-plan type combinations that do not get a sheet.

Thanks again,

Andrey
EE-Example.xlsm
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 33633036
I was missing one puny line :)

The code below appears to be working now.

Sub BreakItUp()
    
    Dim dic1 As Object, dic2 As Object, dic3 As Object
    Dim LastR As Long, LastC As Long
    Dim arr As Variant
    Dim Source As Worksheet
    Dim Dest As Worksheet
    Dim Counter As Long
    Dim FullName As String
    Dim PlanType As String
    Dim State As String
    Dim arr2 As Variant, arr3 As Variant
    Dim Counter2 As Long, Counter3 As Long
    Dim rng As Range
    Dim WsName As String
    
    Set Source = ThisWorkbook.Worksheets("Soucre")
    With Source
        .[a1].AutoFilter
        LastR = .Cells(.Rows.Count, "a").End(xlUp).Row
        LastC = .Cells(1, .Columns.Count).End(xlToLeft).Column
        Set rng = .Range("a1", .Cells(LastR, LastC))
        arr = rng.Value
        Set dic1 = CreateObject("Scripting.Dictionary")
        dic1.CompareMode = vbTextCompare
        For Counter = 2 To LastR
            FullName = arr(Counter, 1)
            State = arr(Counter, 2)
            PlanType = arr(Counter, 3)
            If dic1.Exists(FullName) Then
                Set dic2 = dic1.Item(FullName)
                If dic2.Exists(State) Then
                    Set dic3 = dic2.Item(State)
                    dic3.Item(PlanType) = PlanType
                Else
                    Set dic3 = CreateObject("Scripting.Dictionary")
                    dic3.CompareMode = vbTextCompare
                    dic3.Add PlanType, PlanType
                    dic2.Add State, dic3          'this is the line that was missing!
                End If
            Else
                Set dic2 = CreateObject("Scripting.Dictionary")
                dic2.CompareMode = vbTextCompare
                Set dic3 = CreateObject("Scripting.Dictionary")
                dic3.CompareMode = vbTextCompare
                dic3.Add PlanType, PlanType
                dic2.Add State, dic3
                dic1.Add FullName, dic2
            End If
        Next
        arr = dic1.Keys
        For Counter = 0 To UBound(arr)
            FullName = arr(Counter)
            Set dic2 = dic1.Item(FullName)
            arr2 = dic2.Keys
            For Counter2 = 0 To UBound(arr2)
                State = arr2(Counter2)
                Set dic3 = dic2.Item(State)
                arr3 = dic3.Keys
                For Counter3 = 0 To UBound(arr3)
                    PlanType = arr3(Counter3)
                    .[a1].AutoFilter 1, FullName
                    .[a1].AutoFilter 2, State
                    .[a1].AutoFilter 3, PlanType
                    With ThisWorkbook
                        Set Dest = .Worksheets.Add(After:=.Worksheets(.Worksheets.Count))
                    End With
                    On Error Resume Next
                    WsName = "-" & State & "-" & PlanType
                    WsName = Left(FullName, 31 - Len(WsName)) & WsName
                    Do
                        Dest.Name = WsName
                        If Err <> 0 Then
                            Err.Clear
                            WsName = InputBox("Bad worksheet name.  Please enter replacement", "Invalid Entry", WsName)
                        Else
                            Exit Do
                        End If
                    Loop
                    rng.SpecialCells(xlCellTypeVisible).Copy Dest.[a1]
                Next
            Next
        Next
        .[a1].AutoFilter
        .Select
    End With
    
    Set dic1 = Nothing
    Set dic2 = Nothing
    Set dic3 = Nothing
    
    MsgBox "Done"
    
End Sub

Open in new window

0
 
LVL 6

Author Comment

by:andreyman3d2k
ID: 33637161
Ah, genius! Thank you muchly.

Andrey
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

772 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