ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[讨论] 小花鹿学习VBA记录

  [复制链接]

TA的精华主题

TA的得分主题

 楼主| 发表于 2014-10-6 11:12 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
自定义排序:
Sub Macro2()
Dim i, ar
ar = Sheet1.[a1].CurrentRegion
With Application
    .AddCustomList ListArray:=ar
    i = .GetCustomListNum(ar)
    Sheet2.Range("a1:b" & Sheet2.[a65536].End(3).Row).Sort Key1:=Range("A2"), Order1:=xlAscending, Header:= _
        xlGuess, OrderCustom:=i + 1, MatchCase:=False, Orientation:=xlTopToBottom, _
        SortMethod:=xlPinYin, DataOption1:=xlSortNormal
    .DeleteCustomList ListNum:=i
End With
End Sub

20141005.rar (13.41 KB, 下载次数: 46)

评分

2

查看全部评分

TA的精华主题

TA的得分主题

发表于 2014-10-6 20:36 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2014-10-7 21:46 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2015-11-24 12:08 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
学习啦,占个位置呀

TA的精华主题

TA的得分主题

 楼主| 发表于 2015-11-24 12:35 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Unicode 编码表,放在这里便于查找:
Unicode 编码表.rar (114.89 KB, 下载次数: 36)

TA的精华主题

TA的得分主题

 楼主| 发表于 2016-1-6 13:33 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
下列资料来自本站,放在这里是为了查找方便,谢谢原作者。

'MuLu是要查找的文件夹,如:"F:\VBA\pdf\Excel2007VBA"
'LeiXing是要查找的文件类型,如:*.xls,a?*.txt等,如果省略该参数,函数实现的是查找文件夹功能
'LeiXing参数不省略时:1、Zi为true时搜索所有子文件夹下符合要求的文件。2、Zi为false时仅搜索参数MuLu下符合要求的文件
'LeiXing参数省略时:  1、Zi为true时搜索参数MuLu下所有子文件。2、Zi为false时仅搜索参数MuLu下的文件夹
'函数的返回值是一个一维数组,可视具体情况使用
Public Function ListFile(MuLu As String, Zi As Boolean, Optional LeiXing As String = "")
    Dim MyFile As String, ms As String
    Dim arr, brr, x
    Dim i As Integer
    Set d = CreateObject("Scripting.Dictionary")
    If Right(MuLu, 1) <> "\" Then MuLu = MuLu & "\"
    d.Add MuLu, ""
    i = 0
    On Error Resume Next '这里增加错误时继续的处理代码
    Do While i < d.Count
        brr = d.keys
        MyFile = Dir(brr(i), vbDirectory)
        Do While MyFile <> ""
            If MyFile <> "." And MyFile <> ".." Then
                If (GetAttr(brr(i) & MyFile) And vbDirectory) = vbDirectory Then
                    If Err.Number Then '这里增加文件名字符错误处理判断
                        Err.Clear '#52 文件名错误
                    Else
                        d.Add (brr(i) & MyFile & "\"), ""
                    End If
                End If
            End If
            MyFile = Dir
        Loop
        If Zi = False Then Exit Do
        i = i + 1
    Loop
    If LeiXing = "" Then
        ListFile = Application.Transpose(d.keys)
    Else
        For Each x In d.keys
            MyFile = Dir(x & LeiXing)
            Do While MyFile <> ""
                ms = ms & x & MyFile & ","
                MyFile = Dir
            Loop
            If Zi = False Then Exit For
        Next
        If ms = "" Then ms = "没有符合要求的文件,"
        ListFile = Application.Transpose(Split(ms, ","))
    End If
    On Error GoTo 0
End Function

Sub selectfolder()
    Set myFolder = CreateObject("Shell.Application").BrowseForFolder(0, "请选择文件夹:", 0)
    If Not myFolder Is Nothing Then
        mypath$ = myFolder.Items.Item.Path
    Else
        MsgBox "你没有选择文件夹!"
        Exit Sub
    End If
    If Right(mypath, 1) <> "\" Then mypath = mypath & "\"
    '同样返回的是选中目标文件夹的绝对路径,但除了本地C盘、D盘会以"C:\"形式返回外,其余路径无"\"需要添加
Dim filearray
filearray = ListFile(mypath, True, "*.*")
Range("a1").Resize(UBound(filearray), 1) = filearray
End Sub


TA的精华主题

TA的得分主题

 楼主| 发表于 2016-1-11 11:09 | 显示全部楼层
列出选中文件夹及其子文件夹中的文件:
Option Explicit
Dim FileList(1 To 65536, 1 To 1), filen&

Sub FolderFileList()
'--------------------------------------------------------------------------------------------
Dim myfolder, mypath
    Set myfolder = CreateObject("Shell.Application").BrowseForFolder(0, "请选择文件夹:", 0)
    If Not myfolder Is Nothing Then
        If myfolder <> "桌面" Then mypath = myfolder.Items.Item.Path
    Else
        MsgBox "你没有选择文件夹!"
        Exit Sub
    End If
    If mypath = "" Or Left(mypath, 2) = "::" Then
        MsgBox "文件夹选择错误!"
        Exit Sub
    End If
'------------------------------------------------------------------------------------------------
Dim fso, Folder, myf
Set fso = CreateObject("scripting.filesystemobject")
Set Folder = fso.getfolder(mypath)
filen = 0
For Each myf In Folder.Files
    filen = filen + 1
    FileList(filen, 1) = myf
Next myf
SubFolderFileList (mypath)
If filen > 65536 Then filen = 65536
[a1].Resize(filen) = FileList
filen = 0
End Sub

Function SubFolderFileList(pth)
Dim fso, Folder, SubFolder, myf, fd
Set fso = CreateObject("scripting.filesystemobject")
Set Folder = fso.getfolder(pth)
Set SubFolder = Folder.subfolders
On Error Resume Next
For Each fd In SubFolder
    For Each myf In fd.Files
        If myf <> "" Then
            filen = filen + 1
            FileList(filen, 1) = myf
        End If
    Next myf
    SubFolderFileList (fd.Path)
Next fd
On Error GoTo 0
End Function

列出选中文件夹及其子文件夹中的文件.rar (15.7 KB, 下载次数: 34)

TA的精华主题

TA的得分主题

 楼主| 发表于 2016-1-17 10:37 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 小花鹿 于 2016-1-17 10:39 编辑

搜索选定文件夹及其子文件夹中文件的小工具:(用窗体实时显示搜索到的文件)
Option Explicit
Dim FileList(1 To 65536, 1 To 1), filen&

Sub FolderFileList()
'--------------------------------------------------------------------------------------------
Dim myfolder, mypath
    Set myfolder = CreateObject("Shell.Application").BrowseForFolder(0, "请选择文件夹:", 0)
    If Not myfolder Is Nothing Then
        If myfolder <> "桌面" Then mypath = myfolder.Items.Item.Path
    Else
        MsgBox "你没有选择文件夹!"
        Exit Sub
    End If
    If mypath = "" Or Left(mypath, 2) = "::" Then
        MsgBox "文件夹选择错误!"
        Exit Sub
    End If
'------------------------------------------------------------------------------------------------
Dim fso, Folder, myf
Set fso = CreateObject("scripting.filesystemobject")
Set Folder = fso.getfolder(mypath)
filen = 0
UserForm1.Show 0
For Each myf In Folder.Files
    filen = filen + 1
    FileList(filen, 1) = myf
    UserForm1.Label1.Caption = fileedit(myf)
    DoEvents
Next myf
SubFolderFileList (mypath)
UserForm1.Caption = "文件搜索完成:"
UserForm1.Label1.Caption = "文件搜索完成,共搜索到 " & filen & " 个文件!"
DoEvents
If filen > 65536 Then filen = 65536
If filen Then [a1].Resize(filen) = FileList
filen = 0
End Sub

Function SubFolderFileList(pth)
Dim fso, Folder, SubFolder, myf, fd
Set fso = CreateObject("scripting.filesystemobject")
Set Folder = fso.getfolder(pth)
Set SubFolder = Folder.subfolders
On Error Resume Next
For Each fd In SubFolder
    For Each myf In fd.Files
        If myf <> "" Then
            filen = filen + 1
            FileList(filen, 1) = myf
            UserForm1.Label1.Caption = fileedit(myf)
            DoEvents
        End If
    Next myf
    SubFolderFileList (fd.Path)
Next fd
On Error GoTo 0
End Function

Function fileedit(filenam)
Dim file1, file2, filear
If Len(filenam) > 60 Then
    filear = Split(filenam, "\")
    file2 = filear(UBound(filear))
    file1 = Replace(filenam, "\" & file2, "")
    file1 = Left(file1, 30)
    fileedit = file1 & "...\" & file2
Else
    fileedit = filenam
End If
End Function
列出选中文件夹及其子文件夹中的文件.rar (1.39 MB, 下载次数: 33)



补充内容 (2016-11-4 19:42):
Sub 遍历子文件夹()
Dim d, thispath, thisname, n&, m&, x&, mydir, dk
Set d = CreateObject("scripting.dictionary")
thispath = ThisWorkbook.Path & "\"
thisname = ThisWorkbook.Name
d(thispath) = ""
Do While n < d.Count
    dk = d.keys
    mydir = Dir(dk(n), vbDirectory)
    Do While mydir <> ""
        If mydir <> "." And mydir <> ".." Then
            If GetAttr(dk(n) & mydir) = vbDirectory Then
                d(dk(n) & mydir & "\") = ""
                m = m + 1
                Cells(m, 1) = dk(n) & mydir & "\"
            Else
                x = x + 1
                Cells(x, 7) = dk(n) & mydir
            End If
        End If
        mydir = Dir
    Loop
    n = n + 1
Loop
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2016-6-4 10:38 | 显示全部楼层
WORD常用查找与替换实例及方法
作者:tangqingfu


Word常用查找与替换实例及方法.rar (117.09 KB, 下载次数: 50)

TA的精华主题

TA的得分主题

 楼主| 发表于 2016-6-5 12:56 | 显示全部楼层
xls数据写入WORD模板,每人一个DOC文件:
Sub test()
Dim myword, thispath, mydoc, r&, i&, j&
Set myword = CreateObject("word.application")
thispath = ThisWorkbook.Path & "\"
r = Sheet1.[b65536].End(3).Row
For i = 2 To r
    mydoc = thispath & "授课通知(" & Sheet1.Cells(i, 2) & ").doc"
    FileCopy thispath & "授课通知(模板).doc", mydoc
    With myword
        .documents.Open mydoc
        .Visible = True
        For j = 1 To 5
            If .Selection.Find.Execute("数据" & Format(j, "000")) Then
                .Selection.Text = Sheet1.Cells(i, j + 1)
                .Selection.MoveRight Unit:=1, Count:=1
            End If
        Next j
        For j = 1 To 3
            .activedocument.tables(1).cell(2, j) = Sheet1.Cells(i, j + 6)
            .activedocument.tables(1).cell(4, j) = Sheet1.Cells(i, j + 9)
        Next j
        '--------------------------------------------
        .ActiveWindow.ActivePane.View.SeekView = 9
        If .Selection.Find.Execute("数据006") Then
            .Selection.Text = "中心小学"
        End If
        .ActiveWindow.ActivePane.View.SeekView = 10
        If .Selection.Find.Execute("数据007") Then
            .Selection.Text = "宁阳县"
        End If
        '------------------------------------------
        .documents.Close True
    End With
Next i
myword.Quit
End Sub

将Excel数据对应写入已做好的Word模板的指定位置(分发).rar (38.33 KB, 下载次数: 53)



补充内容 (2016-9-28 23:16):
涉及页眉、页脚
.ActiveWindow.ActivePane.View.SeekView = 9  '页眉
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-4-28 23:40 , Processed in 0.044251 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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