• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Calculating Ordering Quantity - Stock

Here goes my first query . I need to design some formulations to calculate "ordering quantity" required for a project .

Scenario - I have some quantity of goods in stock , there are several projects planned in coming months for which i need to order some goods as per the requirement.

what i need to do is to calculate the quantity to be ordered considering "stock in hand"+"ordered quantity" . Query doesent ends here , quantity required in next projects to be automatically updated upon receipt of quantity of previous project.

attaching a file for better understanding.

Awaiting your reply.

Thanks

Aaditya
 

Attachments

  • book_11(1).xlsx
    10.4 KB · Views: 15
Hi Aaditya,

Considering Requirement and quantity ordered is punched manually for every project, just have a look of attached file (Sheet 3) I had changed the data layout and inserted an extra row between each project as revised stock.

See if this OK to you.

Regards,
 

Attachments

  • book_11(1).xlsx
    12 KB · Views: 12
Hello Somendra ,

thanks for your reply . Your idea is preety useful. I am just stuck at one place now .

If material against project -1 is received and added in stock - will it affect further ordering quantity of project 2 and 3 ? - will there be no ordering generated ? ( as stock will be having - actual stock ( material received in project-1 + material ordered in project -1 ).

can we use some logic using "if: , where we can add one more line called quantity received and same can be subtracted accordingly ..

Point No. 2 - can we keep this data as per "sheet1" , as we have around 500 items and 30 projects . Putting items in column will increase length of the sheet - horizantally ( bit difficult to keep the track).

:) hope my reply is not too long.

Thanks ,

Aaditya
 
One point here if quatntiy to be ordered is -ve thats means you dont need to order that item for this project. and you can pur 0 in quatity orderd. the stock will be carried forward for 2nd project. Just check this by putting 0 I6 of the file.

Secondly, the layout can be changed, we need to add than extra column in between projects.

Regards,
 
:) yes.. it worked fine...

Thanks Somendra...

Well , i have series of questions in my mind , tried to google it but could not get much data..

I am looking for a macro for Vlookup function . there is one post n chandoo.org regarding this , but i could not understand the complete logic ( as i am new to VB).

can you put in your comments - regarding what every command is doing ? atleast for highlighted items with red colour from attached file. That will be a great help from you.


I can assign this vlookup command to one of the command button , once i press it - it will fetch all the relevent data from other files present on the PC.
It was originally posted by Excel Ninja LUKE M on this website only.

Thanks in advance ,

Aaditya
 

Attachments

  • Vlookup Macros File.xlsx
    9.9 KB · Views: 6
Thanks for the compliments. :)
Expanding the comments on the code.
Code:
Sub MakeFormulas()
Dim SourceLastRow As Long
Dim OutputLastRow As Long
Dim sourceBook As Workbook
Dim sourceSheet As Worksheet
Dim outputSheet As Worksheet
'This is an error on my part. Should be  = False
'If we turn ScreenUpdating off, the code will run faster, as it doesn't have to spend time
'updating the screen, and user doesn't see a lot of images flying across their screen
Application.ScreenUpdating = True

'Where is the source workbook?
Set sourceBook = Workbooks.Open("C:\My Documents\Book1.xlsx")

'what are the names of our worksheets?
Set sourceSheet = sourceBook.Worksheets("Sheet1")
Set outputSheet = ThisWorkbook.Worksheets("Sheet1")

'Determine last row of source
'With Statements allow a type of shorthand. Note that the next line has a "." before Cells. This
'means that the Cells traces back to the With object. We use With when we want to not have to write as much
'This also lets the code run a little faster, as it doesn't have to do quite as much thinking
'to find out what we are working with.
With sourceSheet
    'The latter half of equation says to go to the last row in col A, and then "go up" until
    'it runs into data. This lets us find the last row of information we want to
    'be concerned with
    SourceLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    'This is equivalent to writing this line:
    'SourceLastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row
End With

'Another With Statement
With outputSheet
    'Similar to before, want to find the last row of used information
    'in col P of the outputSheet
    'Determine last row in col P
  OutputLastRow = .Cells(.Rows.Count, "P").End(xlUp).Row
    'Apply our formula
    'We want to put a formula in all of the cells. Rather than doing this one cell at a time,
    'it is much more efficient to put the formula in the entire range. Since we
    'know what the OutputLastRow is now, we will apply the formula to some dynamic range in
    'column Q.
   
    'The VLOOKUP function needs to know the name of our workbook and our sheet of interest,
    'so we call that information. The ampersand "&" lets us concatenate VB variables with
    'text strings, so that we can construct the overall formula
  .Range("Q2:Q" & OutputLastRow).Formula = _
        "=VLOOKUP(A2,'[" & sourceBook.Name & "]" & sourceSheet.Name & "'!$A$2:$B$" & SourceLastRow & ",2,0)"
End With

'Close the source workbook, don't save any changes
'The False at the end of the line is Optional. If we didn't have it, the Code would stop
'and ask us if we want to save. We prevent this by purposely telling the code we don't
'want to save
sourceBook.Close False
'Turn the screen updating back on. Again, we should have turned this off at beginning
Application.ScreenUpdating = True
End Sub
 
:) Thanks @Somendra Misra and @Luke M - Excel Ninja.. I must admit that i am really new to macros , newbie in Excel as well. I tried to customise this formula to suit my requirement , but could not succeed.. :( .

I am attaching two more sheet so that i can convey my requirement exactly.

Sorry for disturbing you both again and again for such silly doubts.. :(

Please help..

Thanks ,

Aaditya
 

Attachments

  • Test Vlookup macro.xlsm
    23.8 KB · Views: 4
  • Source File.xlsx
    11.6 KB · Views: 4
Hi Aaditya,

See the attached file.

I had made certain changes to @Luke M code. You have to change the path of your file name in the code. I had left a comment in the code where to change it.

Now Some drawbacks:
1. The code doesn't look descent.
2. I couldnt come up with some error handler for Vlookup as I am also a beginner in VBA.

May be @Luke M can give you a better solution. In mean time you csan study it.

Regards,
 

Attachments

  • Test_Vlookup_macro.xlsm
    31 KB · Views: 1
Here find a better version of the code. (Shorter + with error handler)

Regards,
 

Attachments

  • Test_Vlookup_macro.xlsm
    31 KB · Views: 1
Thanks somendra for the code.. i have large number of queries ( shown as comment in code) . can you please explain ?

once i get the complete idea of this code, i can customise it for my other files also.

Thanks in advance .

Aaditya
 
oopss.. i forgot the attachment.. sorry.. here it is..

you can refer programming section where actual code is written.

thanks,

Aaditya
 

Attachments

  • Test_Vlookup_macro-rev01.xlsm
    32.6 KB · Views: 3
Hi,

Please see my comment in code section of attached file.

Regards,
 

Attachments

  • Test_Vlookup_macro-rev01(1).xlsm
    33.4 KB · Views: 4
Hi Somendra... thanks for your feedback.. I will use and implement this with other files soon.. Thanks a ton !

Have a Happy weekend !

Thanks ,

Aaditya
 
Welcome Back anytime. Just remember when you are testing your code use a sample file as the action of macros are not reversible.

Have a Happy Weekend to you 2!

Regards,
 
Hi.. i am reciving a small error . shown in attached file .

also mentioned the code in sheet 1.

Thanks ,

Aaditya
 

Attachments

  • Error.xlsx
    54.5 KB · Views: 3
Hi...Can you tell me the whole range of your source table and Which column is PO and from what columns you want to extract data?

Regards,
 
Considering Your full table is in Range A2:AD40, But your PO Number is column 9 i.e. column I. Change the below statement as highlighted.

Set rngLookupTable = sourceSheet.Range("I2:AD" & SourceLastRow)

Second you should initialise K=2 as you want to extract data from this sub-table second column to 12th column.

Try these changes and just advise if any issue.

Regards,
 
Hello somendra ,

I tried this changes , but it is skipping first 7 columns from which i need to fetch the data . It is considering column Q as first column and then pasting that data in the source sheet. Where as my data starts from column J.

Please help.

Thanks ,

Aaditya
 
Hi Somendra ,

Here are the details.

Look up column ( from Current file ) - Column No "G" - it is PO No

Starting of Source column - Column "I" - it is having PO No ( against which value is looked up) , next columns from "J" to "U" is having the data which needs to be pasted in my current file.

Data to be pasted in current file from column "X" to "AI".

If you need any further input , please let me know.

Thanks ,

Aaditya
 
Try below code:

Code:
Sub MakeFormulas()
Dim SourceLastRow As Long
Dim OutputLastRow As Long
Dim sourceBook As Workbook
Dim sourceSheet As Worksheet
Dim outputSheet As Worksheet
'Dim rngLookupValue As Range
Dim rngLookupTable As Range
Dim rngFormulaCell As Range
Dim Result As Variant
Application.ScreenUpdating = True

Set sourceBook = Workbooks.Open("C:\Users\aaha\Desktop\Source File.xlsx")
Set sourceSheet = sourceBook.Worksheets("Sheet1")
Set outputSheet = ThisWorkbook.Worksheets("Sheet1")
With sourceSheet
  SourceLastRow = .Cells(.Rows.Count, "I").End(xlUp).Row
End With
Set rngLookupTable = sourceSheet.Range("I2:U" & SourceLastRow)

OutputLastRow = outputSheet.Cells(Rows.Count, "X").End(xlUp).Row + 1
OutputLastRow2 = outputSheet.Cells(Rows.Count, "G").End(xlUp).Row
 K = 2
 For i = OutputLastRow To OutputLastRow2
  rngLookupValue = outputSheet.Range("G" & i)
  If IsError(Application.Match(rngLookupValue, sourceSheet.Range("I2:U" & SourceLastRow), 0)) Then
  
  MsgBox "PO No. " & rngLookupValue & " Not in Source!!!"
  GoTo skipstep:
  
  End If
  For j = 24 To 35
  
  Result = outputSheet.Application.WorksheetFunction.VLookup(rngLookupValue, rngLookupTable, K, 0)
  
  outputSheet.Cells(i, j) = Result
  
  K = K + 1
  Next j
  K = 2
skipstep:
Next i
sourceBook.Close False
Application.ScreenUpdating = True
End Sub

Just advise if any issue.

Regards,
 
Can you Upload your file?
It is working the same code provided above with the sample file earlier provided.

Regards,
 
Last edited:
Back
Top