我有一组4行数据,当我点击功能区中的按钮时,我需要使用vb在userform excel中自动显示第一行数据。稍后点击下一行,它需要显示下一行的完整数据。
例如,
stud_id stud_name age gender
1 a 20 M
2 b 22 M
3 c 25 F
4 d 22 F
Public ncurretnrow As Long
Sub Userform_Initialize()
ncurrentrow = Sheet3("2017").Cells(Rows.Count, 1).End(xlDown).Offset(1, 0).Row
traversedata (ncurrentrow)
End Sub
Public Sub traversedata(nrow As Long)
Me.stud_id.Value = ws.Cells(nrow, 1).Value
Me.stud_name.Value = ws.Cells(nrow, 2).Value
Me.age.Value = ws.Cells(nrow, 2).Value
If Optionyes = True Then
Me.genderm.Value = ws.Cells(nrow, 3).Value
Else
Me.genderf.Value = ws.Cells(nrow, 3).Value
End If
答案 0 :(得分:0)
请参阅下面的代码(User_Form模块中的Sub
}。
Public ws As Worksheet
Private Sub UserForm_Initialize()
Dim HeaderRng As Range
Dim ncurretnrow As Long
' set the worksheet object
Set ws = ThisWorkbook.Sheets("2017")
Set HeaderRng = ws.Cells.Find(what:="stud_id") ' look for the header (below it you will find the first row with data)
If Not HeaderRng Is Nothing Then
ncurretnrow = HeaderRng.Row + 1
Else
MsgBox "Unable to find stud_id header", vbCritical
Exit Sub
End If
traversedata ncurretnrow
End Sub
Sub traversedata(nrow As Long)
With Me
.stud_id.Value = ws.Cells(nrow, 1).Value
.stud_name.Value = ws.Cells(nrow, 2).Value
.age.Value = ws.Cells(nrow, 3).Value
If Optionyes = True Then
.genderm.Value = ws.Cells(nrow, 4).Value
Else
.genderf.Value = ws.Cells(nrow, 4).Value
End If
End With
End Sub