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 ...
随机推荐
- keras,在 fit 和 evaluate 中 都有 verbose 这个参数
1.fit 中的 verbose verbose:该参数的值控制日志显示的方式verbose = 0 不在标准输出流输出日志信息verbose = 1 输出进度条记录verbose = 2 ...
- RSA前端加密
昨天做了登陆模块,接触了md5&RSA加密.有点意思,talk is cheap,show me the code! 前端加密 为什么要加密 前端加密的方式 后台如何解密 1 为什么要加密? ...
- 第02课:启动GDB调试
使用GDB调试程序一般有三种方式: gdb filename gdb attach pid dgb filename corename 也对应这本节课的核心内容: 直接调试目标程序 附加进程 调试co ...
- jquery预览本地图片
本人qq群也有许多的技术文档,希望可以为你提供一些帮助(非技术的勿加). QQ群: 281442983 (点击链接加入群:http://jq.qq.com/?_wv=1027&k=29Lo ...
- 【洛谷P1730】最小密度路径
题目大意:给定一个 N 个点,M 条边的有向图,现有 Q 个询问,每次询问 X 到 Y 的最小密度路径是多少.最小密度路径的定义是路径长度除以路径边数. 题解:利用矩阵乘法,可以预处理出从 X 到 Y ...
- Java 指令重排
Java 指令重排 java 指令重排 package com.feshfans; /** * 用来演示指令重排 * 指令重排会发生在两个阶段: * 1. 编译期(jvm 加载字节码时) * 2. c ...
- Ubuntu启动器创建
Ubuntu 启动器创建 启动器的本质是一个后缀为.desktop的文件,文件内容如下(这里为我创建的Chrome启动器) [Desktop Entry] Encoding=UTF- Name=Chr ...
- CodeForces-682C(DFS,树,思维)
链接: https://vjudge.net/problem/CodeForces-682C 题意: Alyona decided to go on a diet and went to the fo ...
- ESP8266-Soft AP模式 —— 谁想连上我
AP是Access Point简称,也就是访问接入点,是网络的中心节点.一般家庭的无线路由器就是一个AP,众多站点(STA)加入到它所组成的无线网络,网络中的所有的通信都通过AP来转发完成. 软AP也 ...
- GO语言学习笔记5-defer的使用
1. 什么是defer defer是Go语言提供的一种用于注册延迟调用的机制:让函数或语句可以在当前函数执行完毕后(包括通过return正常结束或者panic导致的异常结束)执行. 2. defer的 ...