这是我在网上发布的第一个查询。我希望有人能帮帮忙。请原谅我,因为我不是专家,并且在 学习excel vba 的早期阶段。
如果您能就以下方面提供任何帮助,我将不胜感激。我们也欢迎任何改进代码的建议!
代码目标概述:
a)用户将打开Excel文件'实体列表更改'其中包含vba和表格内容 - 查找和替换。
b)用户将打开他们想要将文本替换为其他值的文件。然后,用户将单击Ctrl Q以执行“实体列表更改”中保存的代码。 - 我认为在宏的选项部分添加这个就可以了。
c)然后,代码将存储他们希望更改详细信息的工作簿名称和文件路径。
d)然后,代码将要求用户使用是或否查询确认应执行代码的文件。
e)如果是,那么它将进入“实体列表更改”。获取数组的文件然后针对第一个子目录中保存的原始工作簿执行代码。
我遇到以下问题:
1)在将工作簿切换到实体更改名称文件后,我似乎无法回想起工作簿 - 它无法读取存储在第一个子程序中的原始工作簿。 (注意我不知道在第一个实例中使用了什么文件。)
2)消息框识别用户选择是否忽略运行该函数并直接进入其他 - 不执行任何操作。 - 为什么我不知道? 它将Yes识别为0,将No识别为0
**Excel VBA Code:**
Option Explicit
Public retvalue As String ' hold the stored value so I can reuse it in a sub procedure.
Dim finalFile As Workbook
Public Sub GetFilePath() ' want to hold variable publicly so it can be reused.
Dim strFileAndPath As String
retvalue = ActiveWorkbook.FullName
strFileAndPath = retvalue
Set finalFile = ActiveWorkbook
retvalue = Application.ActiveWorkbook.Path
'finalFile = Application.ActiveWorkbook.Name this creates a 438 error, cannot use - unsure how to fix this?
'Debug.Print retvalue '********************(first check)
'personal note if you have the string in the public then you do not need to hold it anywhere else.
'this part of the code works, retvalue hold the path and strFileAndPath hold the complete path and file name. FinalFile does store the activeworkbook at a workbook. - checked Locals window!
End Sub
Sub Multi_FindReplace()
'Note this code will not work if there are formula errors, these cells will be omitted from the change. - tested!
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Dim sht As Worksheet
Dim fndList As Integer
Dim rplcList As Integer
Dim tbl As ListObject
Dim myArray As Variant
Dim ActiveOrgFilePath As String
Dim answer As Integer
Dim TempArray As Variant
Dim x As Integer
Dim strFileName As Workbook
'Store and get the file name of the active workbook.
Call GetFilePath
''Debug.Print retvalue '********************(second check)
'Convert the string (file path) to workbook name
retvalue = Split(retvalue, "\")(UBound(Split(retvalue, "\")))
'Confirm that this is the file you would like the change to occur.
MsgBox retvalue & vbNewLine & "Please confirm that this is the file name you wish to change the entity names?", vbYesNo, "Change Entity Names"
'if you wanted the file name to be held after the msg
If answer = vbYes Then
Debug.Print answer '********************(second check)
'file with table array identified to obtain array information:
Windows("Entity List Change.xlsm").Activate 'Obtain code and array from Macro workbook.
'Create variable to point to your table
Set tbl = Worksheets("Sheet1").ListObjects("EntityList1")
'Create an Array out of the Table's Data
Set TempArray = tbl.DataBodyRange
myArray = Application.Transpose(TempArray)
'Designate Columns for Find/Replace data
fndList = 1
rplcList = 2
'Then to go to the original workbook held at the beginning held in 'retvalue', to loop through all sheets in the workbook find values as per those held in Entity List Change.xlsm,
'once found then replace the values in workbook 'retvalue'
Set finalFile = ActiveWorkbook
'finalFile = Application.ActiveWorkbook.Name cannot use, not recognised in first sub procedure?
'Windows(finalFile).Activate
'Workbooks("retvalue").Select
'Windows("finalFile").Activate
'None of these are not working??
'Loop through each item in Array lists
For x = LBound(myArray, 1) To UBound(myArray, 2)
'Loop through each worksheet in ActiveWorkbook (skip sheet with table in it)
For Each sht In ActiveWorkbook.Worksheets
If sht.Name < tbl.Parent.Name Then
sht.Cells.Replace What:=myArray(fndList, x), Replacement:=myArray(rplcList, x), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
End If
Next sht
Next x
MsgBox "The Entity name change has been completed." & vbNewLine & "Thank you for your patience."
Else
'do nothing
MsgBox "The Entity name change has not been completed." & vbNewLine & "Please find and open the correct file to make the change."
End If
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
End Sub
答案 0 :(得分:0)
欢迎来到StackOverflow,并祝贺详细询问。关于你的问题:
retvalue
内部存储了适当的值(您在MsgBox中显示它,并且您并不是说它不会在那里工作)。我只看到您注释掉的代码中存在问题:Workbooks("retvalue").Activate
将查找名为“retvalue”的工作簿。要引用当前存储在变量retvalue
中的值,请使用不带引号的Workbooks(retvalue).Select
。answer
分配值。将其更改为answer = MsgBox(retvalue & vbNewLine & ...)
。注意括号!最后一个想法:在VBA中,.Activate
经常不是你的朋友。几乎在所有情况下,如果将要激活的内容存储在变量中(例如Dim ws as Worksheet
),最好将其分配,分配该变量(Set ws = ThisworkBook.Sheets("Sheetname")
),然后使用{{ 1}} - 属性(例如.Value
)。这将为您节省大量的混淆和可预防的错误。有关这方面的更多信息。见this thread on SO。