ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

请大神加个判断工作簿中的工作表是否存在

[复制链接]

TA的精华主题

TA的得分主题

发表于 2020-1-14 10:29 | 显示全部楼层 |阅读模式
附件中工作簿1中含有 “检测报告”工作表,工作簿2中则没有出现如图错误 ~T~6T8SY4E5D3S73M2UO67E.png
请大神加个判断如果不含有“”检测报告“ 内容不添加进数组里,(不打开工作簿情况下)
Sub checkclosedfile()
Dim strPath As String, strFile As String
Dim strSheet As String, strResult As String
Dim i As Integer
Dim j As Integer
Dim r As String
Dim m As Integer
Dim n As Integer
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim vFill()
Dim brr()
Dim arr()

brr() = Range("b3:Z3").Value
Sheet1.[A5:zz200].ClearContents
strPath = ThisWorkbook.Path
strSheet = "检测报告"


Application.ScreenUpdating = False
    Set fso = CreateObject("scripting.filesystemobject")
    Set ff = fso.getfolder(ThisWorkbook.Path) 'ThisWorkbook.Path是当前代码文件所在路径,路径名可以根据需求修改
    'ActiveSheet.UsedRange.ClearContents
    Dim Crr(1 To 1, 1 To 100) As Variant
    a = 1
    b = 0
    For Each f In ff.Files

        If f.Name Like "*.xls*" Then
          If Not f.Name Like "*$*" Then
            If f.Name <> ThisWorkbook.Name Then


                    Crr(1, a) = f.Name '相对路径名
                       a = a + 1
                         b = b + 1



        End If
        End If
        End If
    Next f
ReDim Preserve vFill(1 To b, 1 To 25) As Variant


For c = 1 To b
   strFile = Crr(1, c)
      If Dir(strPath & "\" & strFile) <> "" Then
            vFill(c, 1) = strFile
             For i = 2 To 25
             r = brr(1, i)
             If r <> "" Then
            vFill(c, i) = GetCellvalue(strPath, strFile, strSheet, r)
           End If
         Next
     End If
  Next


ThisWorkbook.Activate '本工作簿激活为使用状态
    With Sheets("sheet1") '对”汇总“表进行操作
     .[a5].Resize(b, 25) = vFill

  End With '结束对”汇总“表进行操作

    Application.ScreenUpdating = True


End Sub
Public Function GetCellvalue(strPath As String, strFile As String, strSheet As String, strA1 As String)
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
If Dir(strPath & strFile) = "" Then
Err.Raise 12345, "getcellvalue", "no found file"
Exit Function
End If

GetCellvalue = ExecuteExcel4Macro("'" & strPath & "[" & strFile & "]" & strSheet & "'!" & Range(strA1).Address(, , xlR1C1))

End Function


汇总数据.rar

57.22 KB, 下载次数: 1

TA的精华主题

TA的得分主题

发表于 2020-1-14 19:12 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
参考:
  1. Sub Tsht()
  2. Dim d, sh As Worksheet
  3. Set d = CreateObject("Scripting.Dictionary")
  4. For Each sh In Sheets
  5.     If Not d.Exists(sh.Name) Then d(sh.Name) = ""
  6. Next
  7. If Not d.Exists("检测报告") Then
  8.    MsgBox "[检测报告]工作表不存在!"
  9. End If
  10. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2020-1-14 19:29 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
for each sht in sheets
   
next

对工作表进行循环校检一下
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

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

GMT+8, 2024-4-25 07:30 , Processed in 0.033811 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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