Hello I would Like some help with my vba macro

AhmedYasser

New Member
Joined
May 15, 2020
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I am trying to find a script that find the minimum Number in a certain column (H) and then select another column (M) and adds a value equal to another cell (Q)

EX:
pressing my button to find lowest value in column H and its H3 then it selects M3 and adds what I calculated on Q3 and so on till the sum in column M is equal to a certain value(cell F3)


I am not sure if I should be using solver or vba and I am still learning as much as I can any help would be appreciated



Image for my sheet
1589579016025.png
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hello Ahmed & welcome to Mr. Excel !

It is a bit difficult to make the code you need based on a picture provided specially that we don't know what are the underlaying formulas. I suggest that you upload a sample file online (Dropbox or any other sharing websites) & share the link with us here or use download the XL2BB excel add-in HERE
 
Upvote 0
Hello Ahmed & welcome to Mr. Excel !

It is a bit difficult to make the code you need based on a picture provided specially that we don't know what are the underlaying formulas. I suggest that you upload a sample file online (Dropbox or any other sharing websites) & share the link with us here or use download the XL2BB excel add-in HERE
thanks for the information ! I am uploading it and re-editing my post
 
Upvote 0
Thanks for the file. So I am looking at your file trying to understand your requirement. My understanding is the following

  1. Find minimum value in column H - in the sample file it is cell H7
  2. Copy the amount in cell Q in the same row - cell Q7 (1.390 Million)
  3. Place the amount in cell M of the same row - cell M7 - ensuring that total amount in column H (cell H32) doesn't exceed the value of cell F3

Is my understanding correct ? The problem is when I did these steps for the first line, nothing changes in column H so when I search again cell H7 still has the lowest value :unsure:
 
Upvote 0
Thanks for the file. So I am looking at your file trying to understand your requirement. My understanding is the following

  1. Find minimum value in column H - in the sample file it is cell H7
  2. Copy the amount in cell Q in the same row - cell Q7 (1.390 Million)
  3. Place the amount in cell M of the same row - cell M7 - ensuring that total amount in column H (cell H32) doesn't exceed the value of cell F3

Is my understanding correct ? The problem is when I did these steps for the first line, nothing changes in column H so when I search again cell H7 still has the lowest value :unsure:
I am sorry if I couldn't explain everything so I will try to explain the sheet in brief!
I am tried to know which contractor has the lowest percentage to pay and how far the contract is from the ceiling (D3), after getting each percentage I tried ranking them up so the person with lowest priority can get the highest amount possible from the number I enter in cell (F3)
1.1. yes
2.2.yes
3.3 correct and if I still have some amount left it should give the 2nd in place cell(H8) the rest of the amount and column N has the new percentage after adding the amount.

it's alill messy hope you are able to read through!
 
Upvote 0
I can barely read your post as it’s almost 4 AM here in Kuwait. I’ll get back to you once I wake up :)
 
Upvote 0
Hi Ahmed,

Try the below macro ... If the total amount in cell M32 is already equal or greater than cell F3 the macro will not do anything so in order to test it, I suggest that you clear all amounts in column M. Also, the macro will start adding up the amounts then will stop once cell M32 reaches the amount as per cell F3. I see some negative amounts in subsequent rows & I don't know what's suppose to happen there. Anyway, give it a try with multiple scenarios & let me know how it goes

VBA Code:
Sub AddPayments()

Dim lRow&
lRow = Range("M" & Rows.Count).End(xlUp).Row

With Sheets("Certified invoices")
    For x = 7 To lRow - 1
        If .Cells(lRow, "M") + .Cells(x, "Q") <= .Cells(3, "F") Then
            .Cells(x, "M") = .Cells(x, "Q")
        Else
            .Cells(x, "M") = .Cells(3, "F") - .Cells(lRow, "M")
            Exit For
        End If
    Next
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,766
Messages
6,126,754
Members
449,336
Latest member
p17tootie

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