1234

ExcelHome技术论坛

用户名  找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

Dictionary+Folder+File+FileSystemObject胡应用。

[复制链接]

TA的精华主题

TA的得分主题

发表于 2025-3-23 16:53 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助




  1. Function CheckFolder(Fso As Scripting.FileSystemObject, Sht As Worksheet) As Folder
  2.     'Dim oFolder As Folder, oFile As File
  3.     Dim Yyyy, YyyyMm, PathName
  4.         With Sht
  5.              Yyyy = Split(.Name, "年")(0) & "年"
  6.              YyyyMm = Split(.Name, "月")(0) & "月"
  7.         End With
  8.         PathName = ThisWorkbook.Path & "" & Yyyy
  9.         
  10.         If Not Fso.FolderExists(PathName) Then
  11.              Fso.CreateFolder PathName
  12.         End If
  13.         '''
  14.         PathName = ThisWorkbook.Path & "" & Yyyy & "" & YyyyMm
  15.         If Not Fso.FolderExists(PathName) Then
  16.              Fso.CreateFolder PathName
  17.         End If
  18.         PathName = ThisWorkbook.Path & "" & Yyyy & "" & YyyyMm & "" & Sht.Name
  19.         If Not Fso.FolderExists(PathName) Then
  20.              Fso.CreateFolder PathName
  21.         End If
  22.         Set CheckFolder = Fso.GetFolder(PathName)

  23. End Function

  24. Private Sub ShtNameToFolder()
  25.     Dim Fso As Scripting.FileSystemObject
  26.         Set Fso = New Scripting.FileSystemObject
  27.     Dim oDate As Date, Str, Arr
  28.     Dim CurrentFolder As Folder, AimFolder As Folder
  29.     Dim oFile As File, oFile1 As File
  30.     Dim Sht As Worksheet, Rng As Range, oRow
  31.         Set Sht = Application.ActiveSheet
  32.         Set AimFolder = CheckFolder(Fso, Sht)
  33.         Set CurrentFolder = Fso.GetFolder(ThisWorkbook.Path)
  34.         'Debug.Print AimFolder.Path, CurrentFolder.Path
  35.     Dim PathName
  36.         PathName = ThisWorkbook.Path
  37.     Dim FileDict As Scripting.Dictionary
  38.         Set FileDict = New Scripting.Dictionary
  39.         With Sht
  40.             oRow = .Cells(.Rows.Count, 1).End(xlUp).Row
  41.             
  42.             For ii = 4 To oRow
  43.                
  44.                   
  45.                    If Fso.FolderExists(CurrentFolder.Path) Then
  46.                        PathName = CurrentFolder.Path & "" & .Cells(ii, 1)
  47.                        If Fso.FileExists(PathName) Then
  48.                           Set oFile1 = Fso.GetFile(PathName)
  49.                        End If
  50.                        PathName = CurrentFolder.Path & "" & .Cells(ii, 2)
  51.                        If Fso.FileExists(PathName) Then
  52.                           Set oFile = Fso.GetFile(PathName)
  53.                        End If
  54.                        
  55.                        If Not oFile Is Nothing Then
  56.                            Set FileDict(oFile) = oFile1
  57.                        End If
  58.                    End If
  59.             Next ii
  60.         End With
  61.         ''
  62.         With FileDict
  63.             For ii = 0 To .Count - 1
  64.                  Debug.Print .Keys(ii).Path, .Items(ii).Path
  65.                  Fso.MoveFile .Keys(ii), AimFolder
  66.                  Fso.MoveFile .Items(ii), AimFolder
  67.             Next ii
  68.         End With
  69. End Sub
复制代码


del.zip

50.77 KB, 下载次数: 0

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

本版积分规则

1234

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

GMT+8, 2025-4-23 22:21 , Processed in 0.017425 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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