|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub 查询数据()
Dim wsSheet1 As Worksheet
Dim wsSource As Worksheet
Dim lastRow As Long
Dim i As Long
Dim searchName As String
Dim searchMonth As String
Dim found As Boolean
' 设置工作表对象
Set wsSheet1 = ThisWorkbook.Worksheets("Sheet1")
Set wsSource = ThisWorkbook.Worksheets("绩效考核表")
' 获取查询条件
searchName = wsSheet1.Range("D2").Value
searchMonth = wsSheet1.Range("I1").Value
' 初始化标志位
found = False
' 获取数据源最后一行
lastRow = wsSource.Cells(wsSource.Rows.Count, "A").End(xlUp).Row
' 遍历数据源
For i = 2 To lastRow
' 检查月份(A列)和姓名(C列)是否匹配
If wsSource.Cells(i, "A").Value = searchMonth And _
wsSource.Cells(i, "C").Value = searchName Then
' 填充数据到Sheet1
wsSheet1.Range("B2").Value = wsSource.Cells(i, "B").Value ' 部门
wsSheet1.Range("G4").Value = wsSource.Cells(i, "D").Value ' 项目支撑数量
wsSheet1.Range("G5").Value = wsSource.Cells(i, "E").Value ' 项目订单签约
wsSheet1.Range("G6").Value = wsSource.Cells(i, "F").Value ' 项目毛利率
found = True
Exit For
End If
Next i
' 提示未找到记录
If Not found Then
MsgBox "未找到" & searchName & "在" & searchMonth & "月的绩效考核记录!", vbExclamation
End If
End Sub
|
|