|
'声明变量J和Z为公共变量,用于存储符合条件的行号
Public j As Integer
Public z As Integer
Private Sub CommandButton4_Click()
Unload Me
UserForm2.Show '调用窗体
End Sub
Private Sub cxtj_Change()
End Sub
'初始化用户窗体子过程代码
Private Sub UserForm4_Initialize()
'声明变量存储需要的工作表
Dim temp As Worksheet
Set temp = Worksheets("Sheet1")
'声明变量存储当前工作表的列数
Dim Col1 As Integer
Col1 = temp.Range("A1").CurrentRegion.Columns _
.Count
'定义数组InQu(),用于存储工作表的字段名称
Dim InQu(21) As String
'使用循环语句将字段名称赋给数组变量
For i = 0 To Col1 - 1
InQu(i) = temp.Cells(1, i + 1)
Next i
'将数组值赋给下拉列表清单
cxtj.List = InQu
cxtj.ListIndex = 0
'初始化资产变更选项组(框架控件)中的各控件值
cxwj.Text = ""
cpbh.Text = ""
cpmc.Text = ""
mjh.Text = ""
gxmc.Text = ""
ylsx.Value = ""
ylxx.Value = ""
bz.Value = ""
cj.Value = ""
yjlx.Value = ""
End Sub
'编写查询按钮对应的子过程代码
Private Sub CX_Click()
'为行数变量赋初值
j = 2
'调用自定义查询CXDM()函数
CXDM (j)
End Sub
'自定义查询函数CXDM()的代码
Function CXDM(j As Integer)
Dim Sht As Worksheet
Set Sht = Worksheets("sheet1")
Dim rowNum As Integer
Dim Col2 As Integer
'获取指定工作表的行数与列数
rowNum = Sht.Range("A1").CurrentRegion. _
Rows.Count + 2
Col2 = Sht.Range("A1").CurrentRegion. _
Columns.Count
Dim i As Integer
For i = 1 To Col2
'判断确定用户选中字段的列号
If Sht.Cells(1, i) = Inq.Value Then
'查询满足用户条件的记录
Do While j < rowNum
'判断用户输入的条件是否存在
If Cells(j, i) = cxwj.Value Then
'存在,则将查找到的行数赋值给公共变量Z
z = j
'并调用显示记录函数ShowRecord1()
ShowRecord1 j
Exit For
End If
j = j + 1
Loop
End If
Next i
End Function
'显示符合查询条件的记录
Function ShowRecord1(a As Integer)
cpbh.Text = Cells(a, 2)
cpmc.Text = Cells(a, 3)
mjh.Text = Cells(a, 8)
gxmc.Text = Cells(a, 4)
ylsx.Value = Cells(a, 7)
ylxx.Value = Cells(a, 6)
bz.Value = Cells(a, 9)
cj.Value = Cells(a, 1)
yjlx.Value = Cells(a, 5)
End Function
'设置下一条按钮对应的事件代码
Private Sub Next1_Click()
'符合查询条件的下一条记录
j = z + 1
'调用查询CXDM()函数
CXDM (j)
End Sub
'设置上一条按钮对应的事件代码
Private Sub Ship1_Click()
'符合条件的上一条记录
j = z - 1
'调用CXDM1自定义函数
CXDM1 (j)
End Sub
Function CXDM1(j As Integer)
Dim Sht As Worksheet
Set Sht = Worksheets("sheet1")
Dim rowNum As Integer
Dim Col2 As Integer
'获取指定工作表的行数与列数
rowNum = Sht.Range("A1").CurrentRegion. _
Rows.Count + 2
Col2 = Sht.Range("A1").CurrentRegion. _
Columns.Count
Dim i As Integer
For i = 1 To Col2
'判断确定用户选中字段的列号
If Sht.Cells(1, i) = cxtj.Value Then
'查询满足用户条件的记录
Do While j > 1
'判断用户输入的条件是否存在
If Cells(j, i) = InquBox1.Value Then
'存在,则将查找到的行数赋值给公共变量Z
z = j
'并调用显示记录函数ShowRecord1()
ShowRecord1 j
Exit For
End If
j = j - 1
Loop
End If
Next i
End Function
'设置保存按钮对应的事件代码
Private Sub CommandButton2_Click()
Cells(z, 1) = cj.Value
Cells(z, 2) = cpbh.Text
Cells(z, 3) = cpmc.Text
Cells(z, 4) = gxmc.Text
Cells(z, 5) = yjlx.Value
Cells(z, 6) = ylxx.Value
Cells(z, 7) = ylsx.Value
Cells(z, 8) = mjh.Text
Cells(z, 9) = bz.Value
End Sub
|
|