通過

2013-12-16 47 views
0

我已經創建了在Excel VBA用戶窗體,我將使用從表查看和更新​​某些記錄用戶窗體上的Excel VBA記錄導航我:通過

UserForm1 http://im35.gulfup.com/Ay43Z.png

似乎我不能要弄清楚如何使它在狀態欄中有「Y」的工作表中的記錄之間導航。

Sheet1 http://im36.gulfup.com/cq6CN.png

我需要的用戶能夠通過用戶窗體編輯點評列他觀察,這樣的改變被保存在表中的記錄。用戶窗體上其餘的文本框被設置爲鎖定,以便它們只能顯示數據。

我現在面臨的問題是,我似乎只能夠在所有記錄之間循環。我需要的是在具有「Y」作爲其「狀態」的之間導航。此外,我似乎無法弄清楚如何在表單上的評論框中進行的更改保存在表單上。

任何幫助將非常感謝!

編輯:

下面是我對上一個按鈕的代碼:

If CurRecord = 0 Then CurRecord = 1 

    With ws 
     For i = 1 To (CurRecord - 1) 
      If Not .Range("G" & i).Value = "X" Then 
       TextBox1.Text = .Range("A" & i).Value 
       TextBox2.Text = .Range("B" & i).Value 
       ' 
       '~~> And So on load the rest 
       ' 

       CurRecord = i 
       Exit Sub 
      End If 
     Next i 

     If (i - 1) = lRow Then 
      MsgBox "End of record reached" 
     End If 
    End With 

當我使用這一點,不斷跳躍到第一條記錄。

我也試過:

For i = 1 To (CurRecord - 1)

但是,當它到達的第一條記錄這給了我一個「超出範圍」的錯誤。

不知道我要去哪裏錯了?

+1

我可以幫你,如果您可以在www.wikisend.com上傳樣本文件並在此處分享鏈接。我非常懶惰地自己創建一個樣本:p –

+0

@SiddharthRout很抱歉,遲到的迴應是,我實際上是在假日出城!無論如何,我上傳文件的請求:http://wikisend.com/download/461380/Book1.xlsm – CaptainABC

+0

@SiddharthRout任何建議? – CaptainABC

回答

2

這裏是Next按鈕的一個非常基本的例子。現在,我相信你一定能夠代碼的Previous按鈕同樣:)

Dim ws As Worksheet 
Dim lRow As Long 
Dim CurRecord As Long 

'~~> Load the first record 
Private Sub UserForm_Initialize() 
    Set ws = ThisWorkbook.Sheets("Sheet1") 

    With ws 
     lRow = .Range("G" & .Rows.Count).End(xlUp).Row 

     For i = 2 To lRow 
      If .Range("G" & i).Value = "Y" Then 
       TextBox1.Text = .Range("A" & i).Value 
       TextBox2.Text = .Range("B" & i).Value 
       ' 
       '~~> And So on load the rest 
       ' 

       CurRecord = i 
       Exit For 
      End If 
     Next i 
    End With 
End Sub 

'~~> Next Button 
Private Sub CommandButton3_Click() 
    If CurRecord = 0 Then CurRecord = 1 

    With ws 
     For i = (CurRecord + 1) To lRow 
      If .Range("G" & i).Value = "Y" Then 
       TextBox1.Text = .Range("A" & i).Value 
       TextBox2.Text = .Range("B" & i).Value 
       ' 
       '~~> And So on load the rest 
       ' 

       CurRecord = i 
       Exit Sub 
      End If 
     Next i 

     If (i - 1) = lRow Then 
      MsgBox "End of record reached" 
     End If 
    End With 
End Sub 

從評論跟帖

試試這個(未經測試)

'~~> Previous Button 
Private Sub CommandButton2_Click() 
    If CurRecord = 2 Then 
     MsgBox "Begining of record reached" 
     Exit Sub 
    ElseIf CurRecord = 0 Then 
     CurRecord = 3 
    End If 

    With ws 
     For i = (CurRecord - 1) To 2 Step -1 
      If .Range("G" & i).Value = "Y" Then 
       TextBox1.Text = .Range("A" & i).Value 
       TextBox2.Text = .Range("B" & i).Value 
       ' 
       '~~> And So on load the rest 
       ' 

       CurRecord = i 
       Exit Sub 
      End If 
     Next i 

     If i = 2 Then 
      MsgBox "Begining of record reached" 
     End If 
    End With 
End Sub 
+0

非常感謝!剛剛有機會嘗試它,它效果很好。只是由於某種原因,我很難讓「上一步」按鈕正常工作。我嘗試了'For I =(CurRecord - 1)toLRow'和'For I = lRow To(CurRecord + 1)'和'lRow To(CurRecord - 1)',但它似乎不起作用。不知道我要去哪裏錯了? – CaptainABC

+0

可悲的是仍然在編寫「以前的」buttun編碼會很感激你的幫助! – CaptainABC

+0

我現在已經試過'我= 1到(CurRecord - 1)',但這只是跳回到第一行。另外嘗試'對於我 - (CurRecord - 1)到(CurRecord - 1)',但使用它會卡住顯示一條記錄。請幫助! – CaptainABC

0

如果您對「Y」進行自動過濾,那麼導航將只訪問可見的行(其中包含「Y」)。簡單地說:

ActiveSheet.Range("$A1:$G$1").AutoFilter Field:=7, Criteria1:="Y"