简体   繁体   English

引用动态工作簿名称 - VBA

[英]Referencing a Dynamic Workbook Name - VBA

I have created a workbook ("Control Panel.xlsm") for a work college to use as a storing place for all our macros we use in our day to day tasks.我为工作学院创建了一个工作簿(“Control Panel.xlsm”),用作我们在日常任务中使用的所有宏的存储位置。 She is not VBA literate.她不识字 VBA。 One of the macros, (see code below) organizes another workbook so that it add a descriptive column at the end of a table.其中一个宏(参见下面的代码)组织另一个工作簿,以便在表的末尾添加一个描述性列。

    Dim lr As Long
    lr = Cells.Find("*", Cells(1, 1), xlFormulas, xlPart, xlByRows, xlPrevious, False).Row
    Dim tbl As ListObject
    Dim wbNames As Variant, wb As Workbook, w As Workbook, El As Variant, boolFound As Boolean
    wbNames = Split("January,February,March,April,May,June,July, August, September, October,November,December")
    For Each w In Workbooks
        For Each El In wbNames
            If w.Name = "Docs_Tracker_" & El & " 2020.xlsm" Then
                Set wb = w: boolFound = True: Exit For
            End If
        Next
      Next
    
    '1. Column AB - Descriptive Field - Client Name - Manager Name - Research Deliverable
If wb Is Nothing Then Stop
    Set tbl = wb.Worksheets(1).ListObjects("Table_owssvr")
    With tbl
            .ListColumns.Add.Name = "Client Name - Manager Name - Research Deliverable"
    End With
        wb.Activate
        Range("AB2:AB" & lr).FormulaR1C1 = "=CONCATENATE(RC[-22],"" - "",RC[-26],"" - "",RC[-15])"

What I would like to do is reference the workbook ("Docs_Tracker_April 2020.xlsm") so that the code still works even as the workbook reference name changes (Docs_Tracker_May 2020, Docs_Tracker_June 2020 etc)我想要做的是引用工作簿(“Docs_Tracker_April 2020.xlsm”),这样即使工作簿引用名称发生变化,代码仍然有效(Docs_Tracker_May 2020、Docs_Tracker_June 2020 等)

I have tried the ActiveWorkbook, workbook Indexing method however they can have bugging issues, and isn't as foolproof with someone who isn't as familiar as VBA as my work college is.我已经尝试过 ActiveWorkbook,工作簿索引方法,但是它们可能存在漏洞问题,并且对于不像我的工作学院那样熟悉 VBA 的人来说并不是万无一失的。

If someone could teach me this I will apply it to all my macros they all have a similar issue.如果有人可以教我这一点,我会将其应用于我所有的宏,他们都有类似的问题。 Thank you.谢谢你。

Try the next way, please.请尝试下一个方法。 In case of more such workbooks open in the same time, it will choose the first one of the array:如果同时打开更多这样的工作簿,它将选择数组中的第一个:

Sub takeTheMonthsNamedWB()
  Dim wbNames As Variant, wb As Workbook, w As Workbook, El As Variant, boolFound As Boolean
  wbNames = Split("January,February,March,April,May,June,July", ",") 'and so on...
  For Each w In Workbooks
    For Each El In wbNames
        If w.Name = "Docs_Tracker_" & El & " 2020.xlsm" Then
            Set wb = w: boolFound = True: Exit For
        End If
    Next
    If boolFound Then Exit For 'in order to stop iteration if a lot of workbooks are open
  Next
End Sub

Edited:编辑:

Try the next testing code, please.请尝试下一个测试代码。 Does it return the correct workbook name?它是否返回正确的工作簿名称?

Sub testTheMonthsNamedWB()
  Dim wbNames As Variant, w As Workbook, El As Variant, boolFound As Boolean
  wbNames = Split("January,February,March,April,May,June,July", ",") 'and so on...
    For Each El In wbNames
        Debug.Print "Docs_Tracker_" & El & " 2020.xlsm"
    Next
End Sub

only two workbooks will be open ("control panel.xlsm" and "Docs tracker depending on the month"只会打开两个工作簿(“控制面板.xlsm”和“取决于月份的文档跟踪器”

If you would like to refer to the workbook which name depends on current month, you can use something like this:如果您想参考名称取决于当前月份的工作簿,您可以使用以下内容:

Dim sWbkName As String, wbk As Workbook
sWbkName = "Docs_Tracker_" & Format(Date, "MMMM yyyy") & ".xlsm"
On Error Resume Next
Set wbk = Application.Workbooks(sWbkName)
If wbk Is Nothing Then
    MsgBox "Can't find '" & sWbkName & "'!!!" & vbCr & vbCr & _
        "Please, open it.", vbExclamation, "Error."
    Exit Sub
End If

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

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