Excel精英培训网

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

[已解决]资料自动与指定路径下的资料夹内容做比对

[复制链接]
发表于 2021-8-27 15:40 | 显示全部楼层 |阅读模式
本帖最后由 lkk0063 于 2021-8-27 16:40 编辑

原本程式码为将资料输入至E1栏位, 会自动进入"C:\AA\" 比对档名是否存在

想改成将资料输入至E1:E65536栏位, 每个栏位自动比对档名是否存在



  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.     If Target.Count > 1 Then Exit Sub
  3.     Application.EnableEvents = False
  4.   
  5.     Dim ipath$: ipath = "C:\AA"
  6.     If Target.Row = 1 And Target.Column = 5 Then
  7.         Target.Interior.ColorIndex = 3
  8.         Target.Value = UCase(Target.Value)
  9.         Dim x$, xfile$, MyPath$, SPOpen$
  10.         x = Target.Value
  11.         xfile = Dir(ipath & "\.")
  12.         Do While xfile <> ""
  13.             If Left(xfile, Len(xfile) - 4) = x Then
  14.                 Target.Interior.ColorIndex = xlNone
  15.                 Exit Do
  16.             End If
  17.             xfile = Dir
  18.         Loop
  19.             If xfile < Target.Value Then
  20.             MsgBox "請確認是否有材質證明!"
  21.             ActiveWorkbook.FollowHyperlink "C:\AA"
  22.        End If
  23.     End If
  24.     Application.EnableEvents = True
  25. End Sub
复制代码


最佳答案
2021-8-27 15:53
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    Application.EnableEvents = False
  
    Dim ipath$: ipath = "C:\AA\"
    If Target.Column = 5 Then  '修改这句试试
        Target.Interior.ColorIndex = 3
        Target.Value = UCase(Target.Value)
        Dim x$, xfile$, MyPath$, SPOpen$
        x = Target.Value
        xfile = Dir(ipath & "\.")
        Do While xfile <> ""
            If Left(xfile, Len(xfile) - 4) = x Then
                Target.Interior.ColorIndex = xlNone
                Exit Do
            End If
            xfile = Dir
        Loop
            If xfile < Target.Value Then
            MsgBox "叫絋粄琌Τ借靡!"
            ActiveWorkbook.FollowHyperlink "C:\AA\"
       End If
    End If
    Application.EnableEvents = True
End Sub

AA.zip

19.59 KB, 下载次数: 12

发表于 2021-8-27 15:53 | 显示全部楼层    本楼为最佳答案   
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    Application.EnableEvents = False
  
    Dim ipath$: ipath = "C:\AA\"
    If Target.Column = 5 Then  '修改这句试试
        Target.Interior.ColorIndex = 3
        Target.Value = UCase(Target.Value)
        Dim x$, xfile$, MyPath$, SPOpen$
        x = Target.Value
        xfile = Dir(ipath & "\.")
        Do While xfile <> ""
            If Left(xfile, Len(xfile) - 4) = x Then
                Target.Interior.ColorIndex = xlNone
                Exit Do
            End If
            xfile = Dir
        Loop
            If xfile < Target.Value Then
            MsgBox "叫絋粄琌Τ借靡!"
            ActiveWorkbook.FollowHyperlink "C:\AA\"
       End If
    End If
    Application.EnableEvents = True
End Sub
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-29 21:06 , Processed in 0.289494 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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