【发布时间】:2017-05-04 07:06:49
【问题描述】:
我有一个按钮的功能,一旦我点击它就会被激活,它会在一个名为 (Issue_SumofShares) 的新工作表中显示来自两个相同工作表的数据行(具有特定条件,因此它不会返回所有行) .问题是在交叉检查时,由于将第一张表(NBG_RegionaData)中的所有行与第二张表中的第一行(NBG_ComparisonRegionData)进行比较,然后转到第二张表的第二行,所以重复了很多数据行这样下去。我知道我可以手动或通过“删除重复项”按钮删除重复项(删除第一列和第二列中具有相同值的行),但我想在按下按钮时自动删除重复项,然后再显示结果,所以我添加了 DeleteRows 子,并尝试调用它,但它不起作用,所以有人可以告诉我哪里出错或告诉我如何使重复删除自动在打开工作表之前自动发生(Issue_SumofShares)按下它的按钮后。 这是我的代码:
' A function which shows all the same projects with sum of shares <> 1
Function VerifySumofShares() As Integer
Application.ScreenUpdating = False
Application.Calculation = xlManual
'Get the number of rows in NBG_Data_Comparison_Region
MAX_Row = Sheets(NBG_ComparisonRegionDataWorksheetName).UsedRange.Rows.Count
'Get the number of rows in NBG_Data_Region
MAX_Row1 = Sheets(NBG_RegionaDataWorksheetName).UsedRange.Rows.Count
' having names for each comparing part to make the if statment easier
Dim NBGMonth As String
Dim NBGYear As String
Dim NBGCarmaker As String
Dim NBGProject As String
Dim NBGFamily As String
Dim NBGStatus As String
Dim NBGShare As Integer
Dim NBGCst As String
Dim CompMonth As String
Dim CompYear As String
Dim CompCarmaker As String
Dim CompProject As String
Dim CompFamily As String
Dim CompStatus As String
Dim CompShare As Integer
Dim CompCst As String
Dim RNumber As Integer
'Count the Sum of shares for same projects which <> 1
Issue_SumofSharesCnt = 0
Issue_SumofSharesWorksheetName = "Issue_SumofShares"
' Clear Issue Som of Shares Data Sheet
Worksheets(Issue_SumofSharesWorksheetName).Cells.Clear
' Customize Issue_SumofShares sheet
Worksheets(Issue_SumofSharesWorksheetName).Cells(1, 1) = "Report of projects with multiple customers and Sum of Shares that does not equal 100%"
With Worksheets(Issue_SumofSharesWorksheetName).Cells(1, 1).Font
.Bold = True
.Size = 14
.color = RGB(255, 0, 0)
End With
SOP = "C"
Status = "AD"
Customer = "A"
Product = "B"
Responsible = "AT"
Family = "AA"
Project = "AB"
carmaker = "AJ"
Share = "BQ"
GeoRegion = "BF"
With Worksheets(Issue_SumofSharesWorksheetName)
.Range("A2") = "Data Row"
.Range("F2") = "Project"
.Range("C2") = "SOP (dd-Month-yy QQ)"
.Range("D2") = "Product"
.Range("I2") = "Responsible"
.Range("E2") = "Family"
.Range("G2") = "Carmaker"
.Range("H2") = "Share"
.Range("B2") = "Customer"
.Range("J2") = "Region"
.Range("K2") = "Status"
.Range("A2:Z2").Font.Bold = True
End With
' Take the data of the NBG_Data_Comparison_Region
For Row = 2 To MAX_Row
'CompMonth = Worksheets(NBG_ComparisonRegionDataWorksheetName).Cells(Row, SOP).Value
'CompMonth = DatePart("m", CompMonth)
CompYear = Worksheets(NBG_ComparisonRegionDataWorksheetName).Cells(Row, SOP).Value
CompYear = DatePart("yyyy", CompYear)
CompCarmaker = Worksheets(NBG_ComparisonRegionDataWorksheetName).Cells(Row, carmaker).Value
CompProject = Worksheets(NBG_ComparisonRegionDataWorksheetName).Cells(Row, Project).Value
CompFamily = Worksheets(NBG_ComparisonRegionDataWorksheetName).Cells(Row, Family).Value
CompStatus = Worksheets(NBG_ComparisonRegionDataWorksheetName).Cells(Row, Status).Value
CompShare = Worksheets(NBG_ComparisonRegionDataWorksheetName).Cells(Row, Share).Value
CompCst = Worksheets(NBG_ComparisonRegionDataWorksheetName).Cells(Row, "A").Value
' Take the data from NBG_Data_Region sheet to be compared with each row of the NBG_Data_Comparison_Region sheet
For Row1 = 2 To MAX_Row1
If Row1 >= MAX_Row1 Then
Exit For
End If
'NBGMonth = Worksheets(NBG_RegionaDataWorksheetName).Cells(Row1, SOP).Value
'NBGMonth = DatePart("m", NBGMonth)
NBGYear = Worksheets(NBG_RegionaDataWorksheetName).Cells(Row1, SOP).Value
NBGYear = DatePart("yyyy", NBGYear)
NBGCarmaker = Worksheets(NBG_RegionaDataWorksheetName).Cells(Row1, carmaker).Value
NBGProject = Worksheets(NBG_RegionaDataWorksheetName).Cells(Row1, Project).Value
NBGFamily = Worksheets(NBG_RegionaDataWorksheetName).Cells(Row1, Family).Value
NBGStatus = Worksheets(NBG_RegionaDataWorksheetName).Cells(Row1, Status).Value
NBGShare = Worksheets(NBG_RegionaDataWorksheetName).Cells(Row1, Share).Value
NBGCst = Worksheets(NBG_RegionaDataWorksheetName).Cells(Row1, "A").Value
' StatusBar Show
Application.StatusBar = "VerifySumofShares. Progress: " & Row & " of " & MAX_Row
'Check if any row with a SOP date in the previous or current years and if it is a D-IN or OPP is found and add it to the IssueSOP_Date sheet
' NAF 20161208
'Test with comparison of YEAR and MONTH
' If (NBGMonth = CompMonth And NBGYear = CompYear And CompCarmaker = NBGCarmaker And CompProject = NBGProject And CompFamily = NBGFamily And CompShare + NBGShare <> 1 And NBGCst <> CompCst) Then
' With Year only
If (NBGYear = CompYear And CompCarmaker = NBGCarmaker And CompProject = NBGProject And CompFamily = NBGFamily And CompShare + NBGShare <> 1 And NBGCst <> CompCst) Then
'Customization of the Issue_SumofShares sheet to show the NBG Data Row , Cst, SOP , Product, Responsible,Family , Carmaker , Share , Status and the GeoRegion of the data which the condition applies to
'NBGStatus <> "LOST" And CompStatus <> "LOST" And
'And CompCarmaker = NBGCarmaker And CompProject = NBGProject And CompFamily = NBGFamily And CompShare + NBGShare <= 0.99 And CompShare + NBGShare > 1
Worksheets(Issue_SumofSharesWorksheetName).Cells(3 + Issue_SumofSharesCnt, "A").Value = Row1
Worksheets(Issue_SumofSharesWorksheetName).Cells(3 + Issue_SumofSharesCnt, "B").Value = Worksheets(NBG_RegionaDataWorksheetName).Cells(Row1, Customer).Value
Worksheets(Issue_SumofSharesWorksheetName).Cells(3 + Issue_SumofSharesCnt, "C").Value = GetMonthAndQuarter(Worksheets(NBG_RegionaDataWorksheetName).Cells(Row1, SOP).Value)
Worksheets(Issue_SumofSharesWorksheetName).Cells(3 + Issue_SumofSharesCnt, "D").Value = Worksheets(NBG_RegionaDataWorksheetName).Cells(Row1, Product).Value
Worksheets(Issue_SumofSharesWorksheetName).Cells(3 + Issue_SumofSharesCnt, "E").Value = Worksheets(NBG_RegionaDataWorksheetName).Cells(Row1, Family).Value
Worksheets(Issue_SumofSharesWorksheetName).Cells(3 + Issue_SumofSharesCnt, "F").Value = Worksheets(NBG_RegionaDataWorksheetName).Cells(Row1, Project).Value
Worksheets(Issue_SumofSharesWorksheetName).Cells(3 + Issue_SumofSharesCnt, "G").Value = Worksheets(NBG_RegionaDataWorksheetName).Cells(Row1, carmaker).Value
Worksheets(Issue_SumofSharesWorksheetName).Cells(3 + Issue_SumofSharesCnt, "H").Value = Worksheets(NBG_RegionaDataWorksheetName).Cells(Row1, Share).Value
Worksheets(Issue_SumofSharesWorksheetName).Cells(3 + Issue_SumofSharesCnt, "I").Value = Worksheets(NBG_RegionaDataWorksheetName).Cells(Row1, Responsible).Value
Worksheets(Issue_SumofSharesWorksheetName).Cells(3 + Issue_SumofSharesCnt, "K").Value = Worksheets(NBG_RegionaDataWorksheetName).Cells(Row1, Status).Value
' Region As String
Region = ""
'Add any other GeoRegion which is also responsible in the recorded data
If Worksheets(NBG_DataWorksheetName).Cells(Row1, "BC") Then
Region = Region + "@EMEA"
End If
If Worksheets(NBG_DataWorksheetName).Cells(Row1, "BD") Then
Region = Region + "@AMERICAS"
End If
If Worksheets(NBG_DataWorksheetName).Cells(Row1, "BE") Then
Region = Region + "@GCSA"
End If
If Worksheets(NBG_DataWorksheetName).Cells(Row1, "BF") Then
Region = Region + "@JAPAN&KOREA"
End If
Worksheets(Issue_SumofSharesWorksheetName).Cells(3 + Issue_SumofSharesCnt, "J").Value = Region
'Count the number of the cases recorded
Issue_SumofSharesCnt = Issue_SumofSharesCnt + 1
'If there is no items , the Message to show
ElseIf (Worksheets(NBG_ComparisonRegionDataWorksheetName).Cells(Row, SOP).Value = "There are no items to show in this view.") Then
End If
Call DeleteRows
Next Row1
Next Row
' Send the Counter to show on the Menu sheet on the button involved
VerifySumofShares = Issue_SumofSharesCnt
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
End Function
Sub DeleteRows()
Dim Rng As Range
With Issue_SumofSharesWorksheetName
Set Rng = Range("A1", Range("B1").End(xlDown))
Rng.RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes
End With
End Sub
【问题讨论】:
标签: excel vba duplicates