简体   繁体   中英

Running a procedure based on cell values in another workbook

I have six different forms, all of which contain the same information but in different places. These forms are sent to me by other parties and once I receive them, I open the file, determine which form I am dealing with, then run the appropriate transposition macro to upload the information to my summary workbook.

I would like to be able to check cells B4, B8, B10, B15 and B20 to ascertain what form I am dealing with, then have the appropriate transposition macro run on its own.

Can someone please help me set this up?

Right now I have the following:

' I use the file path to identify the form that I want to copy information from
Dim FilePath As String
Dim InputTemplate As Workbook
Dim UploadForm As Workbook
Dim Analysis As Worksheet
Dim supplierdata As Worksheet
Dim TemplateType

Set UploadForm = ActiveWorkbook
FilePath = UploadForm.Sheets("Summary").Range("D4").Value
Set InputTemplate = Workbooks.Open(FilePath)
Set Analysis = UploadForm.Sheets("Analysis")
Set supplierdata = InputTemplate.Sheets("Supplier Input Template")

How do I say that if B4.value=Company Name, B8.value=Date, B10.value = Currency... then run the correct transposition macro?

The most-condensed you could do it might be something like:

'....
Set supplierdata = InputTemplate.Sheets("Supplier Input Template")

With supplierdata.Columns(2) 
    If .Cells(4).value = "Company Name" And .Cells(8).Value="Date" _
       And .Cells(10).Value = "Currency" Then

        'handle this type of form 

    End If


End With

If you have many of these types of checks to perform, then you could consider using a "metadata-driven" approach, where you list on a worksheet the Ranges and the corresponding content, and loop over that information to detect the type of report. That would be more coding up-front but easier ongoing maintenance once you have it set up.

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