PrivateSub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel AsBoolean) '定义双击事件 Dim getExit AsInteger getExit = 0 Application.ScreenUpdating = False'屏幕不更新 ForEach im In ActiveSheet.Shapes '删除名字为测试的图片 If im.Name = "测试"Then im.Delete EndIf Next
Dim getValue AsString'单元格值 Dim Work1 As Workbook '定义工作Excel变量 Dim Work2 As Workbook '定义工作Excel变量 top = Target.top ' 获取当前活动单元格的高度
Set Work1 = CreateObject("C:\Users\Hywell\Desktop\test.xlsx") '创建Worksheet对象 Set Work2 = CreateObject("C:\Users\Hywell\Desktop\test2.xlsx") '创建Worksheet对象
Allsheets = Array(Work1, Work2) '定义工作Excel数组 getValue = Range(Target.Address) '获取当前活动单元格地址的内容 If Target.Column = 6Then'判断是否为F列 If getValue = ""Then MsgBox "数据为空!" Else ForEach i In Allsheets '遍历工作Excel数组 If getExit = 0Then Call getResult(i, getValue, getExit, top) '调用getResult函数 获取所需数据区域 Else ExitFor EndIf Next If getExit = 0Then MsgBox "找不到对应明细表!" EndIf EndIf EndIf Set Work1 = Nothing'清空Work1对象 Set Work2 = Nothing'清空Work2对象 EndSub
Sub getResult(Worksheet, getValue, getExit, top) '获取数据(getValue)区域 Dim getInt AsInteger'开始行数 Dim getInt2 AsInteger'最后行数 Dim getNum AsInteger'用于退出Fox循环 Dim getWork AsInteger'获取工作的sheet Dim Time AsString'定义定位标识符 getInt = 0'初始化 getInt2 = 0'初始化 getNum = 0'初始化 Time = "日期"'初始化
For y = 1To Worksheet.Sheets.Count '遍历工作Excel中的Sheet的数量 For i = 1To Worksheet.Sheets(y).UsedRange.Rows.Count '遍历sheet已经被使用单元格的行数 If Worksheet.Sheets(y).Range("F" & i) = getValue Or InStr(Worksheet.Sheets(y).Range("I" & i), getValue) Then'根据货号取得对应sheet的I列中第一次出现 getInt = i getNum = getNum + 1 ExitFor EndIf Next If getInt <> 0Then For i = getInt To Worksheet.Sheets(y).UsedRange.Rows.Count '从getInt遍历sheet2已经被使用单元格的行数 If InStr(Worksheet.Sheets(y).Range("A" & i), Time) Then getInt2 = i + 1 ExitFor EndIf Next EndIf
If getInt <> 0Then getWork = y ExitFor Else getWork = 1 EndIf Next