zoukankan      html  css  js  c++  java
  • 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(3, 2)
    strPath2 = Cells(4, 2)
    strFilename1 = Cells(3, 3)
    strFilename2 = Cells(4, 3)
    
    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 2
    
    
    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(3, 2)
    strPath2 = Cells(4, 2)
    strFilename1 = Cells(3, 3)
    strFilename2 = Cells(4, 3)
    
    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(7, 4) = sheet.Cells(1, 1)
    
    
        
    End Sub
    
    
    =============================step 3
    
    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(3, 2)
    strPath2 = Cells(4, 2)
    strFilename1 = Cells(3, 3)
    strFilename2 = Cells(4, 3)
    
    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(7, 4) = sheet.Cells(1, 1)
    
    
    '查找项目
    Dim changedItemCol As Integer
    
    For changedItemCol = 1 To 20
        If sheet.Cells(1, changedItemCol) = "列4" Then
        Exit For
        End If
    Next changedItemCol
    
    Cells(16, 5) = 1
    Cells(17, 5) = changedItemCol
    
    
        
    End Sub
    ================================================step 4
    
    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(3, 2)
    strPath2 = Cells(4, 2)
    strFilename1 = Cells(3, 3)
    strFilename2 = Cells(4, 3)
    
    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(10, 2)
    itemName2 = Cells(11, 2)
    changedItemName = Cells(13, 2)
    '循环一次,判定多次好,还是 循环多次判定一次好。。。
    '现在我就想吃饭,无所谓了。。。反正也不是什么大体量的工
    
    作。不用纠结这个
    
    Dim col As Integer
    '变更前
    For col = 1 To 20
        If sheet1.Cells(1, col) = itemName1 Then
        Exit For
        End If
    Next col
    
    Cells(7, 4) = itemName1
    Cells(8, 5) = 1
    Cells(9, 5) = col
    
    
    For col = 1 To 20
        If sheet1.Cells(1, col) = itemName2 Then
        Exit For
        End If
    Next col
    
    Cells(11, 4) = itemName1
    Cells(12, 5) = 1
    Cells(13, 5) = col
    
    For col = 1 To 20
        If sheet1.Cells(1, col) = changedItemName Then
        Exit For
        End If
    Next col
    
    Cells(15, 4) = changedItemName
    Cells(16, 5) = 1
    Cells(17, 5) = col
    
    
    '变更后
    For col = 1 To 20
        If sheet2.Cells(1, col) = itemName1 Then
        Exit For
        End If
    Next col
    
    Cells(7, 6) = itemName1
    Cells(8, 7) = 1
    Cells(9, 7) = col
    
    
    For col = 1 To 20
        If sheet2.Cells(1, col) = itemName2 Then
        Exit For
        End If
    Next col
    
    Cells(11, 6) = itemName1
    Cells(12, 7) = 1
    Cells(13, 7) = col
    
    For col = 1 To 20
        If sheet2.Cells(1, col) = changedItemName Then
        Exit For
        End If
    Next col
    
    Cells(15, 6) = changedItemName
    Cells(16, 7) = 1
    Cells(17, 7) = col

    Set sheet1 = Nothing
    Set sheet2 = Nothing
    Set Workbook1 = Nothing
    Set Workbook2 = Nothing
    Set excel1 = Nothing
    Set excel2 = Nothing

    End Sub

    参考文献

  • 相关阅读:
    P1106 删数问题 / U83355 删数问题【升级版】
    P1955 [NOI2015] 程序自动分析
    P4447 [AHOI2018初中组]分组
    P1308 [NOIP2011 普及组] 统计单词数
    Django | 页面数据的缓存与使用
    Python 虚拟环境 | Mac/Linux下如何避坑安装配置Virtualenv
    python虚拟环境 | virtualenv 的简单使用 (图文)
    机器学习 | 浅谈K-近邻算法
    特征缩放 | 归一化和标准化 (下)
    简析方差、标准差与数值离散程度
  • 原文地址:https://www.cnblogs.com/letben/p/12006355.html
Copyright © 2011-2022 走看看