|
本帖最后由 tmplinshi 于 2022-12-20 16:03 编辑
练习
- Sub 数据处理()
- Application.ScreenUpdating = False
- Dim fPath
-
- For Each fPath In FileList(ThisWorkbook.Path & "\*.xls?")
- If fPath <> ThisWorkbook.FullName Then
- ModifyFile CStr(fPath)
- End If
- Next
- Application.CutCopyMode = False
- Application.ScreenUpdating = True
- End Sub
- Sub ModifyFile(fPath As String)
- With Workbooks.Open(fPath)
- .Sheets(1).Select
- Call 设置单元格格式
- Call 带数字的列转换为数值格式
- .Close True
- End With
-
- End Sub
- Sub 设置单元格格式()
- Dim str
- For Each str In Array("出生日期", "参加工作日期", "入司日期")
- Range("1:1").Find(str).EntireColumn.NumberFormatLocal = " YYYY-MM-DD"
- Next
- Range("1:1").Find("证件号码").EntireColumn.NumberFormatLocal = " @"
- End Sub
- Sub 带数字的列转换为数值格式()
- Dim ARR, I, J, skipColumns
- ARR = [A1].CurrentRegion
- Set skipColumns = GetExcludeColumns("出生日期", "参加工作日期", "入司日期", "证件号码", "部门", "姓名")
- For I = 2 To Range("A1").End(xlDown).Row
- For J = 1 To Range("A1").End(xlToRight).Column
- If Not skipColumns.Exists(J) Then
- ARR(I, J) = Val(Cells(I, J))
- End If
- Next
- Next
- [A1].Resize(I - 1, J - 1) = ARR
- End Sub
- Function GetExcludeColumns(ParamArray cellKeywords()) As Object
- Dim dict As Object
- Set dict = CreateObject("Scripting.Dictionary")
- Dim str, col
- For Each str In cellKeywords
- col = Range("1:1").Find(str).Column
- dict(col) = ""
- Next
-
- Set GetExcludeColumns = dict
- End Function
- Function FileList(filePattern As String) As Collection
- Dim coll As New Collection
- Dim fso As Object
- Dim fDir As String
- Dim fName As String
-
- fName = Dir(filePattern)
- If (fName = "") Then Exit Function
-
- Set fso = CreateObject("Scripting.FilesystemObject")
- fDir = fso.GetParentFolderName(filePattern) & ""
- Do
- coll.Add fDir & fName
- fName = Dir()
- Loop Until (fName = "")
- Set FileList = coll
-
- End Function
复制代码
注:论坛原因,代码第 79 行有一个反斜杠符号没有显示出来。正确的应该是:
|
评分
-
1
查看全部评分
-
|