工作中自己编写的VBA工具,为了记录用(文件处理,PPT,EXCEL,转CSV,合并单元格,时间函数)

'Two useful Functions:

'Use Function below for reference       To judge whether the folder path is exist
Public Function FileFolderExists(strFullPath As String) As Boolean

FileFolderExists = False
On Error GoTo EarlyExit
If Not Dir(strFullPath, vbDirectory) = vbNullString Then
FileFolderExists = True
EarlyExit:
On Error GoTo 0
End If
End Function

'Author: hiperion
'Resource: CSDN
'Original Article: https://blog.csdn.net/ocean20/article/details/6411316

'Open Windows ChooseFolder form application
Public Function ChooseFolder() As String

Dim dlgOpen As FileDialog 'this type FileDialog is microsoft object

Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)

With dlgOpen
If .Show = -1 Then
ChooseFolder = .SelectedItems(1)
End If
End With
Set dlgOpen = Nothing
End Function
'Get Control of PPT file
Dim fso, folder, fds, fd, folder2, fs, f, pptapp
Dim PPTobj As Object
        
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder1 = fso.GetFolder(XXXX)  'getFolder XXX is its path
    Set fds = folder1.subfolders        'collection of sub folder
    Set pptapp = CreateObject("powerpoint.application")  
'define Object to PPT application
    For Each fd In fds                  'travesals every subfolder
        Set folder2 = fd                
        Set fs = folder2.Files          
        For Each f In fs                'travesals every file in subfolder
        
If f.Name Like "*.pptx" Then
Set PPTobj = GetObject(f.path)    
'f type is file, cannot get attributes of PPT, so need to define an Object to set as f(PPT)
        Set pptapp = PPTobj       
'pptapp type is PPT application, get PPTobj(f)
        Debug.Print pptapp.Slides.Count  'Finally can use PPT’s attributes [Slides.Count]
        FileCopy f.path, TarAddress & "\" & f.Name    
'FileCopy [file.path], [targetAddress&file.Name]
End If
        Next







'Get Control of Excel & Windows Files
Dim fso, folder1, f As Object
Dim fexcel As Workbook
Dim fTitle As String

'Set fexcel = CreateObject("Excel.Application")
Set fso = CreateObject("Scripting.FileSystemObject")     
' To give fso object a filesystemobject script function  [very important]
On Error Resume Next
   Application.DisplayAlerts = False

If FileFolderExists(FileFoderPath) Then
        Set folder1 = fso.GetFolder(FileFoderPath)  'GetFolder
End If

If folder1.Files.Count = 0 Then
Else
   For Each f In folder1.Files
   	'FileCopy f.path, TarAddress & "\" & f.Name
Set fexcel = GetObject(f.path)     'get file object to workbook
Debug.Print fexcel.Sheets.Count
fexcel.Close SaveChanges:=False
        Set fecxel = Nothing
   Next
End If
Application.DisplayAlerts = True
Sub TurnToCSV()

Dim wS As Worksheet
Dim sPath As String

On Error Resume Next
sPath = ChooseFolder()    'Open Folder path
If sPath = "" Then
Exit Sub
End If

'Debug.Print sPath

For Each wS In ThisWorkbook.Sheets
                'wS.SaveAs sPath & "\" & wS.Name & ".csv", xlCSV, CreateBackup:=False, TextCodePage:=utf - 8
                'wS.SaveAs sPath & "\" & wS.Name & ".csv", xlUnicodeText, CreateBackup:=False, TextCodePage:=utf - 8
Next wS
End Sub

Public Function ChooseFolder() As String

Dim dlgOpen As FileDialog

Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)
With dlgOpen
If .Show = -1 Then
ChooseFolder = .SelectedItems(1)
End If
End With
Set dlgOpen = Nothing
End Function
Sub MergeCells()

Dim x, y As Integer

'Call delay(5) 'Time delay function  5second

x = Selection.Row   'x is the row which you choose
y = Selection(1).Column    'y is the first column which you choose

'Debug.Print x & " " & y

Range(x & ":" & x).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
'Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove  'this command add one row above your chosen row

For i = 1 To y - 1
Range(Cells(x - 1, i), Cells(x, i)).Merge    'Merge cells
Next

End Sub

Sub delay(T As Single)
    Dim time1 As Single
    time1 = Timer   'Timer is now time in System
    Do
        DoEvents        'This sentance means transfer control right to computer to do another program
    Loop While Timer - time1 < T
End Sub

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值