cancel
Showing results for
Did you mean:
Regular Visitor

## Get a combination result based on excel list column

Hello,

I have a excel with below columns,

Id Name
1 A
2 B
3 C

what I want to get is a combination result with below data in a new excel, and I would define the creation rule.

Like only have combination of 2 numbers or 3 numbers, Can anyone share me a vbs script to do it? Really appreciate your helps.

Id Name
1 A
2 B
3 C
1,2 A,B
1,3 A,C
2,3 B,C
2,4 B.D
3,4 C,D
1,2,3 A,B,C

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

From GPT...let me know if it works, lol.  Good luck!:

Here is a VBScript that will generate combinations for you. It reads data from an Excel file, generates combinations based on your criteria, and then writes the results to a new Excel file. The script is set to generate combinations of 2 and 3, but you can adjust this according to your needs.

``````Option Explicit

Dim objExcel, objWorkbook, objWorksheet
Dim arrData, arrResults(), strData
Dim i, j, k, l, m, n

' Create Excel object and open workbook
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Path\To\Input\Excel\File.xlsx")
Set objWorksheet = objWorkbook.Worksheets(1)

' Read data from worksheet into array
arrData = objWorksheet.UsedRange.Value

' Resize results array
ReDim arrResults(UBound(arrData, 1) * (UBound(arrData, 1) - 1), 2)

' Generate 2-number combinations
m = 0
For i = 1 To UBound(arrData, 1)
For j = i + 1 To UBound(arrData, 1)
arrResults(m, 0) = arrData(i, 1) & "," & arrData(j, 1)
arrResults(m, 1) = arrData(i, 2) & "," & arrData(j, 2)
m = m + 1
Next
Next

' Generate 3-number combinations
For i = 1 To UBound(arrData, 1)
For j = i + 1 To UBound(arrData, 1)
For k = j + 1 To UBound(arrData, 1)
arrResults(m, 0) = arrData(i, 1) & "," & arrData(j, 1) & "," & arrData(k, 1)
arrResults(m, 1) = arrData(i, 2) & "," & arrData(j, 2) & "," & arrData(k, 2)
m = m + 1
Next
Next
Next

' Create new workbook and worksheet
Set objWorksheet = objWorkbook.Worksheets(1)

' Write results to new worksheet
For n = 0 To m - 1
objWorksheet.Cells(n + 1, 1).Value = arrResults(n, 0)
objWorksheet.Cells(n + 1, 2).Value = arrResults(n, 1)
Next

' Save and close new workbook
objWorkbook.SaveAs "C:\Path\To\Output\Excel\File.xlsx"
objWorkbook.Close

' Quit Excel
objExcel.Quit
``````

You need to replace "C:\Path\To\Input\Excel\File.xlsx" and "C:\Path\To\Output\Excel\File.xlsx" with the paths to your input and output Excel files, respectively.

The above script generates all possible combinations of 2 and 3 numbers. If you need to adjust the numbers of combinations, you can modify the loops that generate the combinations.

Please note, this script assumes that the input Excel file has two columns ('Id' and 'Name') and that the data starts in the first row of the worksheet. If your file is set up differently, you may need to adjust the script accordingly.

Also, please be aware that VBScript can only be executed on Windows systems. If you're using a different operating system, you might need to use a different scripting language.

----
If my post has answered your question, please thumbs up and mark this post as a solution.

I also offer paid consulting services. If you would like to discuss this option, please feel free to DM me and we can set up a time to join a Zoom call and fix any issues you are having.
2 REPLIES 2
Super User

From GPT...let me know if it works, lol.  Good luck!:

Here is a VBScript that will generate combinations for you. It reads data from an Excel file, generates combinations based on your criteria, and then writes the results to a new Excel file. The script is set to generate combinations of 2 and 3, but you can adjust this according to your needs.

``````Option Explicit

Dim objExcel, objWorkbook, objWorksheet
Dim arrData, arrResults(), strData
Dim i, j, k, l, m, n

' Create Excel object and open workbook
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Path\To\Input\Excel\File.xlsx")
Set objWorksheet = objWorkbook.Worksheets(1)

' Read data from worksheet into array
arrData = objWorksheet.UsedRange.Value

' Resize results array
ReDim arrResults(UBound(arrData, 1) * (UBound(arrData, 1) - 1), 2)

' Generate 2-number combinations
m = 0
For i = 1 To UBound(arrData, 1)
For j = i + 1 To UBound(arrData, 1)
arrResults(m, 0) = arrData(i, 1) & "," & arrData(j, 1)
arrResults(m, 1) = arrData(i, 2) & "," & arrData(j, 2)
m = m + 1
Next
Next

' Generate 3-number combinations
For i = 1 To UBound(arrData, 1)
For j = i + 1 To UBound(arrData, 1)
For k = j + 1 To UBound(arrData, 1)
arrResults(m, 0) = arrData(i, 1) & "," & arrData(j, 1) & "," & arrData(k, 1)
arrResults(m, 1) = arrData(i, 2) & "," & arrData(j, 2) & "," & arrData(k, 2)
m = m + 1
Next
Next
Next

' Create new workbook and worksheet
Set objWorksheet = objWorkbook.Worksheets(1)

' Write results to new worksheet
For n = 0 To m - 1
objWorksheet.Cells(n + 1, 1).Value = arrResults(n, 0)
objWorksheet.Cells(n + 1, 2).Value = arrResults(n, 1)
Next

' Save and close new workbook
objWorkbook.SaveAs "C:\Path\To\Output\Excel\File.xlsx"
objWorkbook.Close

' Quit Excel
objExcel.Quit
``````

You need to replace "C:\Path\To\Input\Excel\File.xlsx" and "C:\Path\To\Output\Excel\File.xlsx" with the paths to your input and output Excel files, respectively.

The above script generates all possible combinations of 2 and 3 numbers. If you need to adjust the numbers of combinations, you can modify the loops that generate the combinations.

Please note, this script assumes that the input Excel file has two columns ('Id' and 'Name') and that the data starts in the first row of the worksheet. If your file is set up differently, you may need to adjust the script accordingly.

Also, please be aware that VBScript can only be executed on Windows systems. If you're using a different operating system, you might need to use a different scripting language.

----
If my post has answered your question, please thumbs up and mark this post as a solution.

I also offer paid consulting services. If you would like to discuss this option, please feel free to DM me and we can set up a time to join a Zoom call and fix any issues you are having.
Regular Visitor

It works. thank you very much.

By the way, is it possible to optimize the script, for example, has a message box for user:

if user input2, then there will be only 2 numbers result in new excel.

if user input2,3, then there will be both 2 and three numbers result in new excel.

Forgive me as a newbie, but I do not know how to adjust the number to 4, if I need 4 number result in excel.

May I have a common script for what I needed?

Announcements

#### A Celebration of What We've Achieved--And Announcing Our Winners

As the sun sets on the #SummerofSolutions Challenge, it's time to reflect and celebrate! The journey we embarked upon together was not just about providing answers – it was about fostering a sense of community, encouraging collaboration, and unlocking the true potential of the Power Platform tools.   From the initial announcement to the final week's push, the Summer of Solutions Challenge has been a whirlwind of engagement and growth. It was a call to action for every member of our Power Platform community, urging them to contribute their expertise, engage in discussions, and elevate collective knowledge across the community as part of the low-code revolution.   Reflecting on the Impact As the challenge ends, it's essential to reflect on the impact it’s had across our Power Platform communities: Community Resilience: The challenge demonstrated the resilience of our community. Despite geographical distances and diverse backgrounds, we came together to contribute, learn, and collaborate. This resilience is the cornerstone of our collective strength.Diverse Expertise: The solutions shared during the challenge underscore the incredible expertise within our community. From intricate technical insights to creative problem-solving, our members showcased their diverse skill sets, enhancing our community's depth.Shared Learning: Solutions spurred shared learning. They provided opportunities for members to grasp new concepts, expand their horizons, and uncover the Power Platform tools' untapped potential. This learning ripple effect will continue to shape our growth. Empowerment: Solutions empowered community members. They validated their knowledge, boosted their confidence, and highlighted their contributions. Each solution shared was a step towards personal and communal empowerment. We are proud and thankful as we conclude the Summer of Solutions Challenge. The challenge showed the potential of teamwork, the benefit of knowledge-sharing, and the resilience of our Power Platform community. The solutions offered by each member are more than just answers; they are the expression of our shared commitment to innovation, growth, and progress!     Drum roll, Please... And now, without further ado, it's time to announce the winners who have risen above the rest in the Summer of Solutions Challenge!   These are the top community users and Super Users who have not only earned recognition but have become beacons of inspiration for us all.   Power Apps Community:  Community User Winner: @SpongYe Super User Winner: Pending Acceptance Power Automate Community:  Community User Winner: @trice602 Super User Winner: @Expiscornovus  Power Virtual Agents Community: Community User Winner: Pending AcceptanceSuper User: Pending Acceptance Power Pages Community: Community User Winner: @OOlashyn Super User Winner: @ChristianAbata   We are also pleased to announced two additional tickets that we are awarding to the Overall Top Solution providers in the following communities:    Power Apps: @LaurensM   Power Automate: @ManishSolanki    Thank you for making this challenge a resounding success. Your participation has reaffirmed the strength of our community and the boundless potential that lies within each of us. Let's keep the spirit of collaboration alive as we continue on this incredible journey in Power Platform together.Winners, we will see you in Vegas! Every other amazing solutions superstar, we will see you in the Community!Congratulations, everyone!