2014-09-05 82 views
-1

所以我一直在玩宏和按鈕,我試圖做一個按鈕,點擊它時,它會轉到上一張(他們是名字第一天,第二天,3等),所以我認爲這不應該太難!宏按鈕切換到上一張

這裏是我迄今爲止得到的代碼:當我點擊這個它說「下標越界」的按鈕,我得到一個錯誤

Sub Previous_Day() 

    Worksheets("Day " & (sheetNum - 1)).Visible = xlSheetVisible 
    Worksheets("Day " & (sheetNum - 1)).Activate 
    Worksheets("Day " & sheetNum).Visible = xlSheetHidden 

    sheetNum = sheetNum - 1 

End Sub 

,我不完全知道爲什麼,這正在發生的事情,我在全球範圍界定「當前工作簿」模塊中sheetNum,我將證明下面的代碼:

Public sheetNum As Integer 

Private Sub Workbook_Open() 

Dim thisSheet As Worksheet 
Dim sh As Worksheet 
Dim start As Worksheet 
Dim shName As String 
Dim lastSheet As String 



'name of the sheet template 
shName = "Food Diary Template.xltm" 
lastSheet = "Food Diary Last Entry.xltm" 

Set start = Worksheets(1) 
With start 
    If .Range("A1") = "" Then 
    .Range("A1") = Date 
    ActiveSheet.Shapes("Button 5").Select 
    Selection.Delete 
    .Range("B4").Select 
    End If 
End With 

Worksheets(Sheets.Count).Activate 
'#### I like to use variables for worksheets: 
Set thisSheet = ThisWorkbook.ActiveSheet 

'Insert sheet template 
With thisSheet 
    If .Range("A1") < Date Then 

     ActiveSheet.Buttons.Add(436.5, 104.25, 58.5, 18.75).Select 
     Selection.OnAction = "nextDay_Click" 
     ActiveSheet.Shapes("Button 1").Select 
     Selection.Characters.Text = "Button 1" 
     With Selection.Characters(start:=1, Length:=8).Font 
      .Name = "Calibri" 
      .FontStyle = "Regular" 
      .Size = 11 
      .Strikethrough = False 
      .Superscript = False 
      .Subscript = False 
      .OutlineFont = False 
      .Shadow = False 
      .Underline = xlUnderlineStyleNone 
     End With 
     ActiveSheet.Shapes("Button 1").Select 
     Selection.Characters.Text = "Next Day" 
     With Selection.Characters(start:=1, Length:=8).Font 
      .Name = "Calibri" 
      .FontStyle = "Regular" 
      .Size = 11 
      .Strikethrough = False 
      .Superscript = False 
      .Subscript = False 
      .OutlineFont = False 
      .Shadow = False 
      .Underline = xlUnderlineStyleNone 
     End With 
     .Range("B4").Select 

     Set sh = Sheets.Add(Type:="C:\Users\edawes\AppData\Roaming\Microsoft\Templates\" & lastSheet, _ 
          after:=Sheets(Sheets.Count)) 
     '#### Put today's date in the new sheet: 
     sh.Range("A1") = Date 
     sh.Name = "Day " & Worksheets.Count 
     'Worksheets(sh).Active 
     sh.Range("B4").Select 

     '#### Hide the old sheet 
     .Visible = xlSheetHidden 

    End If 
End With 

sheetNum = Sheets.Count 

End Sub 

,我想它,但它的sheetNum那我不知道該位工作正常關於,我做對了嗎?我希望它是打開文件時的總頁數,然後按下按鈕時(也有第二天的按鈕,但是當我得到這個工作時,這將非常簡單),它會更新sheetNum變量,以便它可以跟蹤和前一個按鈕可以繼續按下...

任何想法?

+0

不能在'Sub Previous_Day()'中使用'sheetNum = ThisWorkbook.Sheets.Count'嗎? – EvenPrime 2014-09-05 12:55:33

+0

@Thinkingcap這似乎沒有區別 – MrPhooky 2014-09-05 12:58:25

+0

看看這個:http://www.vbaexpress.com/kb/getarticle.php?kb_id=536 – EvenPrime 2014-09-05 13:01:41

回答

2

您可以解析名稱他的活動工作表,從日數減去1並激活前一頁。

Sub ButtonCode() 
    dy = CLng(Split(ActiveSheet.Name, " ")(1)) 
    dy = dy - 1 
    If dy = 0 Then Exit Sub 
    Sheets("Day " & dy).Activate 
End Sub 

因此,如果活性片的名稱爲第4天,然後第3天將被激活。