Advertisement
Alx09

Untitled

Aug 24th, 2023
1,079
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. Sub Buton2_Clic()
  2.     Dim sumaCautata As Double
  3.     Dim coloana As Range
  4.    
  5.     sumaCautata = InputBox("Introduceti suma pe care doriti sa o atingeti:")
  6.     Set coloana = Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
  7.    
  8.     ClearColumnC2 ' Cură?ăm coloana C2 înainte de a începe
  9.    
  10.    Dim usedRows() As Boolean
  11.    ReDim usedRows(1 To coloana.Rows.Count) As Boolean
  12.    
  13.    FindSums coloana, sumaCautata, 1, "", 0, usedRows
  14. End Sub
  15.  
  16. Sub FindSums(coloana As Range, sumaCautata As Double, rowIndex As Long, indices As String, currentSum As Double, usedRows() As Boolean)
  17.    Dim celula As Range
  18.    
  19.    For i = rowIndex To coloana.Rows.Count
  20.        Set celula = coloana.Cells(i, 1)
  21.        
  22.        If celula.Value <> "" And IsNumeric(celula.Value) And Not usedRows(i) Then
  23.            Dim newSum As Double
  24.            newSum = currentSum + celula.Value
  25.            
  26.            If newSum = sumaCautata Then
  27.                WriteIndices indices & celula.Row & " " ' Înregistrăm indicele rândului
  28.                 usedRows(i) = True
  29.             ElseIf newSum < sumaCautata Then
  30.                 FindSums coloana, sumaCautata, i + 1, indices & celula.Row & " ", newSum, usedRows ' Apelăm recursiv pentru următorul rând
  31.            End If
  32.        End If
  33.    Next i
  34. End Sub
  35.  
  36. Sub WriteIndices(indices As String)
  37.    Dim lastRow As Long
  38.    lastRow = Range("C" & Rows.Count).End(xlUp).Row + 1
  39.    
  40.    Range("C" & lastRow).Value = Trim(indices)
  41. End Sub
  42.  
  43. Sub ClearColumnC2()
  44.    Range("C:C").ClearContents
  45. End Sub
  46.  
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement