excel 文件操作技巧
判斷文件是否存在辽幌,看文件地址的長度椿访,len
Sub ttt4()
If Len(Dir("C:\Users\Sun\Desktop\A.xlsb.xlsm")) = 0 Then ‘dir后面加文件的路徑,dir函數(shù)可以返回指定路徑的文件名稱
MsgBox "A profile doesn't exist"
Else
MsgBox "A profile exist"
End If
End Sub
判斷文件是否打開成玫,看窗口windows
Sub ttt5()
Dim x As Integer
For x = 1 To Windows.Count
If Windows(x).Caption = "A.xlsb.xlsm" Then '標題caption等于什么什么字符串
MsgBox "窗口已經(jīng)打開"
Exit Sub
End If
Next x
End Sub
excel文件新建和保存
‘給對象變量賦值,一定要在之前加set猪腕,對象是具有屬性和方法的對象。
Sub ttt6()
Dim wk As Workbook
Set wk = Workbooks.Add ’注意此處是workbooks.add
wk.Sheets("sheet1").Range("a1") = 125
wk.SaveAs "C:\Users\Sun\Desktop\B.xls"
End Sub
excel文件的打開和關(guān)閉
Sub ttt7()
Dim wb As Workbook
Set wb = Workbooks.Open("C:\Users\Sun\Desktop\B.xls")
MsgBox wb.Sheets("sheet1").Range("a1").Value
wb.Close True
End Sub
excel文件的保存和備份
Sub ttt8()
Dim wb As Workbook
Set wb = ThisWorkbook
wb.Save
wb.SaveCopyAs "C:\Users\Sun\Desktop\c.xls"
End Sub
excel文件復制和刪除
Sub ttt9()
FileCopy "C:\Users\Sun\Desktop\B.xls", "C:\Users\Sun\Downloads\fuckidiot.xls"
End Sub ‘還可以順便實現(xiàn)改名的作用
Sub ttt9()
Kill "C:\Users\Sun\Desktop\c.xls"
End Sub