简体   繁体   English

如何在VB中创建适当的循环(宏)

[英]How to create proper loops in VB (macro)

I have recorded and polished the following macro which should create an extra sheet with hypertext links pointing on a starting cell of each table within the original sheet called "All_tables". 我已经记录并完善了以下宏,该宏将创建一个额外的工作表,该工作表具有指向原始工作表中称为“ All_tables”的每个表的起始单元格的超文本链接。 In this sheet, every single table is separated by a hash symbol (#). 在此工作表中,每个表都由一个井号(#)分隔。 See a screenshot : 看截图

Sub Create_list_of_tables()

Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "list of tables"

ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
    "All_Tables!A22", TextToDisplay:="some variable pointing at the table name"
Range("A2").Select
End Sub

Now I would like to put it into a loop which would repeat itself ten (or more) times. 现在,我想将其放入一个循环中,该循环将自身重复十次(或更多次)。 I tried to use the hash symbol as a reference point for a program to find out at which cell he should point the hyperlink. 我试图将哈希符号用作程序的参考点,以找出他应将超链接指向哪个单元。 Here is the result: 结果如下:

Sub Create_list_of_tables()    
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "list of tables"

Const cStrDivider As String = "#"

Dim rMyCell As Range
Dim table_number As Long
table_number = 0


Do Until table_number = 10
Set rMyCell = Range("cStrDivider").Select
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
    "All_Tables!&rMyCell", TextToDisplay:="some variable pointing at the table name"
ActiveCell.Offset(1, 0).Select
table_number = table_number + 1
Loop

End Sub

And it doesn't work. 而且它不起作用。 I am totally new to macro and VB programming so I'd be really happy if you could at least show me the direction. 我对宏和VB编程完全陌生,所以如果您至少可以向我展示方向,我将非常高兴。 Is my approach is completely wrong? 我的方法是完全错误的吗?

Thank you so much 非常感谢

I'm not sure exactly where you want your hyperlink pointing but this should get you a good start. 我不确定您要指向超级链接的确切位置,但这应该可以为您提供一个良好的开端。 Things to look out for: 要注意的事情:

  • Don't use Select or Selection statements. 不要使用SelectSelection语句。 They are slow and can produce undesirable effects. 它们很慢并且会产生不良影响。 Instead use very explicit statements that do not depend on cursor position but rater the absolutle position of where you know things are. 而是使用不依赖于光标位置的非常明确的语句,而是对您所知道的事物的绝对位置进行评级。
  • Use the Find and FindNext method of a range object to locate strings. 使用范围对象的FindFindNext方法查找字符串。 When FindNext can't find anything more it returns nothing . FindNext找不到其他东西时,它nothing回报。 Good to check for instead of doing your table_number loop. 好检查而不是执行table_number循环。

updated 更新

Sub Create_list_of_tables()

    Const cStrDivider As String = "#"

    Dim sht As Worksheet, rMyCell As Range, rSearchRange As Range
    Dim testSht As Worksheet, firstMyCell As Range

    Set sht = ActiveSheet

    On Error Resume Next
    Set testSht = ActiveWorkbook.Sheets("All_Tables")
    If Err.Number <> 9 Then
        Application.DisplayAlerts = False
        testSht.Delete
        Application.DisplayAlerts = True    'important to set back to true!
    End If
    On Error GoTo 0

    ActiveWorkbook.Sheets.Add After:=Sheets(Sheets.Count)
    ActiveWorkbook.Sheets(Sheets.Count).Name = "All_Tables"

    Set rSearchRange = sht.Range("A:A")

    'do initial "Find"
    Set rMyCell = rSearchRange.Find(cStrDivider)
    Set firstMyCell = rMyCell

    Do
        sht.Hyperlinks.Add Anchor:=rMyCell.Offset(0, 1), Address:="All_Tables!" & rMyCell.Address, _
            TextToDisplay:="Link"

        'get the next "MyCell" to use from the master range to search
        Set rMyCell = rSearchRange.FindNext(rMyCell)
        'increment your table counter (if you want to do this you can still
        table_number = table_number + 1
        Debug.Print firstMyCell.Address
        Debug.Print rMyCell.Address
    Loop While firstMyCell.Address <> rMyCell.Address

End Sub

See how that works an move on from there. 看看如何从那里继续前进。

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

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