简体   繁体   English

VBA - Excel (2013) 如何在每张工作表中“查找”相同的值,但在每张工作表中用不同的值替换?

[英]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).我有一本工作簿,里面有 93 个预算(每张一张)。 These sheets use vlookups to pull the values other budget Excel files.这些工作表使用 vlookups 来提取其他预算 Excel 文件的值。 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.在 93 个预算文件中的每一个中,查找发生的范围是相同的。 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.我想做一个查找和替换,在每个选项卡 (budgetcode.xlsx) 中查找相同的值,并将其替换为每个工作表不同的新值。

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).我发现下面的代码适用于 1 张纸,我需要运行 93 次(这在本工作簿中需要一段时间)。 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?有没有办法更新它以运行所有 93 个工作表,并用每个工作表中某个单元格中的值替换“预算代码”?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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