2013-02-25 46 views
0

我已經記錄並拋光了下面的宏,它應該創建一個額外的工作表,並在原始工作表中的每個表的起始單元格上創建一個名爲「All_tables」的超文本鏈接。在這張表中,每個表都由一個散列符號(#)分開。 See a screenshot如何在VB中創建正確的循環(宏)

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 

現在我想將其放到一個循環會重演十(或更多)次。我試圖使用散列符號作爲程序的參考點來找出他應該指向哪個單元格的超鏈接。這裏是結果:

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 

而且它不起作用。我對宏和VB編程完全陌生,所以如果你至少能向我展示方向,我會非常高興。我的方法是完全錯誤的嗎?

謝謝你這麼多

+2

當你說*它不起作用*,'1.'這是否意味着代碼有錯誤?或'''你的代碼是否返回任何內容或者什麼都不做,但是沒有編譯錯誤? – bonCodigo 2013-02-25 16:55:30

+0

這篇文章似乎是錯誤的:ActiveSheet.Hyperlinks.Add Anchor:=選擇,地址:=「」,SubAddress:= _ 「All_Tables!&rMyCell」 我認爲語法是錯誤的。我不知道如何正確地將變量與固定位置結合... – DDEX 2013-02-25 18:30:34

+0

向變量添加變量時,它需要在引號之外。試試「All_Tables!」 &rMyCell – Zaider 2013-02-25 19:44:47

回答

1

我不知道究竟在何處您希望您的超鏈接指向,但這應該讓你一個良好的開端。需要注意的事項:

  • 請勿使用SelectSelection聲明。它們速度慢,會產生不良影響。取而代之的是使用非常明確的語句,它不依賴於光標位置,但可以評估事物所處位置的絕對位置。
  • 使用範圍對象的FindFindNext方法來查找字符串。當FindNext找不到更多時,它返回nothing。很好檢查,而不是做你的table_number循環。

更新

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 

見它是如何工作的,從那裏的舉動。

+0

謝謝,這是很好的開始。 All_tables工作表應該成爲ActiveSheet中由散列符號消除的表格(即超鏈接列表)的單獨列表。簡而言之,我需要用其他方式:-)但無論如何,您的代碼幫助我理解宏如何工作。謝謝! – DDEX 2013-02-26 08:34:09

+0

偉大的:)不客氣,不要忘記接受,如果它幫助你 – Brad 2013-02-26 13:55:48