做這些東西主要是為了,實現,我們的最終目標。
查到 兩個大表里面的變化數據。
所以 這次
①實現了 文件操作的一部分內容。
包括,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
參考文獻