hungvo0102
Thành viên mới
- Tham gia
- 3/11/24
- Bài viết
- 2
- Được thích
- 0
Problem of statement
A set of data has 3 columns of:
- Column A contains the Batch ID
- Column B contains the production date
- Column C contains the ship date
The Batch ID has a two-digit code to the left of the hyphen and a 3- or 4-digit code to the right of the hyphen. The first letter of the Batch ID is known as the Identifier and the leading number of the 3- or 4-digit code to the right of the hyphen is known as the Key. For example, in the Batch ID "N9-363B", the Identifier is "N" and the Key is 3.
Batch ID Prod. Date Ship date
T3-238L 12.10.2017 19.10.2017
N8-462Z 10.12.2017 12.12.2017
A9-488N 22.01.2018 29.01.2018
H3-107R 27.01.2018 05.02.2018
E6-104Q 13.02.2018 21.02.2018
C1-465A 01.03.2018 06.03.2018
Identifier: E
Key: 2
Your goal is to create a subroutine that allows the user to select the Identifier from a drop-down menu in cell F2 and the Key from a drop-down menu in cell F3 (these drop-down/data validation menus are already available in the starter file) and any rows of the data (columns A, B, and C) whose Batch ID meets those criteria will be highlighted GREEN.
- Đây là những sub đã có sẵn
Sub Example()
' This is just to show how the Identifier and Key functions below can be utilized in VBA code
Dim ID As String
ID = "Y4-824X"
MsgBox "The identifier is " & Identifier(ID) & " and the key is " & Key(ID)
End Sub
Function Identifier(ID As String) As String
Identifier = Left(ID, 1)
End Function
Function Key(ID As String) As Integer
Key = Left(Mid(ID, 4, 4), 1)
End Function
Sub Reset()
' Obtained through a macro recording:
With Cells.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub
- Phần bài làm của em:
Sub HighlightRows()
Dim nr As Integer, i As Integer, filterID As String, filterKey As Integer
Call Reset
nr = WorksheetFunction.CountA(Columns("A:A")) - 1
filterID = WorksheetFunction.Identifier(Range("A2:A" & nr + 1))
filterKey = WorksheetFunction.Key(Range("A2:A" & nr + 1))
For i = 1 To nr
If filterKey = Range("key") And filterID = Range("identifier") Then
Range("A" & i + 1).Color.Index = 4
Range("B" & i + 1).Color.Index = 4
Range("C" & i + 1).Color.Index = 4
End If
Next i
End Sub
Nhưng mà ghi chạy bài làm cứ hiện lỗi Error Run time 438. Mong các giáo sư giúp đỡ ạ.
A set of data has 3 columns of:
- Column A contains the Batch ID
- Column B contains the production date
- Column C contains the ship date
The Batch ID has a two-digit code to the left of the hyphen and a 3- or 4-digit code to the right of the hyphen. The first letter of the Batch ID is known as the Identifier and the leading number of the 3- or 4-digit code to the right of the hyphen is known as the Key. For example, in the Batch ID "N9-363B", the Identifier is "N" and the Key is 3.
Batch ID Prod. Date Ship date
T3-238L 12.10.2017 19.10.2017
N8-462Z 10.12.2017 12.12.2017
A9-488N 22.01.2018 29.01.2018
H3-107R 27.01.2018 05.02.2018
E6-104Q 13.02.2018 21.02.2018
C1-465A 01.03.2018 06.03.2018
Identifier: E
Key: 2
Your goal is to create a subroutine that allows the user to select the Identifier from a drop-down menu in cell F2 and the Key from a drop-down menu in cell F3 (these drop-down/data validation menus are already available in the starter file) and any rows of the data (columns A, B, and C) whose Batch ID meets those criteria will be highlighted GREEN.
- Đây là những sub đã có sẵn
Sub Example()
' This is just to show how the Identifier and Key functions below can be utilized in VBA code
Dim ID As String
ID = "Y4-824X"
MsgBox "The identifier is " & Identifier(ID) & " and the key is " & Key(ID)
End Sub
Function Identifier(ID As String) As String
Identifier = Left(ID, 1)
End Function
Function Key(ID As String) As Integer
Key = Left(Mid(ID, 4, 4), 1)
End Function
Sub Reset()
' Obtained through a macro recording:
With Cells.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub
- Phần bài làm của em:
Sub HighlightRows()
Dim nr As Integer, i As Integer, filterID As String, filterKey As Integer
Call Reset
nr = WorksheetFunction.CountA(Columns("A:A")) - 1
filterID = WorksheetFunction.Identifier(Range("A2:A" & nr + 1))
filterKey = WorksheetFunction.Key(Range("A2:A" & nr + 1))
For i = 1 To nr
If filterKey = Range("key") And filterID = Range("identifier") Then
Range("A" & i + 1).Color.Index = 4
Range("B" & i + 1).Color.Index = 4
Range("C" & i + 1).Color.Index = 4
End If
Next i
End Sub
Nhưng mà ghi chạy bài làm cứ hiện lỗi Error Run time 438. Mong các giáo sư giúp đỡ ạ.