我遇到了一些我的vba代碼問題。我試圖讓兩份報告比較自己。如果有差異,它將突出顯示該單元格爲紅色,如果其爲負值,則在綠色的單元格上顯示爲正值。在差異報告(表單3)上,它將顯示與其尊重顏色的差異值。 Sheet2 - Sheet1將顯示在sheet3上。如何比較兩個工作表並生成差異報告作爲第三個工作表?
如果沒有差別,它將顯示數字值爲0。如果沒有區別,文本和日期將保持不變。
我已經完整地完成了這項任務,除非我只有在數據和報告與單元匹配的情況下才能工作。我需要它能夠實現數據是否從sheet1的單元格A15開始,並且如果sheet2的數據從A17開始,我需要知道它不是從sheet2的A15開始,而是開始在A17進行比較。因此,sheet1上的A15將自己與sheet2上的A17進行比較,依此類推,整個報表都是這樣。
當我現在運行它時,它會破壞它,或者如果報告不匹配,則意識到一切都不一樣。我需要它有一個聰明的感覺,我猜並且知道它需要比較正確的數據,即使這些單元格不匹配。我做了大量的研究,不知道我是否必須使用vlookup,match,index或什麼?如果是這樣,我甚至不知道從哪裏開始。代碼如下。
Option Explicit
'This is where the program calls all sub procedures In Order.
Sub RunCompareSchedules()
Application.ScreenUpdating = False
Sheet3Creation "Sheet1", "Sheet2", "Sheet3"
Copy_range "Sheet1", "Sheet2", "Sheet3"
compareSheets "Sheet1", "Sheet2", "Sheet3"
DataPush "Sheet1", "Sheet2", "Sheet3"
CellFormat "Sheet1", "Sheet2", "Sheet3"
AutoFit "Sheet1", "Sheet2", "Sheet3"
Application.ScreenUpdating = True
End Sub
Sub compareSheets(shtSheet1 As String, shtSheet2 As String, shtSheet3 As String)
Dim mycell As Range
Dim mydiffs As Integer
'For each cell in sheet2 that is less in Sheet1, color it red, if it's more color it Green. If neither of these are true that don't add interior color.
For Each mycell In ActiveWorkbook.Worksheets(shtSheet2).UsedRange
If Not IsDate(mycell.Value) Or Not IsNumeric(mycell.Value) Then
If mycell.Value <> ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
mycell.Interior.ColorIndex = 33
mydiffs = mydiffs + 1
Else
mycell.Interior.ColorIndex = 0
End If
End If
Next
For Each mycell In ActiveWorkbook.Worksheets(shtSheet2).UsedRange
If IsNumeric(mycell.Value) Then
If mycell.Value > ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
mycell.Interior.Color = vbRed
mydiffs = mydiffs
ElseIf mycell.Value < ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
mycell.Interior.Color = vbGreen
Else
mycell.Interior.ColorIndex = 0
End If
End If
Next
'For each cell in the date colomn sheet2 that is not the same in Sheet1, color it yellow
For Each mycell In ActiveWorkbook.Worksheets(shtSheet2).UsedRange
If IsDate(mycell.Value) Then
If mycell.Value < ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
mycell.Interior.Color = vbGreen
mydiffs = mydiffs
ElseIf mycell.Value > ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
mycell.Interior.Color = vbRed
Else
mycell.Interior.ColorIndex = 0
End If
End If
Next
If Sheets(shtSheet2).Cells(1, 1).Value <> Sheets(shtSheet1).Cells(1, 1).Value Then
Sheets(shtSheet2).Cells(1, 1).Interior.Color = vbYellow
mydiffs = mydiffs + 1
Else
Sheets(shtSheet2).Cells(1, 1).Interior.ColorIndex = 0
End If
If Sheets(shtSheet3).Cells(1, 1).Value <> Sheets(shtSheet1).Cells(1, 1).Value Then
Sheets(shtSheet3).Cells(1, 1).Interior.Color = vbYellow
Else
Sheets(shtSheet3).Cells(1, 1).Interior.ColorIndex = 0
End If
'Display a message box to demonstrate the differences
MsgBox mydiffs & " differences found. If Date cells are highlighted yellow on Sheet3, they will show the amount of difference in days.", vbInformation
ActiveWorkbook.Sheets(shtSheet2).Select
End Sub
Sub Copy_range(shtSheet1 As String, shtSheet2 As String, shtSheet3 As String)
'Copy worksheet 2 to worksheet 3
Worksheets("Sheet2").UsedRange.Copy
Worksheets("Sheet3").UsedRange.PasteSpecial
End Sub
Sub DataPush(shtSheet1 As String, shtSheet2 As String, shtSheet3 As String)
Dim mycell As Range
Dim mydiffs As Integer
Dim cellLoc As String
'For each cell in sheet3 that is not the same in Sheet2, color it red
For Each mycell In ActiveWorkbook.Worksheets(shtSheet3).UsedRange
If Not IsDate(mycell.Value) Or Not IsNumeric(mycell.Value) Then
If mycell.Value <> ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
mycell.Interior.ColorIndex = 33
mydiffs = mydiffs + 1
Else
mycell.Interior.ColorIndex = 0
End If
End If
Next
For Each mycell In ActiveWorkbook.Worksheets(shtSheet3).UsedRange
If IsNumeric(mycell.Value) Then
If mycell.Value > ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
mycell.Interior.Color = vbRed
mydiffs = mydiffs
ElseIf mycell.Value < ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
mycell.Interior.Color = vbGreen
Else
mycell.Interior.ColorIndex = 0
End If
End If
Next
'For each cell in the date colomn sheet3 that is not the same in Sheet2, color it yellow
For Each mycell In ActiveWorkbook.Worksheets(shtSheet3).UsedRange
If IsDate(mycell.Value) Then
If mycell.Value < ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
mycell.Interior.Color = vbGreen
ElseIf mycell.Value > ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
mycell.Interior.Color = vbRed
Else
mycell.Interior.ColorIndex = 0
End If
End If
Next
'This will show the difference between each cell with a numeric value from sheet1 and 2, in sheet3. If it's not different, it will show a zero.
For Each mycell In Sheets(shtSheet3).UsedRange
If IsNumeric(mycell.Value) Then
If Not mycell.Value = Sheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
ActiveWorkbook.Worksheets(shtSheet3).Cells(mycell.Row, mycell.Column).Value = _
ActiveWorkbook.Worksheets(shtSheet2).Cells(mycell.Row, mycell.Column).Value - ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value
ElseIf mycell.Value = "" Then
ActiveWorkbook.Worksheets(shtSheet3).Cells(mycell.Row, mycell.Column).Value = ""
Else
ActiveWorkbook.Worksheets(shtSheet3).Cells(mycell.Row, mycell.Column).Value = 0
End If
End If
Next
End Sub
Public Sub CellFormat(shtSheet1 As String, shtSheet2 As String, shtSheet3 As String)
Dim mycell As Range
'This will show the difference of dates, in days, from sheet1 and 2, in sheet3. If it's not different it will still show the date.
For Each mycell In Sheets(shtSheet3).UsedRange
If IsDate(mycell.Value) Then
If Not mycell.Value = Sheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
ActiveWorkbook.Worksheets(shtSheet3).Cells(mycell.Row, mycell.Column).Value = _
ActiveWorkbook.Worksheets(shtSheet2).Cells(mycell.Row, mycell.Column).Value - ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value
End If
End If
Next
'This will format the cells in the date column to be in the General format if the cell is yellow.
For Each mycell In Sheets(shtSheet3).UsedRange
If IsDate(mycell.Value) Then
If mycell.Value <> ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
ActiveWorkbook.Worksheets(shtSheet3).Cells(mycell.Row, mycell.Column).NumberFormat = "#,##0"
ElseIf mycell.Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(mycell.Row, mycell.Column).Value Then
ActiveWorkbook.Worksheets(shtSheet3).Cells(mycell.Row, mycell.Column).NumberFormat = "m/d/yyyy"
End If
End If
Next
End Sub
Sub Sheet3Creation(shtSheet1 As String, shtSheet2 As String, shtSheet3 As String)
Dim shName As String, Wsh As Worksheet
shName = "Sheet3"
'This will loop through existing sheets to see if there is a sheet named "Sheet3". If there is a "Sheet3", then a message box will appear to
'let the user know that "Sheet3" already exists. If not it will exit loop and go to next area where it will create "Sheet3" at the end of
'excel sheets 1 and 2.
For Each Wsh In Sheets
If Wsh.Name = shName Then
If MsgBox("" & shName & " already exists! Please press Yes to continue or No to cancel operation.", vbYesNo) = vbNo Then
End
End If
Exit Sub 'Exit sub will allow the entire sub procedure to end if the "For If" Loop is true. If it's not true it will continue on.
End If
Next
'This section will create a worksheet called "Sheet3" if the "For If" loop above is false.
Set Wsh = ThisWorkbook.Sheets.Add(After:= _
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
Wsh.Name = shName
End Sub
Sub AutoFit(shtSheet1 As String, shtSheet2 As String, shtSheet3 As String)
ActiveWorkbook.Worksheets(shtSheet1).UsedRange.Columns.AutoFit
ActiveWorkbook.Worksheets(shtSheet2).UsedRange.Columns.AutoFit
ActiveWorkbook.Worksheets(shtSheet3).UsedRange.Columns.AutoFit
End Sub