+ Reply to Thread
Results 1 to 4 of 4

Select/update/retrieve cells between Excel files

Hybrid View

  1. #1
    Registered User
    Join Date
    10-13-2022
    Location
    morocco
    MS-Off Ver
    2021
    Posts
    7

    Select/update/retrieve cells between Excel files

    Hi please I need help. I want to use an excel file named for ex:Contracts as my workbook this file contains this cells: model, interval, KmC.
    And other excel files as source of data, here is what I want to achieve in

    steps:

    1- In Contracts when I write model name in B1 the system should choose the file with the same name. nothing will happened just picking the right workbook for the rest.

    2- when I write kmc B2 it should be updated in a given cell in the right workbook that has same Model name.

    3 - same think as step 2

    NB: after updating these values the amount and tax will change in the model file that we are working with.

    4 - now I want to retrieve the amount and tax in the workbook that has the same name of the model that we write in the first step.



    Please I need guide for what I have to use in each step or what I should learn in the future to do those kind of things: exchange data between file, Data manipulation. Create word documents from excel.

    Thanks in advance

  2. #2
    Valued Forum Contributor
    Join Date
    09-18-2023
    Location
    Geogia, USA
    MS-Off Ver
    365
    Posts
    308

    Re: Select/update/retrieve cells between Excel files

    You could do something like:

    Private Sub Worksheet_Change(ByVal Target As Range)
        ' this event fires when something changes in the worksheet
        
        ' keep the workbook filename in memory
        Static modelWorkborkFileName As String
        
        Dim sourceDataPath As String
        sourceDataPath = "F:\Temp\"  ' this is the path to find the Excel files to use as source data
        
        Select Case Target.Address
            Case "$B$1" ' this code will run if the cell B1 changed - MODEL
                ' build the complete path to the source data file
                modelWorkborkFileName = sourceDataPath & Target.Value & ".xlsx"
                
            Case "$B$2"
                ' call the procedure that opens and interacts with the source data workbook
                ActiveSheet.Range("C2").Value = OpenSourceDataWB(modelWorkborkFileName, UCase(Target.Value))
                
        End Select
    End Sub
    
    Private Function OpenSourceDataWB(workbookFileName As String, whatValueToRetrieve As String) As Variant
    
        ' open the source data workbook file
        Dim fso As FileSystemObject
        Set fso = New FileSystemObject
        
        ' check to see if the workbook is where it is expected to be
        If Not fso.FileExists(workbookFileName) Then
            MsgBox "The workbook '" & workbookFileName & "' could not be found", vbCritical, "Missing File"
            Set fso = Nothing
            OpenSourceDataWB = ""
            Exit Function
        End If
        
        Application.ScreenUpdating = False  ' don't display the opened workbook
        
        Dim sourceDataWB As Workbook
        Dim sourceDataWS As Worksheet
        Dim readCellValue As Variant
        
        Set sourceDataWB = Workbooks.Open(workbookFileName)
        Set sourceDataWS = sourceDataWB.Sheets("Sheet1") ' which sheet will be used in the workbook
        
        ' this is an example of reading data from a specific cells in the source workbook
        ' depending on an indicator passed to this function
        Select Case whatValueToRetrieve
            Case "KMC"
                readCellValue = sourceDataWS.Range("B2").Value
            Case "ABC"
                readCellValue = sourceDataWS.Range("C2").Value
        End Select
        
        Set sourceDataWS = Nothing
        sourceDataWB.Close
        
        Set sourceDataWB = Nothing
        Application.ScreenUpdating = True  ' turn displaying on again
        
        OpenSourceDataWB = readCellValue
        
    End Function

    2- when I write kmc B2 it should be updated in a given cell in the right workbook that has same Model name.
    This isn't clear on if the Contracts workbook is updated or the Model workbook is updated

    The example will show you one way to react to a cell's change and open a workbook.

    Good luck with your project, hopefully, you can use this to figure out the rest.
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    10-13-2022
    Location
    morocco
    MS-Off Ver
    2021
    Posts
    7

    Re: Select/update/retrieve cells between Excel files

    Thanks man I appreciate your help

  4. #4
    Valued Forum Contributor
    Join Date
    09-18-2023
    Location
    Geogia, USA
    MS-Off Ver
    365
    Posts
    308

    Re: Select/update/retrieve cells between Excel files

    You're welcome, good luck with your project.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 07-10-2022, 09:51 AM
  2. [SOLVED] Retrieve values from different Excel files
    By jeriss in forum Excel General
    Replies: 1
    Last Post: 04-11-2022, 12:02 AM
  3. Retrieve value of field across different Excel files
    By jeriss in forum Excel General
    Replies: 0
    Last Post: 02-25-2022, 10:55 AM
  4. how to retrieve latest update by using vlookup in excel
    By satya sunil in forum Excel General
    Replies: 1
    Last Post: 03-10-2020, 09:43 AM
  5. Retrieve and Update Files
    By emmieshane in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-31-2018, 07:08 AM
  6. Replies: 5
    Last Post: 10-24-2014, 04:58 PM
  7. How to Update Values without having to re-select source files
    By ichiruki17 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-08-2012, 09:46 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1