简体   繁体   English

Excel VBA - 重命名命名范围会导致无法读取的内容错误?

[英]Excel VBA - Renaming named ranges causes unreadable content error?

I'm trying to teach myself VBA by completing a project, but unfortunately I've reached the limit of what I can figure out. 我试图通过完成一个项目来教自己VBA,但不幸的是我已经达到了我能想到的极限。

The project involves a workbook with worksheet intended to be used as a template for other worksheets. 该项目涉及一个工作表,其工作表旨在用作其他工作表的模板。 This sheet has multiple tables which are referenced by named ranges; 此工作表有多个表,由命名范围引用; the tables and named ranges include the word 'template'. 表格和命名范围包括“模板”一词。

The template sheet is copied seven times into a new workbook. 模板工作表被复制七次到新工作簿中。 Immediately after being copied, the copied sheet is renamed for a day of the week. 复制后,立即将复制的工作表重命名为一周中的某一天。

I also need to rename the tables and named ranges too, but while I have discovered how to loop through and rename the tables by replacing 'template' with the appropriate day of the week, I cannot figure out how to do the same for the named ranges. 我还需要重命名表和命名范围,但是虽然我已经发现如何通过在一周中的适当日期替换“模板”来循环并重命名表,但我无法弄清楚如何对命名执行相同操作范围。

The named ranges begin as: 命名范围以:

AHSO_Tasks_Template Refers to table _01_AHSO_Tasks_Monday[AHSO Tasks] Scope is Monday AHSO_Tasks_Template参考 _01_AHSO_Tasks_Monday [AHSO任务] 范围是星期一

This is mirrored for the other six days of the week, and repeated many times with different collections of words replacing AHSO_Tasks. 这反映在一周中的其他六天,并且重复多次,不同的单词集合替换AHSO_Tasks。

I run this code to rename the named ranges according to the weekday contained in the name of the table they refer to: 我运行此代码,根据它们引用的表名称中包含的工作日重命名命名范围:

Sub Namedrangesloop()

Dim Nm As Name

'Loop through each named range in workbook
For Each Nm In ActiveWorkbook.Names

Dim oldrng As String
oldrng = Nm.Name

Dim rfto As String
rfto = Nm.RefersTo

Dim day As String
day = Mid(rfto, InStrRev(rfto, "_") + 1, InStr(rfto, "[") - InStrRev(rfto, "_") - 1)

Dim nwrng As String
nwrng = Replace(oldrng, "Template", day)

Nm.Name = nwrng

Next Nm

End Sub

This does work - for the above example the Name Manager will show the named range as Monday!AHSO_Tasks_Monday (so I would only then want to change the scope now the names have unique names, rather than AHSO_Tasks_Template seven times over). 这确实有效 - 对于上面的示例,名称管理器将命名范围显示为星期一!AHSO_Tasks_Monday(因此我只想更改范围,现在名称具有唯一名称,而不是AHSO_Tasks_Template七次)。

But when I save and re-open the new workbook, I get the message: 但是当我保存并重新打开新工作簿时,我收到消息:

Excel found unreadable content in filename.xlsx. Excel在filename.xlsx中找到了不可读的内容。 Do you want to recover the content of this workbook? 您想恢复此工作簿的内容吗? (etc). (等等)。

If I click yes, I then find when I open Name Manager that all the named ranges have been deleted! 如果我单击是,我会在打开名称管理器时发现所有已命名的范围都已删除! What can I do to change this? 我该怎么做才能改变这个?

I did think of an alternative but I'm also stuck on that too! 我确实想到了另一种选择,但我也坚持了这一点!

You have two options in your case... 你的情况有两种选择......

Option 1: Local worksheet-local named ranges. 选项1:本地工作表 - 本地命名范围。

In this case, your named tables use identical names but are differentiated by the sheetname. 在这种情况下,您的命名表使用相同的名称,但由sheetname区分。

Sunday!AHSO_Tasks or Monday!AHSO_Tasks Sunday!AHSO_TasksMonday!AHSO_Tasks

Clearly, renaming is no longer necessary with this option. 显然,使用此选项不再需要重命名。

Option 2: Rename each table as you copy 选项2:复制时重命名每个表

Your named tables remain global, but are renamed on each sheet as you copy in order to avoid confusion. 您的命名表保持全局,但在复制时在每个工作表上重命名,以避免混淆。

Option Explicit

Sub CreateWeekdaySheets()
    Dim srcWb As Workbook
    Dim dstWb As Workbook
    Dim tmplSh As Worksheet
    Dim daySh As Worksheet
    Dim weekDays() As String
    Dim day As Variant
    Dim tbl As ListObject
    Dim newName As String

    weekDays = Split("Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday", ",", , vbTextCompare)

    Set srcWb = ThisWorkbook
    Set dstWb = Workbooks.Add
    Set tmplSh = srcWb.Sheets("Template")

    For Each day In weekDays
        tmplSh.Copy After:=dstWb.Sheets(dstWb.Sheets.Count)
        Set daySh = ActiveSheet
        daySh.Name = CStr(day)
        For Each tbl In daySh.ListObjects
            newName = Replace(tbl.Name, "Template", day, , , vbTextCompare)
            tbl.Name = newName
        Next tbl
    Next day

End Sub

Both of these options should also work if your named ranges are not applied to Tables as well. 如果您的命名范围也未应用于Tables ,则这两个选项也应该有效。

I took PeterT 's solution, added one extra For Each / Next loop and got exactly what I wanted for that part of the project. 我采用了PeterT的解决方案,添加了一个额外的For Each / Next循环,并获得了我想要的项目部分。 But he deserves the credit! 但他值得赞扬!

Sub CreateWeekdaySheets()

Dim srcWb As Workbook
Dim dstWb As Workbook
Dim tmplSh As Worksheet
Dim daySh As Worksheet
Dim weekDays() As String
Dim day As Variant
Dim tbl As ListObject
Dim newName As String

Dim nm As Name 'Added by me

weekDays = Split("Monday-Tuesday-Wednesday-Thursday-Friday-Saturday-Sunday", "-", , vbTextCompare)

Set srcWb = ThisWorkbook
Set dstWb = Workbooks.Add
Set tmplSh = srcWb.Sheets("DSP Template")

For Each day In weekDays
    tmplSh.Copy After:=dstWb.Sheets(dstWb.Sheets.Count)
    Set daySh = ActiveSheet
    daySh.Name = CStr(day)
    For Each tbl In daySh.ListObjects
        newName = Replace(tbl.Name, "Template", day, , , vbTextCompare)
        tbl.Name = newName
    Next tbl
    For Each nm In dstWb.Names 'Added by me
        nm.Name = Replace(nm.Name, "Template", day) 'Added by me
    Next nm 'Added by me
Next day

ActiveWindow.TabRatio = 0.7

End Sub

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

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