2013-02-25 1 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 

지금 10 개 (또는 그 이상)의 시대가 반복 것 루프에 넣어 싶습니다. 하이픈 링크를 가리켜 야하는 셀을 알아 내기 위해 프로그램의 참조 포인트로 해시 기호를 사용하려고했습니다. 결과는 다음과 같습니다.

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.' 코드에 오류가 있습니까? 아니면'2.' 당신의 코드는 아무 것도 반환하지 않거나 아무런 컴파일 오류도하지 않습니까? – bonCodigo

+0

이 조각은 틀린 것 같습니다 : ActiveSheet.Hyperlinks.Add 앵커 : = 선택, 주소 : = "", SubAddress : = _ "All_Tables! & rMyCell" 구문이 잘못되었다고 생각합니다. 변수를 고정 위치와 적절하게 결합하는 법을 모르겠습니다 ... – DDEX

+0

문자열에 변수를 추가 할 때는 따옴표 밖에 있어야합니다. "All_Tables!"를 시도하십시오. & rMyCell – Zaider

답변

1

너무 감사드립니다. 주의 사항 :

  • Select 또는 Selection 문을 사용하지 마십시오. 그것들은 느리며 바람직하지 않은 결과를 초래할 수 있습니다. 대신 커서 위치에 의존하지 않고 상황을 알 수있는 절대 위치를 비교하는 매우 명시적인 명령문을 사용하십시오.
  • 범위 개체의 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

+0

위대한 :) 당신은 환영합니다. 그리고 그것이 당신을 도왔다면 받아들이는 것을 잊지 마십시오. – Brad

관련 문제