VBA文件操作
做这些东西主要是为了,实现,我们的最终目标。
查到 两个大表里面的变化数据。
所以 这次
①实现了 文件操作的一部分内容。
包括,excel的打开。分四个步骤。
1、路径
2、打开工作博
3、操作
4、关闭工作簿
②路径里面 包括 文件 是否 存在 的判断逻辑,如果 文件不存在,要记得 终止函数。
③以及 for 循环的 书写。还有 如果 到达了 查找目的以后,退出 for循环。
布局的创建。

判断文件存在的效果图

最终结果图

Sub 矩形1_Click()
'
' 矩形1_Click Macro
' Dim strPath1 As String
Dim strPath2 As String
Dim strFilename1 As String
Dim strFilename2 As String
Dim strFile1 As String
Dim strFile2 As String strPath1 = Cells(, )
strPath2 = Cells(, )
strFilename1 = Cells(, )
strFilename2 = Cells(, ) strFile1 = strPath1 & "/" & strFilename1
strFile2 = strPath2 & "/" & strFilename2 If Dir(strFile1) = "" Then
MsgBox "The target file :" & vbCrLf & strFile1 & vbCrLf & "is NOT exist!"
Exit Sub
Else
MsgBox "The target file :" & vbCrLf & strFile1 & vbCrLf & "is exist!"
End If End Sub ===============================step Sub 矩形1_Click()
'
' 矩形1_Click Macro
' Dim strPath1 As String
Dim strPath2 As String
Dim strFilename1 As String
Dim strFilename2 As String
Dim strFile1 As String
Dim strFile2 As String strPath1 = Cells(, )
strPath2 = Cells(, )
strFilename1 = Cells(, )
strFilename2 = Cells(, ) strFile1 = strPath1 & "/" & strFilename1
strFile2 = strPath2 & "/" & strFilename2 If Dir(strFile1) = "" Then
MsgBox "The target file :" & vbCrLf & strFile1 & vbCrLf & "is NOT exist!"
Exit Sub
Else
MsgBox "The target file :" & vbCrLf & strFile1 & vbCrLf & "is exist!"
End If '判定文件是否 存在 2 Dim excel As Object
Dim sheet As Object
Dim Workbook As Object Set excel = CreateObject("excel.application")
Set Workbook = excel.Workbooks.Open(strFile1)
Set sheet = Workbook.ActiveSheet Cells(, ) = sheet.Cells(, ) End Sub =============================step Sub 矩形1_Click()
'
' 矩形1_Click Macro
' Dim strPath1 As String
Dim strPath2 As String
Dim strFilename1 As String
Dim strFilename2 As String
Dim strFile1 As String
Dim strFile2 As String strPath1 = Cells(, )
strPath2 = Cells(, )
strFilename1 = Cells(, )
strFilename2 = Cells(, ) strFile1 = strPath1 & "/" & strFilename1
strFile2 = strPath2 & "/" & strFilename2 If Dir(strFile1) = "" Then
MsgBox "The target file :" & vbCrLf & strFile1 & vbCrLf & "is NOT exist!"
Exit Sub
Else
MsgBox "The target file :" & vbCrLf & strFile1 & vbCrLf & "is exist!"
End If '判定文件是否 存在 2 Dim excel As Object
Dim sheet As Object
Dim Workbook As Object Set excel = CreateObject("excel.application")
Set Workbook = excel.Workbooks.Open(strFile1)
Set sheet = Workbook.ActiveSheet Cells(, ) = sheet.Cells(, ) '查找项目
Dim changedItemCol As Integer For changedItemCol = To
If sheet.Cells(, changedItemCol) = "列4" Then
Exit For
End If
Next changedItemCol Cells(, ) =
Cells(, ) = changedItemCol End Sub
================================================step Sub 矩形1_Click()
'
' 矩形1_Click Macro
'
' getFile
Dim strPath1 As String
Dim strPath2 As String
Dim strFilename1 As String
Dim strFilename2 As String
Dim strFile1 As String
Dim strFile2 As String strPath1 = Cells(, )
strPath2 = Cells(, )
strFilename1 = Cells(, )
strFilename2 = Cells(, ) strFile1 = strPath1 & "/" & strFilename1
strFile2 = strPath2 & "/" & strFilename2 If Dir(strFile1) = "" Then
MsgBox "The target file :" & vbCrLf & strFile1 & vbCrLf & "is NOT exist!"
Exit Sub
'Else
' MsgBox "The target file :" & vbCrLf & strFile1 & vbCrLf & "is exist!"
End If If Dir(strFile2) = "" Then
MsgBox "The target file :" & vbCrLf & strFile2 & vbCrLf & "is NOT exist!"
Exit Sub
'Else
' MsgBox "The target file :" & vbCrLf & strFile1 & vbCrLf & "is exist!"
End If 'get workbook and sheet Dim excel1 As Object
Dim sheet1 As Object
Dim Workbook1 As Object Set excel1 = CreateObject("excel.application")
Set Workbook1 = excel1.Workbooks.Open(strFile1)
Set sheet1 = Workbook1.ActiveSheet Dim excel2 As Object
Dim sheet2 As Object
Dim Workbook2 As Object Set excel2 = CreateObject("excel.application")
Set Workbook2 = excel2.Workbooks.Open(strFile2)
Set sheet2 = Workbook2.ActiveSheet 'find Item
Dim changedItemName As String
Dim itemName1 As String
Dim itemName2 As String itemName1 = Cells(, )
itemName2 = Cells(, )
changedItemName = Cells(, )
'循环一次,判定多次好,还是 循环多次判定一次好。。。
'现在我就想吃饭,无所谓了。。。反正也不是什么大体量的工 作。不用纠结这个 Dim col As Integer
'变更前
For col = To
If sheet1.Cells(, col) = itemName1 Then
Exit For
End If
Next col Cells(, ) = itemName1
Cells(, ) =
Cells(, ) = col For col = To
If sheet1.Cells(, col) = itemName2 Then
Exit For
End If
Next col Cells(, ) = itemName1
Cells(, ) =
Cells(, ) = col For col = To
If sheet1.Cells(, col) = changedItemName Then
Exit For
End If
Next col Cells(, ) = changedItemName
Cells(, ) =
Cells(, ) = col '变更后
For col = To
If sheet2.Cells(, col) = itemName1 Then
Exit For
End If
Next col Cells(, ) = itemName1
Cells(, ) =
Cells(, ) = col For col = To
If sheet2.Cells(, col) = itemName2 Then
Exit For
End If
Next col Cells(, ) = itemName1
Cells(, ) =
Cells(, ) = col For col = To
If sheet2.Cells(, col) = changedItemName Then
Exit For
End If
Next col Cells(, ) = changedItemName
Cells(, ) =
Cells(, ) = col
Set sheet1 = Nothing
Set sheet2 = Nothing
Set Workbook1 = Nothing
Set Workbook2 = Nothing
Set excel1 = Nothing
Set excel2 = Nothing
End Sub
参考文献

VBA文件操作的更多相关文章
- VBA中操作XML
OFFICE2007之后使用了OpenXml标准(伟大的改变),定制文本级的Ribbon可以通过修改压缩包内的xml文件来实现. 先学习一下VBA中操作XML的方法 先引用Microsoft XML ...
- 【.NET深呼吸】Zip文件操作(1):创建和读取zip文档
.net的IO操作支持对zip文件的创建.读写和更新.使用起来也比较简单,.net的一向作风,东西都准备好了,至于如何使用,请看着办. 要对zip文件进行操作,主要用到以下三个类: 1.ZipFile ...
- 野路子出身PowerShell 文件操作实用功能
本文出处:http://www.cnblogs.com/wy123/p/6129498.html 因工作需要,处理一批文件,本想写C#来处理的,后来想想这个是PowerShell的天职,索性就网上各种 ...
- Node基础篇(文件操作)
文件操作 相关模块 Node内核提供了很多与文件操作相关的模块,每个模块都提供了一些最基本的操作API,在NPM中也有社区提供的功能包 fs: 基础的文件操作 API path: 提供和路径相关的操作 ...
- 归档NSKeyedArchiver解归档NSKeyedUnarchiver与文件管理类NSFileManager (文件操作)
========================== 文件操作 ========================== 一.归档NSKeyedArchiver 1.第一种方式:存储一种数据. // 归档 ...
- SQL Server附加数据库报错:无法打开物理文件,操作系统错误5
问题描述: 附加数据时,提示无法打开物理文件,操作系统错误5.如下图: 问题原因:可能是文件访问权限方面的问题. 解决方案:找到数据库的mdf和ldf文件,赋予权限即可.如下图: 找到mdf ...
- 通过cmd完成FTP上传文件操作
一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...
- Linux文件操作的主要接口API及相关细节
操作系统API: 1.API是一些函数,这些函数是由linux系统提供支持的,由应用层程序来使用,应用层程序通过调用API来调用操作系统中的各种功能,来干活 文件操作的一般步骤: 1.在linux系统 ...
- C语言的fopen函数(文件操作/读写)
头文件:#include <stdio.h> fopen()是一个常用的函数,用来以指定的方式打开文件,其原型为: FILE * fopen(const char * path, c ...
随机推荐
- List&LinQ
DataTable会将整个数据表接收过来,可真正使用的数据未必是整个数据表所有的数据. 使用List可以接收需要使用的数据 public class Data_Model { public strin ...
- Mysql定时器定时删除表数据
由于测试环境有张日志表没定时2分钟程序就狂插数据,导致不到1一个月时间,这张日志表就占用了6.7G的空间,但是日志刷新较快,有些日志就没什么作用,就写了个定时器,定期删除这张表的数据 首先先查看mys ...
- Python 3标准库课件第二章
整理第一章我又觉得烦,我就看第二章了,灰头土脸的,第二章一.如列表(list).元组(tuple).字典(dict).集合(set)二.2.1 enum:枚举类型 enum模块定义了一个提供迭代和比较 ...
- 跨平台信息获取小工具第三版本(增加了继承、多线程、异常处理模块、excel表格内容剔除空格)
# coding=utf-8 import threadingimport paramikoimport osimport timeimport xlrdimport xlwtimport openp ...
- contenteditable 光标定位到最后
在Vue做项目时,做了一个div[contenteditable=true]的组件作为文本输入框 在非手动输入值后,光标会丢失,经测试以下这段代码可用,直接将光标定位到最后 function keep ...
- 在javascript中,如何判断一个被多次encode 的url 已经被decode到原来的格式?
% 而不能被无限次decodeURIComponent 可以用%来进行判断
- Linux的目录结构与目录管理
Linux的目录结构与目录管理 Linux目录结构: 目录创建的规则 FHS 文件系统层次化标准 指定了Linux操作系统,哪些目录是一定要具备的 /boot /bin /sbin /etc /sys ...
- jetcache:在Spring Boot中怎么玩?
- JUnit——Failure与Error
(1)Failure是指测试失败(2)Error是指测试程序本身出错
- JUnit——单元测试步骤
步骤: 1. New Package(一般命名为*.Test,测试类与开发类放在不同的包中)2. New JUnit Text Case(一般命名为*Test)3. 选择需要测试的方法 4. 可以下载 ...