简体   繁体   中英

VBA to copy cell values from workbooks in a folder

I need to create calculations for about 150 products, each of which consists of about 50-60 sub-products. Depending whether the sub-product is produced or bought in from an external source, the price would be calculated differently. Essentially I need to copy-paste the weight and price for each sub-product included in the product and determine what the source is.

I would like excel to look at previously made calculations in the same folder (all different workbooks) and if it finds a cell in one of the workbooks, then to copy-paste the weight, price and source to the currently open workbook. I'm completely lost as to how to compare the cells and if a match is found, then copy that to the right place. I believe .Find and .FindNext could be useful here, but I'm not entirely sure how to use them.

Below is an example of how my data is structured: 在此处输入图片说明

And this is what I've found so far:

    Sub RunCodeOnAllXLSFiles()
            Dim lCount As Long
            Dim wbResults As Workbook
            Dim wbCodeBook As Workbook


       Application.ScreenUpdating = False
       Application.DisplayAlerts = False
       Application.EnableEvents = False

       On Error Resume Next
           Set wbCodeBook = ThisWorkbook
               With Application.FileSearch
                    .NewSearch
                    'Change path to suit
                    .LookIn = "C:\ahjualune\stuff"
                    .FileType = msoFileTypeExcelWorkbooks
                    'Optional filter with wildcard
                    .Filename = "*NAV*.xls*"
            If .Execute > 0 Then 'Workbooks in folder
                For lCount = 1 To .FoundFiles.Count 'Loop through all
                    'Open Workbook x and Set a Workbook variable to it
                    Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)

                    'DO YOUR CODE HERE
                    With ActiveSheet
                             For Each c In .Range("B2:B90").Cells
   ' problematic place         If c.Value = Then


                                  End If
                            Next c
                    End With

                    wbResults.Close SaveChanges:=False
                Next lCount
            End If
    End With
    On Error GoTo 0
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    End Sub
  • This is what i understood from you question u had 150 products.
  • Each product will had 50 to 60 sub products.
  • You will have weight and price for each sub product.Based on number of sets of that kind of sub product you had you will multiply price per set and number of sets to find total price for that sub product.
  • After that you will add all sub products price to find one product price.

If this what you are looking for i have one idea.

Put all the Main product types in sheet1 of one workbook.

From second sheet onwards put each product sub products in one sheet. Use excel sheet formulas to multiply and add prices.

Record a macro to copy that price of sub products to sheet1.

store the source in a variable

figure out the price for the current row -add to a variable

when the source changes put the totals in a column to the right

maybe use something like

range("A2").select

dim sSource as string
dim dPrice as decimal
Dim dWeight as decimal
dim dtotal as decimal

Do Until ActiveCell = ""
    sSource = ActiveCell.Offset(0,8).value
    dprice = Activecell.offset(0,7).value
   Activecell.Offset(1,0).Select
   etc.....
   when your source changes write out to a column to the right...or something like that
Loop 

Sorry, no time to write out complete code for you...but trying to get you on the path...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM