Кнопки «Следующая» и «Предыдущая» в форме Excel VBA - PullRequest
0 голосов
/ 11 марта 2020

У меня есть форма, над которой я работаю, которая будет собирать набор данных. У меня есть текстовые поля, поля со списком и несколько кнопок выбора. Я пробовал несколько способов создать правильный код для go для предыдущей записи и для следующей записи, но я не могу заставить код работать. Я новичок в этом, но надеюсь, что кто-то может помочь. Я также могу отправить по электронной почте форму, если это поможет. Я попытался добавить TraverseData для очистки и облегчения ввода данных. Кажется, у меня работает следующая кнопка, но я продолжаю получать ошибку 1004 с предыдущей кнопкой, которая возвращается к TraverseData. Мой код ниже, и любая помощь очень ценится.

Private Sub cmdprevious_Click()
Dim nCurrentRow As Long
Do
nCurrentRow = nCurrentRow - 1
TraverseData (nCurrentRow)
Loop Until nCurrentRow = 1 Or Sheets(1).Cells(nCurrentRow, 1).Value = Me.txtname.Value

If nCurrentRow = 1 Then
MsgBox "This is the last entry.", , "Alert!"

cmdprevious.Enabled = False
  End If
End Sub

Private Sub Cmdnext_Click()
Dim nCurrentRow As Long
Do
    nCurrentRow = nCurrentRow + 1
    TraverseData (nCurrentRow)
Loop Until Sheet1.Cells(nCurrentRow, 1).Value = "" Or Sheet1.Cells(nCurrentRow, 1).Value = 
Me.txtname.Value


End Sub
Private Sub TraverseData(nCurrentRow As Long)

Me.txtname.Value = Sheet1.Cells(nCurrentRow, 1)
Me.txtposition.Value = Sheet1.Cells("nCurrentRow, 2")
Me.txtassigned.Value = Sheet1.Cells(nCurrentRow, 3)
Me.cmbsection.Value = Sheet1.Cells(nCurrentRow, 4)
 Me.txtdate.Value = Sheet1.Cells(nCurrentRow, 5)
 Me.txtjoint.Value = Sheet1.Cells(nCurrentRow, 7)
 Me.txtDAS.Value = Sheet1.Cells(nCurrentRow, 8)
Me.txtDEROS.Value = Sheet1.Cells(nCurrentRow, 9)
Me.txtDOR.Value = Sheet1.Cells(nCurrentRow, 10)
Me.txtTAFMSD.Value = Sheet1.Cells(nCurrentRow, 11)
Me.txtDOS.Value = Sheet1.Cells(nCurrentRow, 12)
Me.txtPAC.Value = Sheet1.Cells(nCurrentRow, 13)
Me.ComboTSC.Value = Sheet1.Cells(nCurrentRow, 14)
Me.txtTSC.Value = Sheet1.Cells(nCurrentRow, 15)
Me.txtAEF.Value = Sheet1.Cells(nCurrentRow, 16)
Me.txtPCC.Value = Sheet1.Cells(nCurrentRow, 17)
Me.txtcourses.Value = Sheet1.Cells(nCurrentRow, 18)
Me.txtseven.Value = Sheet1.Cells(nCurrentRow, 19)
Me.txtcle.Value = Sheet1.Cells(nCurrentRow, 20)

End Sub

Private Sub UserForm_Initialize()
Dim nCurrentRow As Long
Dim currentrow As Long
Dim lastrow As Long

txtname = Cells(nCurrentRow, 1)
txtposition = Cells(nCurrentRow, 2)
txtassigned = Cells(nCurrentRow, 3)
cmbsection = Cells(nCurrentRow, 4)
txtdate = Cells(nCurrentRow, 5)
txtjoint = Cells(nCurrentRow, 7)
txtDAS = Cells(nCurrentRow, 8)
txtDEROS = Cells(nCurrentRow, 9)
txtDOR = Cells(nCurrentRow, 10)
txtTAFMSD = Cells(nCurrentRow, 11)
txtDOS = Cells(nCurrentRow, 12)
txtPAC = Cells(nCurrentRow, 13)
ComboTSC = Cells(nCurrentRow, 14)
txtTSC = Cells(nCurrentRow, 15)
txtAEF = Cells(nCurrentRow, 16)
txtPCC = Cells(nCurrentRow, 17)
txtcourses = Cells(nCurrentRow, 18)
txtseven = Cells(nCurrentRow, 19)
txtcle = Cells(nCurrentRow, 20)

With UserForm1.cmbsection

    .AddItem "Law Office Superintendent"
    .AddItem "NCOIC, Legal Office"
    .AddItem "NCOIC, Training & Readiness"
    .AddItem "NCOIC, Military Justice"
    .AddItem "NCOIC, Adverse Actions"
    .AddItem "NCOIC, General Law"
    .AddItem "NCOIC, International Law"
    .AddItem "NCOIC, Civil Law"
    .AddItem "NCOIC, Other"
    .AddItem "Military Justice Paralegal"
    .AddItem "General Law Paralegal"
    .AddItem "International Law Paralegal"
    .AddItem "Civil Law Paralegal"
    .AddItem "Adverse Actions Paralegal"
    .AddItem "Other see notes"
  End With

  With UserForm1.ComboTSC

    .AddItem "B – initial upgrade to journeyman (5 level)"
    .AddItem "C – initial upgrade to craftsman (7 level; SSgt-select or above)"
    .AddItem "F – held prior 5 level; in upgrade to 5 level (retrainee)"
    .AddItem "G – held prior 7 level; in upgrade to 7 level (retrainee SSgt-select or above)"
    .AddItem "R – fully qualified"
    .AddItem "Other see notes"
 End With

 With UserForm1.txtdate
 txtdate.Value = Format(txtdate.Value, "dd/mm/yyyy")
 End With
 nCurrentRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
 TraverseData (nCurrentRow)

 End Sub

1 Ответ

0 голосов
/ 11 марта 2020

Добро пожаловать в SO.

Вы выполняете Dim nCurrentRow As Long, но вы не присваиваете ему никакого значения, поэтому значение по умолчанию равно 0.

И затем вы делаете al oop с nCurrentRow = nCurrentRow - 1 так nCurrentRow = -1.

Ваше условие для завершения l oop:

Loop Until nCurrentRow = 1 Or Sheets(1).Cells(nCurrentRow, 1).Value = Me.txtname.Value, и это условие никогда не может быть истинным.

В первый раз ваш Do...Loop выполняется, значение nCurrentRow равно -1, поэтому вы должны выполнить условие:

...Loop Until -1= 1 Or Sheets(1).Cells(-1, 1).Value = Me.txtname.Value...

Обратите внимание на деталь после Or. Он вызывает невозможную ячейку (строка -1, столбец 1). Это невозможно, поэтому VBA выдает ошибку 1004.

...