当前位置:主页 > Office办公 > 尤其是

尤其是

Excel使用宏操作另一个Excel文件
Excel使用宏操作另一个Excel文件

#使用宏在一个Excel文件中操作另一个Excel文件。办公软件中使用宏处理一些信息,尤其是大量的重复信息,可以提高效率。操作过程结构如下:Dim strFilePath As StringDim strFileName As StringDim strFile As StringDim excel As ObjectDim sheet As ObjectDim Workbook As Object‘The target filestrFileName = "test_out.xls"strFilePath = Me.Parent.PathstrFile = strFilePath & "\" & strFileName‘STEP 0: proof the existence of excel fileIf Dir(strFile) = "" ThenMsgBox "The target file:" & vbCrLf & _strFile & vbCrLf & _"is NOT exsit!"Exit SubEnd If‘STEP 1: open the excel fileSet excel = CreateObject("excel.application")Set Workbook = excel.Workbooks.Open(strFile)‘STEP 2: find the needed sheetSet sheet = Workbook.ActiveSheet‘STEP 3: processMsgBox sheet.Range("a1").Valuesheet.Range("a2").Value = "sunyt"‘STEP 4: close file‘ : to save file firstlyWorkbook.SaveWorkbook.Closeexcel.QuitSet sheet = NothingSet Workbook = NothingSet excel = Nothing

94 次浏览