我正在编写 VBA 代码来替换位于特定目录中的多个 Excel 文件(工作簿)中的特定字符串。我尝试在 Stack Overflow 上搜索,找到答案,但这与通过 Excel 中的宏替换文本文件中的字符串有关。相同的链接是 find and replace string in a file 。
我的所有工作簿都有三个工作表,每个工作表在单元格 A2 中都有 Bloomberg 公式:
=BDH("CBK IN Equity","BID","07-04-2015 09:00:00","07-04-2015 16:00:00")
该研究需要一个宏,将特定目录中许多此类工作簿中的 CBK 替换为 ALBK。这样做是为了下载 ALBK 的数据来代替 CBK。
最佳答案
使用公式属性获取单元格中公式的字符串值,然后在替换文本后重新分配它。
Dim formulaString as String
formulaString = ThisWorkbook.Sheets(1).Range("A1").Formula
formulaString = Replace(formulaString,"CBK","ALBK")
ThisWorkbook.Sheets(1).Range("A1").Formula = formulaString
浏览工作簿中的每个工作表
Dim wb as workbook
Dim i as integer
set wb = thisworkbook 'or whatever you choose to open here
for i = 1 to wb.worksheets.count
wb.sheets(i).range("A1").Formula = Replace(wb.sheets(i).range("A1").Formula,"CBK","ALBK")
next i
关于vba - 查找并替换目录中所有 Excel 文件工作簿中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32744968/