Generate catalogs with hyperlinks in batches and automatically update Excel


Automatically generate a table of contents with hyperlinks


提示:以下是本篇文章正文内容,下面案例可供参考

1. Knowledge points

Get.workbook macro table function use

  • Function: extract worksheet information
  • Syntax: get.workbook(type_num,name_text)
  • Explanation: get.workbook(type number, open worksheet name)
  • Note: The parameter type_num contains more codes, and we mainly use 1, which means "a horizontal array of text values, returning the names of all worksheets in the workbook". If the parameter name_text is omitted, it means the current active workbook.

INDEX()

  • Function: Returns the value or application of the value in the table or area.
  • Syntax: INDEX(array,row_num,[column_num]).
  • Explanation: INDEX(array or range, row number, column number).
  • Example "=INDEX(directory,ROW(A2))"
    INDEX refers to the previously defined macro function. The purpose of ROW(A1) is to make the second parameter of the INDEX function change accordingly, so that we can extract the names of the 1st, 2nd, 3rd, 4th...N worksheets in turn.

FIND()

  • Function: Position the text to be found to determine its position, and return the displacement.
  • Syntax: FIND(find_text, within_text, start_num)
  • Explanation: FIND (the text to be found, the cell where the text is located, from which character to start searching [optional, if omitted, the default is 1, start searching from the first one]).
  • For example: "=FIND("]",INDEX(directory,ROW(A2))", "]" ranks the sixth position of the character string in the cell, that is, 6 is obtained.
    insert image description here

REPLACE()

  • Function: replace the old string with the new string, and the replacement position and quantity are specified.
  • Syntax: REPLACE(old_text, start_num, num_chars, new_text)
  • Explanation: REPLACE (string to be replaced, starting position, number of replacements, new text)
  • Note: The fourth parameter is text and must be quoted.
  • For example: "=REPLACE(INDEX(Directory, ROW(A2)), 1, FIND("]",INDEX(Directory, ROW(A2))), "")" INDEX(Directory, ROW(A2)) is
    to Replacement string, 1 means start from the first string,
    FIND("]",INDEX(directory,ROW(A2)) means the number of replacements is 6, "" means replace with a null value, that is, start
    from the first to the sixth character is replaced with a null value.

HYPERLINK()

  • Function: Hyperlink function
    – ① Open a local folder
    – ② Open a local file (any extension)
    – ③ Jump to a cell in the current worksheet
    – ④ Jump to / a certain file in the current workbook Worksheet/Cell
    – ⑤ Jump to other workbook/Worksheet/Cell
    – ⑥ Jump to Web Page
    – ⑦ Jump to Reference Range of Name Manager
    – ⑧ Jump to VBA Code
    – ⑨ Reference the cell content to form a dynamic hyperlink
  • Syntax: HYPERLINK(LINK_LOCATION, [FRIENDLY_NAME])
  • Explanation: HYPERLINK (link address, displayed title)
  • Example:

① Open a local folder
and enter the formula: "=HYPERLINK("C:\Users\xxx\Desktop\Retire early", "Open the folder and retire early")"
insert image description here

② Open a local file (any extension)
and enter the formula: "=HYPERLINK("F:\Machine Learning\Machine Learning Practical HD PDF.pdf", "Open the test file pdf")"
insert image description here

③ Jump to a cell in the current worksheet
and enter the formula: "=HYPERLINK("#D2","Go to cell D2")"
注意:引用单元格地址的时候,是一定要在单元格地址前面加上“#”的,否则就是“无效引用”insert image description here

④ Jump to the current workbook/a worksheet/a cell and
enter the formula: "=HYPERLINK("#a!D2", "Go to D2 whose name is a in another sheet")"
insert image description here

⑤ Jump to another workbook/a worksheet/a cell and
enter the formula: " =HYPERLINK("[C:\Users\Zhou Xiaoting\Desktop\Early Retirement\Model Summary.xlsx]sheet1!D2", " D2 of other workbooks")"
注意:链接其他工作薄的话,就用“[工作薄的完整路径]”。
insert image description here

⑥ Jump to the webpage and
enter the formula: "=HYPERLINK("https://blog.csdn.net/sodaloveer/article/details/124866941?spm=1001.2014.3001.5502", "Link to other URLs")"
insert image description here

⑦ Jump to the reference range of the name manager
Enter the formula: "=HYPERLINK("#test name", "cell referenced by name")"
insert image description here

⑧ Jump to the VBA code
Enter the formula: "=HYPERLINK("#test code", "VBA project")"

⑨ Quote the cell content to form a dynamic hyperlink
Enter the formula: "=HYPERLINK(CONCATENATE("#",A3,"!",A4),A5)"
insert image description here

Two, examples

  • Goal: Column A of the directory is the worksheet name, now the worksheet name needs to be extracted to column A

Step 1: Define the name

1. Click [Define Name] in the [Formula] option

insert image description here
2. Pop up the [New Name] box, enter "directory" in [Name], use the macro table function in [Reference Location], and enter "=GET.WORKBOOK(1)". After clicking [OK], you can see the newly created record in the [Name Manager].

insert image description here

Step Two: Extract All Sheet Names

1. Use the formula: "=INDEX(Category,ROW(A2))" to extract the names of all worksheets named "Catalog".
insert image description here
2. Because the name of the extracted worksheet now has the name of the workbook, it is further optimized to achieve the effect of only retaining the name of the worksheet. Enter the formula in cell A2: "=REPLACE(INDEX(Category,ROW(A2)),1,FIND("]",INDEX(Catalogue,ROW(A2))),"")", you can extract it after pulling down Output the worksheet name without workbook.
insert image description here

Step 3: generate hyperlinks in batches

Use the HYPERLINK function, enter the formula in cell B2: "
=HYPERLINK("#"&A2&"!A1",A2) ", and pull down. Click the text that generates the hyperlink to jump to the corresponding worksheet.
insert image description here

Step 4: Set the "return to directory" hyperlink

1. Click the [A model] worksheet name, press the "shift" key to select all worksheets except [Category], and enter the formula in A1 of one of the worksheets: "=HYPERLINK("#Category!A1", "Return to Table of Contents")", generate a hyperlink back to the [Table of Contents] worksheet.
insert image description here

Guess you like

Origin blog.csdn.net/sodaloveer/article/details/124946570