首页officeexcel正文

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

强国说学习2023-04-20164尤其是操作文件虚线Excel2003教程

#使用宏在一个Excel文件中操作另一个Excel文件。
办公软件中使用宏处理一些信息,尤其是大量的重复信息,可以提高效率。

操作过程结构如下:

Dim strFilePath As String
Dim strFileName As String

文章内容由WPS之家(wps.qiangguoshuo.com)收集于网络,希望能为您提供帮助。


Dim strFile As String
Dim excel As Object
Dim sheet As Object
Dim Workbook As Object

‘The target file
strFileName = "test_out.xls"
strFilePath = Me.Parent.Path
strFile = strFilePath & "\&; & strFileName

‘STEP 0: proof the existence of excel file
If Dir(strFile) = "" Then
MsgBox "The target file:" & vbCrLf & _
strFile & vbCrLf & _
"is NOT exsit!"
Exit Sub
End If

‘STEP 1: open the excel file
Set excel = CreateObject("excel.application")
Set Workbook = excel.Workbooks.Open(strFile)

‘STEP 2: find the needed sheet
Set sheet = Workbook.ActiveSheet

‘STEP 3: process
MsgBox sheet.Range("a1").Value

sheet.Range("a2").Value = "sunyt"

‘STEP 4: close file
‘ : to save file firstly
Workbook.Save
Workbook.Close
excel.Quit
Set sheet = Nothing
Set Workbook = Nothing
Set excel = Nothing

如想转载该文章请注明出处:强国说学习-qiangguoshuo.com
强国说学习

转载声明:本站发布文章及版权归原作者所有,转载本站文章请注明文章来源!

本文链接:https://www.qiangguoshuo.com/excel/60498.html