Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1788|回复: 4

[已解决]如何判断另一文件中是否存在指定工作表

[复制链接]
发表于 2017-2-28 10:57 | 显示全部楼层 |阅读模式
各位网友好
现同一文件夹中有两文件AB,在B中写代码,
代码判断文件A中是否存在工作表C ,代码如何编写,如果把A改为变化的(是一个变量,即文件名称是变化的,但工作表C不变)
请指导,谢谢
最佳答案
2017-2-28 20:45
  1. Sub test()
  2.     Application.ScreenUpdating = False
  3.     Dim sht As String, file As String, path As String, wb As Workbook
  4.     sht = "判断结果"
  5.     On Error Resume Next
  6.     Set wsht = ThisWorkbook.Sheets(sht)
  7.     If Err.Number <> 0 Then
  8.         ThisWorkbook.Sheets.Add.Name = sht
  9.     Else
  10.         ThisWorkbook.Sheets(sht).Cells.ClearContents
  11.         Err.Clear
  12.     End If
  13.     path = ThisWorkbook.path
  14.     file = Dir(path & "\*.xls*")
  15.     Do While file <> ""
  16.         i = i + 1
  17.         If file <> ThisWorkbook.Name Then
  18.             Workbooks.Open path & "" & file
  19.             Set wb = ActiveWorkbook
  20.             On Error Resume Next
  21.             Set jude = wb.Sheets("测试")
  22.             If Err <> 0 Then
  23.                 ThisWorkbook.Sheets(sht).Cells(i, 1) = file & "工作薄不存在指定工作表!"
  24.             Else
  25.                 ThisWorkbook.Sheets(sht).Cells(i, 1) = file & "工作薄存在指定工作表!"
  26.             End If
  27.             Err.Clear
  28.             wb.Close
  29.         Else: ThisWorkbook.Sheets(sht).Cells(1, 1) = "判断结果"
  30.         End If
  31.         file = Dir
  32.     Loop
  33.     Application.ScreenUpdating = True
  34. End Sub
复制代码
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2017-2-28 11:58 | 显示全部楼层
xuanze.gif
判断.rar (13.82 KB, 下载次数: 1)
回复

使用道具 举报

 楼主| 发表于 2017-2-28 17:53 | 显示全部楼层
回复

使用道具 举报

发表于 2017-2-28 20:45 | 显示全部楼层    本楼为最佳答案   
  1. Sub test()
  2.     Application.ScreenUpdating = False
  3.     Dim sht As String, file As String, path As String, wb As Workbook
  4.     sht = "判断结果"
  5.     On Error Resume Next
  6.     Set wsht = ThisWorkbook.Sheets(sht)
  7.     If Err.Number <> 0 Then
  8.         ThisWorkbook.Sheets.Add.Name = sht
  9.     Else
  10.         ThisWorkbook.Sheets(sht).Cells.ClearContents
  11.         Err.Clear
  12.     End If
  13.     path = ThisWorkbook.path
  14.     file = Dir(path & "\*.xls*")
  15.     Do While file <> ""
  16.         i = i + 1
  17.         If file <> ThisWorkbook.Name Then
  18.             Workbooks.Open path & "" & file
  19.             Set wb = ActiveWorkbook
  20.             On Error Resume Next
  21.             Set jude = wb.Sheets("测试")
  22.             If Err <> 0 Then
  23.                 ThisWorkbook.Sheets(sht).Cells(i, 1) = file & "工作薄不存在指定工作表!"
  24.             Else
  25.                 ThisWorkbook.Sheets(sht).Cells(i, 1) = file & "工作薄存在指定工作表!"
  26.             End If
  27.             Err.Clear
  28.             wb.Close
  29.         Else: ThisWorkbook.Sheets(sht).Cells(1, 1) = "判断结果"
  30.         End If
  31.         file = Dir
  32.     Loop
  33.     Application.ScreenUpdating = True
  34. End Sub
复制代码
回复

使用道具 举报

发表于 2017-2-28 20:46 | 显示全部楼层
代码中的测试改成你要判断的是否存在的工作表名
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-23 14:47 , Processed in 0.318901 second(s), 12 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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