2017-10-06 120 views
0

嗨,大家好我正在使用它的工作正常的排序代碼,直到它沒有經過一段時間的閱讀,雖然代碼和測試我發現錯誤的原因是因爲選擇,據我所知只工作時,你可以選擇工作表首先帶有一個可見的工作表,這裏出現我的問題我需要代碼與隱藏工作簿一起工作,任何人都可以幫助獲得代碼與隱藏工作簿,因爲我只知道這個代碼。謝謝!Excel VBA隱藏工作簿時的排序工作表

什麼這裏的代碼沒有爲B柱之後的排序,但它有第1排的頭部

Set tsheet = ThisWorkbook.Sheets("Courses_tee") 

    ThisWorkbook.Sheets("Courses_tee").Range("B2").CurrentRegion.Select 
    Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlGuess, _ 
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ 
    DataOption1:=xlSortNormal 
+2

有沒有聽說過標點符號? – Aganju

+0

嗯,我很抱歉阿甘居,我不是英語,並沒有任何線索把他們放在哪裏。它一定是如此難以閱讀它 –

回答

2

更改此:

ThisWorkbook.Sheets("Courses_tee").Range("B2").CurrentRegion.Select 
Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlGuess, _ 
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ 
    DataOption1:=xlSortNormal 

對此:

Set tsheet = ThisWorkbook.Sheets("Courses_tee") 
tSheet.Range("B2").CurrentRegion.Sort Key1:=Range("B2"), _ 
    Order1:=xlAscending, Header:=xlGuess, _ 
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ 
    DataOption1:=xlSortNormal 

你不需要和你不應該SelectActivate你可以直接在對象上操作的東西。 SelectActivate本質上是一樣的擊鍵/鼠標點擊/用戶行爲 - 你不需要Select代碼中的一個範圍,如果你知道這是什麼範圍,即:

Range("A1:A100").Select 
Selection.Copy Destination:=Range("B1") 

可以成爲簡單:

Range("A1:A100").Copy Destination:=Range("B1") 

等等

更多信息:

How to avoid using Select in Excel VBA

+0

嗨大衛感謝您的代碼和解釋。非常感謝。 –

0

你並不需要選擇

tSheet.Range("B2").CurrentRegion.Select 
Selection.Sort Key1:=tSheet.Range("B2"), Order1:=xlAscending, Header:=xlGuess, _ 
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ 
DataOption1:=xlSortNormal