2014-10-17 59 views
0

首先,請允許我提前感謝您在這方面的幫助。Excel VBA - 如何在表格中的特定行開始輸出

這似乎很簡單,但我一直無法弄清楚。該代碼僅列出特定工作表之間的所有工作表名稱。我將一些代碼混合在一起,並在網上進行了研究,一切正常,但只有一種。我需要的最後一件事是能夠從特定行開始輸出。

當前,輸出從單元格A2開始。我如何認爲代碼是從特定單元格(如單元格A3)開始輸出的?

我下面的代碼:

Sub Summary_All_Worksheets() 
Dim i As Long 
Dim Low As Long, High As Long, Skip As Long 
Dim Newsh As Worksheet 
Dim Basebook As Workbook 

With Application 
    .Calculation = xlCalculationManual 
    .ScreenUpdating = False 
End With 

Set Basebook = ThisWorkbook 
Set Newsh = Basebook.Worksheets("Client Test") 
Newsh.Columns("a").ClearContents 

Low = Basebook.Worksheets("front").Index 
Middle = Basebook.Worksheets("P5GBack").Index 
High = Basebook.Worksheets("back").Index 

If High < Low Then 
    i = Low 
    Low = High 
    High = i 
End If 

With Newsh.Cells(Rows.Count, 1) 
    For i = Low + 1 To Middle - 1 
    .End(xlUp).Offset(1, 0) = Basebook.Worksheets(i).Name 
    Next i 

    For i = Middle + 1 To High - 1 
     .End(xlUp).Offset(1, 0) = Basebook.Worksheets(i).Name 
    Next i 

    With Application 
     .Calculation = xlCalculationAutomatic 
     .ScreenUpdating = True 
    End With 
End With 

End Sub 

回答

-1

你的這部分代碼決定了細胞將開始填充(我已經添加評論你的代碼):

With Newsh.Cells(Rows.Count, 1) 'count all the rows in the sheet, and 
'make everything relative to that row, in column 1. (Basically, you're 
'selecting the very bottom cell in column A.) 
For i = Low + 1 To Middle - 1 
    .End(xlUp).Offset(1, 0) = Basebook.Worksheets(i).Name 'Move up 
'from that bottom-most cell until you find a cell with a value (like 
'you were pressing ctrl+up). Go down one row (this is the 'Offset(1,0)' 
' part), and in that cell, enter the worksheet name. 
Next i 

雖然這種方式是相當穩定並避免覆蓋任何數據,如果您希望能夠將代碼放入一個新的地方開始您的列表,您可以像這樣做(從單元格E5開始):

NewSh.Cells(5,5).Activate 'start in E5 on NewSh 
For i = Low + 1 To Middle - 1 
    ActiveCell = Basebook.Worksheets(i).Name 'put the name in the active cell 
    NewSh.Hyperlinks.Add Anchor:=ActiveCell, Address:="", SubAddress:= ActiveCell.Value & "!A1",  TextToDisplay:=ActiveCell.Value' 
    ActiveCell.Offset(1, 0).Activate 'activate the next cell down 
Next i 

還有其他方法可以做到這一點,最好的方法是由您的情況決定的(並且我急於添加 - 在For循環的兩個循環中進行上述更改。 ;-)

+0

這將把數據放在ActiveSheet上,這可能是也可能不是所需的表單(OP的代碼正確避免了這個缺陷)。也沒有必要使用ActiveCell這個 – 2014-10-18 07:46:38

+0

非常感謝你,它的工作非常好。非常感激! – D100 2014-10-20 18:25:14

+0

對不起,還有一個問題。我如何超鏈接上面表單的所有名字?再次感謝! – D100 2014-10-20 19:17:25

相關問題