ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 1158|回复: 0

请高手斧正一下。

[复制链接]

TA的精华主题

TA的得分主题

发表于 2013-1-25 22:03 | 显示全部楼层 |阅读模式
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
Sub 数据接收()
Set st11 = ThisWorkbook.Worksheets("花名册")
fileopen = Application.GetOpenFilename("Excel File(*.xls),*.xls", , "请选择文件(可多选)", , True)
If IsArray(fileopen) = False Then
    If fileopen = False Then
        Exit Sub
    End If
End If
ThisWorkbook.Activate
Application.ScreenUpdating = False      '窗体实时跳动
'接收情况
file_a = 0: file_y = 0:
jl = 0
For Each file In fileopen       '逐一处理所选接收文件
      file_a = file_a + 1
    ifopen = "0"        '判别文件是否已打开
'开始逐个文件的处理————————————————————————
    For i = 1 To Len(file)     '获取文件名,不含路径
        If Left(Right(file, i), 1) <> "\" Then
            bm = Right(file, i)
        Else
            Exit For
        End If
    Next
     
    If InStr(bm, "[") > 0 Or InStr(bm, "]") > 0 Then
        cause = "文件名中含有方括号,请删之..."
        structure = False
    End If
    For Each sht In Workbooks       '判断文件是否打开
        If sht.Name = bm Then       'sht.name包含扩展名 , workbooks()的函数和操作系统是否隐藏扩展名一致
            ifopen = "1"
            Exit For
        End If
    Next
    If ifopen = "0" Then            '上报表未打开则将其打开
        Workbooks.Open (file)
    End If
    Set nt11 = Workbooks(bm).Sheets(1)
    Set nt1r = nt11.Range("A65536").End(xlUp)
    nt1row = nt1r.Row
    rs = 0
    For i = 2 To nt1row
       If nt11.Cells(i, 29) > 0 And (nt11.Cells(i, 2) = "公务员" Or nt11.Cells(i, 2) = "机关技术工人") Then
             rs = rs + 1
          If nt11.Cells(i, 1) = nt11.Cells(i + 1, 1) And nt11.Cells(i + 1, 29) > 0 Then
             i = i + 1
          End If
        End If
    Next
    If rs > 18 Then
       For j = 1 To rs - 18
          Worksheets("花名册").Rows(9).Select
          Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
       Next
    End If
     For i = 2 To nt1row
       If nt11.Cells(i, 29) > 0 And (nt11.Cells(i, 2) = "公务员" Or nt11.Cells(i, 2) = "机关技术工人") And Trim(nt11.Cells(i, 14)) <> "" Then
          For j = 6 To rs + 5
              If st11.Cells(j, 1) = "" Then
                  Exit For
              End If
          Next
          st11.Cells(j, 1) = "=row()-5"
          If nt11.Cells(i, 1) = nt11.Cells(i + 1, 1) And nt11.Cells(i, 29) > 0 Then
             If nt11.Cells(i, 14) > nt11.Cells(i + 1, 14) Then
                st11.Cells(j, 2) = nt11.Cells(i, 1)
                st11.Cells(j, 4) = nt11.Cells(i, 14)
                st11.Cells(j, 5) = nt11.Cells(i, 15)
                st11.Cells(j, 6) = nt11.Cells(i, 22)
                st11.Cells(j, 7) = nt11.Cells(i, 29)
                i = i + 1
             Else
                st11.Cells(j, 2) = nt11.Cells(i + 1, 1)
                st11.Cells(j, 4) = nt11.Cells(i + 1, 14)
                st11.Cells(j, 5) = nt11.Cells(i + 1, 15)
                st11.Cells(j, 6) = nt11.Cells(i + 1, 22)
                st11.Cells(j, 7) = nt11.Cells(i + 1, 29)
                i = i + 1
             End If
           Else
                st11.Cells(j, 2) = nt11.Cells(i, 1)
                st11.Cells(j, 4) = nt11.Cells(i, 14)
                st11.Cells(j, 5) = nt11.Cells(i, 15)
                st11.Cells(j, 6) = nt11.Cells(i, 22)
                st11.Cells(j, 7) = nt11.Cells(i, 29)
        End If
        End If
    Next
Next
        Workbooks(bm).Saved = True      '退出时不提示保存
        Application.CutCopyMode = False     '清除剪贴版
        Workbooks(bm).Close
ThisWorkbook.Activate
Application.ScreenUpdating = False      '窗体实时跳动
End Sub
在文件已经打开的情况接收数据没有问题,文件没有打开接收就提示“越界”,请高手斧正一下。


您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-8 06:44 , Processed in 0.036841 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表