简体   繁体   中英

VBA - Excel (2013) How to 'Find' the same value in each sheet but replace with a different value in each sheet?

I have a workbook with 93 budgets in (one per sheet). These sheets use vlookups to pull the values other budget Excel files. I created the sheets by making duplicates of the original budget sheet and then renaming them.

Currently, all the sheets are now looking up values from the same file, but I need them to lookup the values from the different files. The range from which the lookups occur are the same in each of the 93 budget files. I'd like to do a find and replace that looks for the same value in each tab (budgetcode.xlsx) and replaces it with a new value that differs per sheet.

I found the code below which works for 1 sheet, and I would need to run this 93 times (which takes a while in this workbook). Is there a way to update it to run for all 93 sheets and replace the 'budgetcode' with the value in a certain cell in each sheet?

Sub Macro1()

Application.EnableEvents = False
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Range("E3:CN9254").Select
Selection.Replace What:="", Replacement:="0", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.CalculateFull

End Sub

Any help will be much appreciated!!

This code should give you a really nice shell. I made some obvious assumptions that you'll need to change to reflect your actual data structure. The only caveat I can think of at the moment is to ensure that when you replace the file names to make sure that the filepath exists on your drive, otherwise, you'll get a very long list of annoying pop-ups looking for a valid file path for each item to replace.

Option Explicit

Sub ChangeFileNameInFormula()

Application.ScreenUpdating = False

Dim ws as Worksheet
For each ws in ThisWorkbook.Worksheets

    Dim sBudgetCode as String
    sBudgetCode = ws.Range("A1") 'change to the cell that contains the budget code

    ws.Range("E3:CN9254").Replace What:="OriginalFileName", Replacement:=sBudgetCode, LookAt:=xlPart

Next

End Sub

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