做这些东西主要是为了,实现,我们的最终目标。

查到 两个大表里面的变化数据。

所以 这次

①实现了 文件操作的一部分内容。

包括,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文件操作的更多相关文章

  1. VBA中操作XML

    OFFICE2007之后使用了OpenXml标准(伟大的改变),定制文本级的Ribbon可以通过修改压缩包内的xml文件来实现. 先学习一下VBA中操作XML的方法 先引用Microsoft XML ...

  2. 【.NET深呼吸】Zip文件操作(1):创建和读取zip文档

    .net的IO操作支持对zip文件的创建.读写和更新.使用起来也比较简单,.net的一向作风,东西都准备好了,至于如何使用,请看着办. 要对zip文件进行操作,主要用到以下三个类: 1.ZipFile ...

  3. 野路子出身PowerShell 文件操作实用功能

    本文出处:http://www.cnblogs.com/wy123/p/6129498.html 因工作需要,处理一批文件,本想写C#来处理的,后来想想这个是PowerShell的天职,索性就网上各种 ...

  4. Node基础篇(文件操作)

    文件操作 相关模块 Node内核提供了很多与文件操作相关的模块,每个模块都提供了一些最基本的操作API,在NPM中也有社区提供的功能包 fs: 基础的文件操作 API path: 提供和路径相关的操作 ...

  5. 归档NSKeyedArchiver解归档NSKeyedUnarchiver与文件管理类NSFileManager (文件操作)

    ========================== 文件操作 ========================== 一.归档NSKeyedArchiver 1.第一种方式:存储一种数据. // 归档 ...

  6. SQL Server附加数据库报错:无法打开物理文件,操作系统错误5

    问题描述:      附加数据时,提示无法打开物理文件,操作系统错误5.如下图: 问题原因:可能是文件访问权限方面的问题. 解决方案:找到数据库的mdf和ldf文件,赋予权限即可.如下图: 找到mdf ...

  7. 通过cmd完成FTP上传文件操作

    一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...

  8. Linux文件操作的主要接口API及相关细节

    操作系统API: 1.API是一些函数,这些函数是由linux系统提供支持的,由应用层程序来使用,应用层程序通过调用API来调用操作系统中的各种功能,来干活 文件操作的一般步骤: 1.在linux系统 ...

  9. C语言的fopen函数(文件操作/读写)

    头文件:#include <stdio.h> fopen()是一个常用的函数,用来以指定的方式打开文件,其原型为:    FILE * fopen(const char * path, c ...

随机推荐

  1. C#基础知识之图解TCP IP》读书笔记

    一.网络基础知识 1. 计算机使用模式的演变 2.协议 协议就是计算机与计算机之间通过网络实现通信事先达成的一种“约定”.这种“约定”使那些由不同厂商的设备.不同的CPU以及不同的操作系统组成的计算机 ...

  2. Linux中profile(转载)

    原文地址:http://www.cnblogs.com/mmfzmd517528/archive/2012/07/05/2577988.html 标黄是个人批注. 环境变量就是一个系统变量,系统配置一 ...

  3. python基础31[常用模块介绍]

    python基础31[常用模块介绍]   python除了关键字(keywords)和内置的类型和函数(builtins),更多的功能是通过libraries(即modules)来提供的. 常用的li ...

  4. 【NOIP2016提高A组模拟8.23】函数

    题目 分析 观察这个是式子\(\sum_{d|n}f(n)=n\), 发现其实函数\(f()\)就是欧拉函数\(φ()\)(见http://blog.csdn.net/chen1352/article ...

  5. lazarus for win32 serviceApp

    procedure TMyThread.Execute; begin //Application.EventLog.FileName:='d:\xx.txt'; //Application.Event ...

  6. node.js入门学习(四)--Demo图书的增删改查

    需求:图书的增删改查,图书数据保存在data.json文件中. 1.Demo结构: 2.首先下载安装node.js,配置环境变量:参考博客 3.项目初始化 1)创建项目根目录node-hello,进入 ...

  7. html abbr标签 语法

    html abbr标签 语法 作用:标记一个缩写 大理石平台 说明:<abbr> 标签指示简称或缩写,比如 "WWW" 或 "NATO".通过对缩写 ...

  8. JPA学习(三、JPA_API)

    框架学习之JPA(三) JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中 ...

  9. 2018山东省赛 H Dominoes ( 搜索 )

    题目链接 题意 : 给出一个 n * m 的矩阵,用规格 1 * 2 的多米诺去填充,题目数据保证最后只有一个格子是空白的(即没有被多米诺骨牌覆盖),问你现在通过移动多米诺能够产生多少种不同的状态(空 ...

  10. ArrayList,Vector ,LinkedList的存储性能和特性

    ArrayList,Vector,LinkedList : 两者都采用数组元素方式存储数据,此数组元素数大于实际存储的数据(以便于增加和插入元素),允许直接按照序号索引元素,但是插入元素涉及数组元素移 ...