Size Matching within a require limits

Darshan Shah

New Member
Joined
Jul 4, 2020
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hello,

I stuck in a middle of an output. Attaching herewith the code which i've used in my sheet. Existing output & require output is attached as image.

Below is a code.. Need some correction..

VBA Code:
Option Explicit

Dim inparr() As Double, outarr() As String

Sub test()
Dim arr, i As Long
arr = Range(Cells(6, "A"), Cells(Rows.Count, "A").End(xlUp)).Value
ReDim inparr(1 To UBound(arr))
For i = 1 To UBound(arr)
  inparr(i) = arr(i, 1)
Next i
ReDim outarr(1 To 1)
check_next_one "", 0, 0
If Range("E6") <> "" Then Range("E6").CurrentRegion.Clear
With Range("E6").Resize(UBound(outarr) - 1, 1)
  .Value = Application.Transpose(outarr)
  .TextToColumns Destination:=Range("E6"), DataType:=xlDelimited, Comma:=True
End With
End Sub

Sub check_next_one(ByVal currentset As String, ByVal currentsum As Double, ByVal currentposition As Long)
Dim i As Long
If currentsum <= Range("G1") Then  'else do nothing
  If currentsum >= Range("G2") Then 'it's one of solutions
    i = UBound(outarr)
    ReDim Preserve outarr(1 To i + 1)
    outarr(i) = currentset
  End If
  For i = currentposition + 1 To UBound(inparr)
    check_next_one currentset & inparr(i) & ",", currentsum + inparr(i), i
  Next i
End If
End Sub
 

Attachments

  • ScreenShot.JPG
    ScreenShot.JPG
    48.2 KB · Views: 11

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I got lost in the logic of your macro.
The following provides the results you need, I hope it helps you.

VBA Code:
Sub test2()
  Dim c As Range, d As Range
  Range("E6:F" & Rows.Count).Clear
  For Each c In Range("A6", Range("A" & Rows.Count).End(3))
    For Each d In Range(c, Range("A" & Rows.Count).End(3))
      If c + d >= [G2] And c + d <= [G1] Then Range("E" & Rows.Count).End(3)(2).Resize(1, 2).Value = Array(c, d)
    Next
  Next
End Sub
 
Upvote 0
Appreciate your code Mr. DanteAmor. Which is working good with two size combination. But in case of three or four or five sizes needs to be match, this code is not giving output.

Further, if we add any extra sizes after running this model, this code is overwriting size matching output.

Thank you :)
 
Upvote 0
Recursion is not my forte, I hope someone can help with this.
 
Upvote 0

Forum statistics

Threads
1,215,256
Messages
6,123,914
Members
449,132
Latest member
Rosie14

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top