使用表中的值dynamic地在excel vba中build立名称pipe理器条目

我想根据表格的第一个和最后一个单元格在名称pipe理器中创build一个新条目。

我在当前工作表的macros中有这个硬编码,但是我将有很多不同大小的工作表。

ActiveWorkbook.Names.Add Name:="TitleRegion1.a6.h48.4", RefersToR1C1:= "=pw!R142C1" 

这是我的情况:

我的工作表有一个表 – 带有标题的Table3,它跨越单元格A6到H49我希望我的入口具有名称“TitleRegion1.a6.h49.4”

这些是我的变数。

 TitleRegion = just a string, 1 = refers to the 1st table in the current worksheet, a6 = start cell of table, h49 = end cell of table, worksheet 4 in workbook 

这里是使用loggingmacros的macros

 Sub Macro1() Range("Table3[[#Headers],[Data Source]]").Select ActiveWorkbook.Names.Add Name:="TitleRegion1.a6.h48.4", RefersToR1C1:= _ "=Table3[[#Headers],[Data Source]]" End Sub` 

[数据源] – 是a6的值我不知道从哪里开始? 我是Excel VBA编程的新手。

你可以试验下面的代码( 不要使用工作文件,直到你看到它是如何工作的

  1. 打开一个新文件
  2. 打开VBA编辑器(Alt + F11)
  3. 插入一个新的模块。 从菜单:插入 – >模块
  4. 将代码粘贴到新模块中
  5. 点击此代码中的任意位置并运行它(F5)

这是它的作用:

  • 定义所有有表格的表格范围,定义为“A6:H49”(第3行)
  • 定义不应有表格的工作表列表:“| Sheet2 | Sheet3 | … |” (第4行)
  • 遍历每张表,如果不在排除列表上,则在其上创build一个新表
  • 新的表格名称将与您的要求类似:Title1.A6_H49.1

根据表名,Excel将在创build表时自动生成一个新名称

这里是代码(更新):

 Option Explicit Sub makeTables() Dim excludedSheets As String, sh As Worksheet, tbl As ListObject, i As Long Dim tblRngList As Object, headerList As Object, indx As Long, rng As String Set tblRngList = CreateObject("Scripting.Dictionary") Set headerList = CreateObject("Scripting.Dictionary") With tblRngList .Add Key:=1, Item:="A6:H49" 'Sheet 1: Key:=1 -> sheet index .Add Key:=2, Item:="A6:H22" 'Sheet 2: "A6:H22" -> Tbl region on Sheet2 .Add Key:=3, Item:="" 'Sheet 3: No table End With With headerList .Add Key:=1, Item:="Header 1" 'Headers must be unique .Add Key:=2, Item:="Header 2" .Add Key:=3, Item:="Header 3" .Add Key:=4, Item:="Header 4" .Add Key:=5, Item:="Header 5" .Add Key:=6, Item:="Header 6" .Add Key:=7, Item:="Header 7" .Add Key:=8, Item:="Header 8" 'Should match total cols defined in ranges End With '"A6:H49" = 8 columns With ActiveWorkbook 'Current Excel file For Each sh In .Worksheets 'Iterate through all sheets of current file With sh 'Current sheet: Sheet1, Sheet2, Sheet3 indx = .Index 'Current sheet index: 1, 2, 3 rng = tblRngList(indx) 'Current table range: "A6:H49", "A6:H22", "" If Len(rng) > 0 Then 'If tbl rng is not empty ("") If .ListObjects.Count > 0 Then 'If any previous tables exist For Each tbl In sh.ListObjects 'Go through each one tbl.Delete 'and delete it Next End If 'Create table --------------------------------------------------- Set tbl = .ListObjects.Add(xlSrcRange, sh.Range(rng), , _ xlYes, , "TableStyleMedium5") 'Set Table name ------------------------------------------------- tbl.Name = "Title" & .ListObjects.Count & "." & _ tblRngList(indx) & "." & indx 'Set Headers ---------------------------------------------------- With tbl For i = 1 To .HeaderRowRange.Count .HeaderRowRange(i) = headerList(i) Next End With End If End With Next End With End Sub 
  • 在具有3个默认图纸的新文件中,它将在Sheet1上创build一个表格,在Sheet2上创build一个表格

  • 对于超过3张使用这样的行添加更多的项目列表:

    .Add Key:=7, Item:="A1:Z999"

  • 其中Key是工作表的索引,而item是该工作表上的表的范围

  • 您可以将“TableStyleMedium5”更改为“表格工具” – >“表格样式”部分中的任何样式

  • 将鼠标放在任何可用样式上以查看其名称(从名称中删除所有空格):

    TableStyles

运行后,检查名称pipe理器以获取与此类似的列表(取决于排除的工作表)

NameManager