我有如下所示的代码,我尝试在代码中解释我正在做什么作为评论。
我想要的结果也作为图像附加。
但是,当我单击该命令按钮时,我只得到如图1所示的结果。我必须再次单击按钮以获得结果(计数),如图2所示。 而且我第三次必须单击按钮才能得到结果(排序计数),如图3所示。
我想通过单击命令按钮获得最终结果(如图3所示)。
我无法理解出现了什么问题,因为我没有收到任何错误。请帮我理解我的代码中的错误。 另外,请建议如何解决此问题。
'I have a sheet named 'Search' which has the Active X control Command Button 'CommandButton2'
Private Sub CommandButton2_Click()
Dim s1, s2 As Worksheet
Dim lrs1, lrs2, lrs3, lrs4, lrs5, lrs6, lrs7, lrs8 As Long
Dim cnt, row As Integer
Dim j, i, a As Long
'I have 2 sheets as below
Set s1 = Sheets("FG_Data")
Set s2 = Sheets("Data_Analysis")
'I am copying the Range A:B from sheet s1 to sheet s2
s1.Range("A:B").EntireColumn.Copy
s2.Paste s2.Range("B1")
'Get the last row for sheet 'FG_Data'
lrs1 = s1.Cells(Rows.Count, "A").End(xlUp).row
'Get the last row for sheet 'Data_Analysis'
'I am doing for all because the number of records are dynamic based on how many unique values of function group you get.
lrs2 = s2.Cells(Rows.Count, "B").End(xlUp).row
lrs3 = s2.Cells(Rows.Count, "D").End(xlUp).row
lrs4 = s2.Cells(Rows.Count, "E").End(xlUp).row
lrs5 = s2.Cells(Rows.Count, "G").End(xlUp).row
lrs6 = s2.Cells(Rows.Count, "I").End(xlUp).row
lrs7 = s2.Cells(Rows.Count, "K").End(xlUp).row
lrs8 = s2.Cells(Rows.Count, "M").End(xlUp).row
'Standardizing the column hearders. Hard coding it on the cell
s2.Range("D1") = "Function Group"
s2.Range("E1") = "PROTUS - F"
s2.Range("F1") = "Function Group"
s2.Range("G1") = "PROTUS - ALP"
s2.Range("H1") = "Function Group"
s2.Range("I1") = "PRAudit"
s2.Range("J1") = "Function Group"
s2.Range("K1") = "QULIS"
s2.Range("L1") = "Function Group"
s2.Range("M1") = "FF1"
'Get the unique values of Function Group.
'I am getting the uniques values of Column B from that range I copied from s1 to s2
s1.Range("B2:B" & lrs1).Copy s2.Range("D2")
s2.Range("D:D").RemoveDuplicates Columns:=1, Header:=xlNo
s1.Range("B2:B" & lrs1).Copy s2.Range("F2")
s2.Range("F:F").RemoveDuplicates Columns:=1, Header:=xlNo
s1.Range("B2:B" & lrs1).Copy s2.Range("H2")
s2.Range("H:H").RemoveDuplicates Columns:=1, Header:=xlNo
s1.Range("B2:B" & lrs1).Copy s2.Range("J2")
s2.Range("J:J").RemoveDuplicates Columns:=1, Header:=xlNo
s1.Range("B2:B" & lrs1).Copy s2.Range("L2")
s2.Range("L:L").RemoveDuplicates Columns:=1, Header:=xlNo
'count the number of issues for each function group in PROTUS - F Report
For j = 2 To lrs3
cnt = 0
For i = 2 To lrs2
If s2.Cells(i, 2).Value = "PROTUS - F Report" And s2.Cells(i, 3).Value = s2.Cells(j, 4).Value Then
cnt = cnt + 1
End If
Next i
s2.Cells(j, 5).Value = cnt
cnt = 0
Next j
'count the number of issues for each function group in PROTUS - ALP Report
For j = 2 To lrs3
cnt = 0
For i = 2 To lrs2
If s2.Cells(i, 2).Value = "PROTUS - ALP Report" And s2.Cells(i, 3).Value = s2.Cells(j, 4).Value Then
cnt = cnt + 1
End If
Next i
s2.Cells(j, 7).Value = cnt
cnt = 0
Next j
'count the number of issues for each function group in Product Audit report
For j = 2 To lrs3
cnt = 0
For i = 2 To lrs2
If s2.Cells(i, 2).Value = "Product Audit" And s2.Cells(i, 3).Value = s2.Cells(j, 4).Value Then
cnt = cnt + 1
End If
Next i
s2.Cells(j, 9).Value = cnt
cnt = 0
Next j
'count the number of issues for each function group in QULIS report
For j = 2 To lrs3
cnt = 0
For i = 2 To lrs2
If s2.Cells(i, 2).Value = "QULIS" And s2.Cells(i, 3).Value = s2.Cells(j, 4).Value Then
cnt = cnt + 1
End If
Next i
s2.Cells(j, 11).Value = cnt
cnt = 0
Next j
'count the number of issues for each function group in FF1 report
For j = 2 To lrs3
cnt = 0
For i = 2 To lrs2
If s2.Cells(i, 2).Value = "FF1" And s2.Cells(i, 3).Value = s2.Cells(j, 4).Value Then
cnt = cnt + 1
End If
Next i
s2.Cells(j, 13).Value = cnt
cnt = 0
Next j
'Sorting the count of issues data in all the columns in descending order
s2.Range("D2:E" & lrs3).Sort key1:=s2.Range("E2:E" & lrs3), _
order1:=xlDescending, Header:=xlNo
s2.Range("F2:G" & lrs4).Sort key1:=s2.Range("G2:G" & lrs4), _
order1:=xlDescending, Header:=xlNo
s2.Range("H2:I" & lrs5).Sort key1:=s2.Range("I2:I" & lrs5), _
order1:=xlDescending, Header:=xlNo
s2.Range("J2:K" & lrs6).Sort key1:=s2.Range("K2:K" & lrs6), _
order1:=xlDescending, Header:=xlNo
s2.Range("L2:M" & lrs8).Sort key1:=s2.Range("M2:M" & lrs8), _
order1:=xlDescending, Header:=xlNo
'Standardizing the sheet color. Just some beutification of the sheet
s2.Cells.Interior.ColorIndex = 0
s2.Cells.Font.color = vbBlack
s2.Cells.Font.Bold = False
s2.Columns.AutoFit
End Sub