VBA: How to merge duplicate data in a single array/dictionary and return values?












0















I've written a VBA macro for CorelDraw that loops through selected objects and returns values as a string. I want it to be able to handle duplicate data better, for example if two objects have the same size, it should return



"2 of 10 x 10"


instead of



"1 of 10 x 10"
"1 of 10 x 10"


Coming from Ruby (specifically thinking of hashes), I'm thinking that the macro should loop through the selected range, add (object.sizeWidth, object.sizeHeight) data as strings to an array/dictionary where it should be checked for duplicates and count them. I don't know what's best or how to set/check their values.



Here is my code so far



Sub objectsToString()

Dim str As String
Dim v As Shape, vr As ShapeRange
Dim xSize#, ySize#
Dim dupCount As Integer

str = ""
Set vr = ActiveSelectionRange

For Each v In vr
dupCount = 'value assigned via iteration
xSize = v.SizeWidth
ySize = v.SizeHeight
str = str & dupCount & " of " & xSize & " x " & ySize & vbNewLine

Next v

End Sub









share|improve this question

























  • " I'm thinking that the macro should loop through the selected range, add (object.sizeWidth, object.sizeHeight) data as strings to an array/dictionary " - yes, that's correct. You need to use a dictionary (can find plenty of examples online), there you can check if the specific dimensions were already used, if not add them, if yes, increase the number of their occurrence

    – Máté Juhász
    Feb 13 at 14:58
















0















I've written a VBA macro for CorelDraw that loops through selected objects and returns values as a string. I want it to be able to handle duplicate data better, for example if two objects have the same size, it should return



"2 of 10 x 10"


instead of



"1 of 10 x 10"
"1 of 10 x 10"


Coming from Ruby (specifically thinking of hashes), I'm thinking that the macro should loop through the selected range, add (object.sizeWidth, object.sizeHeight) data as strings to an array/dictionary where it should be checked for duplicates and count them. I don't know what's best or how to set/check their values.



Here is my code so far



Sub objectsToString()

Dim str As String
Dim v As Shape, vr As ShapeRange
Dim xSize#, ySize#
Dim dupCount As Integer

str = ""
Set vr = ActiveSelectionRange

For Each v In vr
dupCount = 'value assigned via iteration
xSize = v.SizeWidth
ySize = v.SizeHeight
str = str & dupCount & " of " & xSize & " x " & ySize & vbNewLine

Next v

End Sub









share|improve this question

























  • " I'm thinking that the macro should loop through the selected range, add (object.sizeWidth, object.sizeHeight) data as strings to an array/dictionary " - yes, that's correct. You need to use a dictionary (can find plenty of examples online), there you can check if the specific dimensions were already used, if not add them, if yes, increase the number of their occurrence

    – Máté Juhász
    Feb 13 at 14:58














0












0








0








I've written a VBA macro for CorelDraw that loops through selected objects and returns values as a string. I want it to be able to handle duplicate data better, for example if two objects have the same size, it should return



"2 of 10 x 10"


instead of



"1 of 10 x 10"
"1 of 10 x 10"


Coming from Ruby (specifically thinking of hashes), I'm thinking that the macro should loop through the selected range, add (object.sizeWidth, object.sizeHeight) data as strings to an array/dictionary where it should be checked for duplicates and count them. I don't know what's best or how to set/check their values.



Here is my code so far



Sub objectsToString()

Dim str As String
Dim v As Shape, vr As ShapeRange
Dim xSize#, ySize#
Dim dupCount As Integer

str = ""
Set vr = ActiveSelectionRange

For Each v In vr
dupCount = 'value assigned via iteration
xSize = v.SizeWidth
ySize = v.SizeHeight
str = str & dupCount & " of " & xSize & " x " & ySize & vbNewLine

Next v

End Sub









share|improve this question
















I've written a VBA macro for CorelDraw that loops through selected objects and returns values as a string. I want it to be able to handle duplicate data better, for example if two objects have the same size, it should return



"2 of 10 x 10"


instead of



"1 of 10 x 10"
"1 of 10 x 10"


Coming from Ruby (specifically thinking of hashes), I'm thinking that the macro should loop through the selected range, add (object.sizeWidth, object.sizeHeight) data as strings to an array/dictionary where it should be checked for duplicates and count them. I don't know what's best or how to set/check their values.



Here is my code so far



Sub objectsToString()

Dim str As String
Dim v As Shape, vr As ShapeRange
Dim xSize#, ySize#
Dim dupCount As Integer

str = ""
Set vr = ActiveSelectionRange

For Each v In vr
dupCount = 'value assigned via iteration
xSize = v.SizeWidth
ySize = v.SizeHeight
str = str & dupCount & " of " & xSize & " x " & ySize & vbNewLine

Next v

End Sub






vba macros coreldraw






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 13 at 14:30







Thirdcube

















asked Feb 13 at 11:12









ThirdcubeThirdcube

33




33













  • " I'm thinking that the macro should loop through the selected range, add (object.sizeWidth, object.sizeHeight) data as strings to an array/dictionary " - yes, that's correct. You need to use a dictionary (can find plenty of examples online), there you can check if the specific dimensions were already used, if not add them, if yes, increase the number of their occurrence

    – Máté Juhász
    Feb 13 at 14:58



















  • " I'm thinking that the macro should loop through the selected range, add (object.sizeWidth, object.sizeHeight) data as strings to an array/dictionary " - yes, that's correct. You need to use a dictionary (can find plenty of examples online), there you can check if the specific dimensions were already used, if not add them, if yes, increase the number of their occurrence

    – Máté Juhász
    Feb 13 at 14:58

















" I'm thinking that the macro should loop through the selected range, add (object.sizeWidth, object.sizeHeight) data as strings to an array/dictionary " - yes, that's correct. You need to use a dictionary (can find plenty of examples online), there you can check if the specific dimensions were already used, if not add them, if yes, increase the number of their occurrence

– Máté Juhász
Feb 13 at 14:58





" I'm thinking that the macro should loop through the selected range, add (object.sizeWidth, object.sizeHeight) data as strings to an array/dictionary " - yes, that's correct. You need to use a dictionary (can find plenty of examples online), there you can check if the specific dimensions were already used, if not add them, if yes, increase the number of their occurrence

– Máté Juhász
Feb 13 at 14:58










1 Answer
1






active

oldest

votes


















1














I don't have CorelDraw myself, but here's a comparable example:



' Set up some sample items
Dim items(2) As String
items(0) = "10 x 10"
items(1) = "20 x 20"
items(2) = "10 x 10"

' Create a dictionary to store the items and count
' Key: [n] x [n]
' Value: Count of item
Dim dict As New Scripting.Dictionary

For Each Item In items
If dict.Exists(Item) Then
' Increase existing count
dict(Item) = dict(Item) + 1
Else
' Add new item to dictionary and set count to 1
dict.Add Item, 1
End If
Next

' Print dictionary
For Each Key In dict.Keys
Debug.Print dict(Key) & " of " & Key
Next


If you haven't done so already, you need to add a reference to the Microsoft Scripting Runtime library in the Tools → References dialog. If that library is not in the list, use the browse button to select C:WindowsSystem32scrrun.dll.






share|improve this answer

























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1405231%2fvba-how-to-merge-duplicate-data-in-a-single-array-dictionary-and-return-values%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I don't have CorelDraw myself, but here's a comparable example:



    ' Set up some sample items
    Dim items(2) As String
    items(0) = "10 x 10"
    items(1) = "20 x 20"
    items(2) = "10 x 10"

    ' Create a dictionary to store the items and count
    ' Key: [n] x [n]
    ' Value: Count of item
    Dim dict As New Scripting.Dictionary

    For Each Item In items
    If dict.Exists(Item) Then
    ' Increase existing count
    dict(Item) = dict(Item) + 1
    Else
    ' Add new item to dictionary and set count to 1
    dict.Add Item, 1
    End If
    Next

    ' Print dictionary
    For Each Key In dict.Keys
    Debug.Print dict(Key) & " of " & Key
    Next


    If you haven't done so already, you need to add a reference to the Microsoft Scripting Runtime library in the Tools → References dialog. If that library is not in the list, use the browse button to select C:WindowsSystem32scrrun.dll.






    share|improve this answer






























      1














      I don't have CorelDraw myself, but here's a comparable example:



      ' Set up some sample items
      Dim items(2) As String
      items(0) = "10 x 10"
      items(1) = "20 x 20"
      items(2) = "10 x 10"

      ' Create a dictionary to store the items and count
      ' Key: [n] x [n]
      ' Value: Count of item
      Dim dict As New Scripting.Dictionary

      For Each Item In items
      If dict.Exists(Item) Then
      ' Increase existing count
      dict(Item) = dict(Item) + 1
      Else
      ' Add new item to dictionary and set count to 1
      dict.Add Item, 1
      End If
      Next

      ' Print dictionary
      For Each Key In dict.Keys
      Debug.Print dict(Key) & " of " & Key
      Next


      If you haven't done so already, you need to add a reference to the Microsoft Scripting Runtime library in the Tools → References dialog. If that library is not in the list, use the browse button to select C:WindowsSystem32scrrun.dll.






      share|improve this answer




























        1












        1








        1







        I don't have CorelDraw myself, but here's a comparable example:



        ' Set up some sample items
        Dim items(2) As String
        items(0) = "10 x 10"
        items(1) = "20 x 20"
        items(2) = "10 x 10"

        ' Create a dictionary to store the items and count
        ' Key: [n] x [n]
        ' Value: Count of item
        Dim dict As New Scripting.Dictionary

        For Each Item In items
        If dict.Exists(Item) Then
        ' Increase existing count
        dict(Item) = dict(Item) + 1
        Else
        ' Add new item to dictionary and set count to 1
        dict.Add Item, 1
        End If
        Next

        ' Print dictionary
        For Each Key In dict.Keys
        Debug.Print dict(Key) & " of " & Key
        Next


        If you haven't done so already, you need to add a reference to the Microsoft Scripting Runtime library in the Tools → References dialog. If that library is not in the list, use the browse button to select C:WindowsSystem32scrrun.dll.






        share|improve this answer















        I don't have CorelDraw myself, but here's a comparable example:



        ' Set up some sample items
        Dim items(2) As String
        items(0) = "10 x 10"
        items(1) = "20 x 20"
        items(2) = "10 x 10"

        ' Create a dictionary to store the items and count
        ' Key: [n] x [n]
        ' Value: Count of item
        Dim dict As New Scripting.Dictionary

        For Each Item In items
        If dict.Exists(Item) Then
        ' Increase existing count
        dict(Item) = dict(Item) + 1
        Else
        ' Add new item to dictionary and set count to 1
        dict.Add Item, 1
        End If
        Next

        ' Print dictionary
        For Each Key In dict.Keys
        Debug.Print dict(Key) & " of " & Key
        Next


        If you haven't done so already, you need to add a reference to the Microsoft Scripting Runtime library in the Tools → References dialog. If that library is not in the list, use the browse button to select C:WindowsSystem32scrrun.dll.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 13 at 15:08

























        answered Feb 13 at 15:00









        BerendBerend

        1,5951713




        1,5951713






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1405231%2fvba-how-to-merge-duplicate-data-in-a-single-array-dictionary-and-return-values%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

            Mangá

            Eduardo VII do Reino Unido