Data_Crunched
New Member
- Joined
- Jun 11, 2019
- Messages
- 3
Hi all, new to the group, but will do my best to make this as clear as possible. I searched up and down and couldn't find a solution to this problem.
I have a macro that searches for specific trades that are listed within cells of an Excel spreadsheet and replaces the exported craft names with shortened versions (eg. Carpenter is shortened to CARP, Electrician is shortened to ELECT, etc.). The number in parenthesis is the quanity of each craft required to perform the job, and it exports into Excel like that from our EPR database. Many work orders require multiple crafts, which is why in my example below, you'll see multiple crafts listed in one cell, delineated by a comma. I'd like the macro to completely remove specific crafts if possible (Manufacturing Engineer, Electrical Engineer, Operations Coordinator, Facility Manager), there are other crafts I want to remove, but as long as I can see an example of how one is done, I can duplicate it.
Here's an example of the macro I'm working with, and it seems to work well with everything I'm doing(i.e. search and replace), but I can't figure out how to modify it to remove, delete or ignore specific crafts completely. I only pasted the top third of the macro as it's quite long and didn't feel it was necessary beyond the area in red font, which is what I mostly modify for search and replace.
Dim fromdateuserinput As String 'This variable will store the beginning date of the range of dates that will be kept in the data; it will be input by the user
Dim throughdateuserinput As String 'This variable will store the end date of the range of dates that will be kept in the data; it will be input by the user
Do Until IsDate(fromdateuserinput) = True 'This loop will continue until a date is captured with correct syntax, or until the user cancels the macro entirely
fromdateuserinput = InputBox("Please input the beginning date of the range of dates you desire to show in the Schedule (format mm/dd/yy)", _
"Input From Date", Format(Now(), "mm/dd/yy")) 'The InputBox creates a pop-up that will prompt the user for the beginning of the date range of WOs to be captured
If IsDate(fromdateuserinput) Then 'Checks if the input string from the user is a date
fromdateuserinput = Format(CDate(fromdateuserinput), "mm/dd/yy Hh:Nn:Ss AM/PM") 'If the string is a date, the formatting of it is changed to include hours, minutes, and seconds (for convenience while working in VBA)
ElseIf fromdateuserinput = "" Then 'If the string is empty, then the macro is cancelled. The string is always empty if the user cancels/closes the pop-up
MsgBox "Macro has been cancelled; user input was null."
Exit Sub
Else
MsgBox "Wrong date format...Try Again" 'If the string input by the user was not formatted correctly, the user will be informed, and the loop will repeat, prompting the user again for a date
End If
Loop
Do Until IsDate(throughdateuserinput) = True 'This loop will continue until a date is captured with correct syntax, or until the user cancels the macro entirely
throughdateuserinput = InputBox("Please input the ending date of the range of dates you desire to show in the Schedule (format mm/dd/yy)", _
"Input Through Date", Format(Now(), "mm/dd/yy")) 'The InputBox creates a pop-up that will prompt the user for the end of the date range of WOs to be captured
If IsDate(throughdateuserinput) Then 'Checks if the input string from the user is a date
throughdateuserinput = Format(CDate(throughdateuserinput), "mm/dd/yy Hh:Nn:Ss AM/PM") 'If the string is a date, the formatting of it is changed to include hours, minutes, and seconds (for convenience while working in VBA)
ElseIf throughdateuserinput = "" Then 'If the string is empty, then the macro is cancelled. The string is always empty if the user cancels/closes the pop-up
MsgBox "Macro has been cancelled; user input was null."
Exit Sub
Else
MsgBox "Wrong date format" 'If the string input by the user was not formatted correctly, the user will be informed, and the loop will repeat, prompting the user again for a date
End If
Loop
throughdateuserinput = DateAdd("h", 23, throughdateuserinput) '11 hours, 59 minutes, and 59s are added to the end date that the user input so that logic will correctly below
throughdateuserinput = DateAdd("n", 59, throughdateuserinput) 'For example, if the user input the date 5/7/2018 as the end date, this code will make sure that the logic below keeps data/WOs
throughdateuserinput = DateAdd("s", 59, throughdateuserinput) 'with schedule dates on 5/7/18 itself
Dim lastrowindex As Long
lastrowindex = Cells(Rows.Count, "A").End(xlUp).Row 'lastrowindex = number of rows of data on the sheet. It is based off the Status column (currently column F) because the status field always contains data when exporting
ActiveSheet.Range("A3:M" & lastrowindex).RemoveDuplicates Columns:=2, Header:=xlYes 'Remove Duplicates based on Work Order number data field. I replaced the fixed range that the Macro recorded with ("A1:J" & lastrowindex) which identifies all the data
lastrowindex = Cells(Rows.Count, "A").End(xlUp).Row 'reset the lastrowindex as number of rows may have decreased with the duplicates removed
Range("J4:J" & lastrowindex).Select 'Replacment in Periodicity column
Selection.Replace What:="NOT APPLICABLE", Replacement:=" ", LookAt:= _
xlWhole, SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Range("K4:K" & lastrowindex).Select
Selection.Replace What:="Escort Required", Replacement:="ESCORT", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="Cal Lab(1)", Replacement:="WSVS[1]", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="Cal Lab(2)", Replacement:="WSVS[2]", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="Contamination Control Technician", Replacement:="CCT", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="Carpenter(1)", Replacement:="CARP[1]", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="Carpenter(2)", Replacement:="CARP[2]", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Here are the row outputs, column K header is 'Resources' and this would be an example of what would appear in cells K1:K10:
<colgroup><col style="mso-width-source:userset;mso-width-alt:19968;width:410pt" width="546"> </colgroup><tbody>
[TD="class: xl72, width: 546"]Row
1 ET(4), EE(2), OC(4), DRVR(2), INSP(4), MT(10), SAF(1), FLTMECH(1) , ME(5)
[/TD]
[TD="class: xl72, width: 546"]2 FAB(1), WELD(1), PAINT(1)
[/TD]
[TD="class: xl72, width: 546"]3 EE(1), INSP(1), MACH(1)
[/TD]
[TD="class: xl72, width: 546"]4 FAB(1), ELECT(1)
[/TD]
[TD="class: xl72, width: 546"]5 FAB(1), INSP(1)
[/TD]
[TD="class: xl72, width: 546"]6 FAB(2), WELD(1), INSP(1), MT(2), MACH(1)
[/TD]
[TD="class: xl72, width: 546"]7 CARP(2)
[/TD]
[TD="class: xl72, width: 546"]8 FLTMECH(1)
[/TD]
[TD="class: xl72, width: 546"]9 ET(2), EE(1), OC(1), MT(2), TBMCI(1), ME(4)
[/TD]
[TD="class: xl72, width: 546"]10 OC(1), INSP(1), MT(1) , ME(1)
[/TD]
</tbody>
In the outputs listed above, I'd like to remove any instance of ME, EE, OC
Thanks in advance for your help!
I have a macro that searches for specific trades that are listed within cells of an Excel spreadsheet and replaces the exported craft names with shortened versions (eg. Carpenter is shortened to CARP, Electrician is shortened to ELECT, etc.). The number in parenthesis is the quanity of each craft required to perform the job, and it exports into Excel like that from our EPR database. Many work orders require multiple crafts, which is why in my example below, you'll see multiple crafts listed in one cell, delineated by a comma. I'd like the macro to completely remove specific crafts if possible (Manufacturing Engineer, Electrical Engineer, Operations Coordinator, Facility Manager), there are other crafts I want to remove, but as long as I can see an example of how one is done, I can duplicate it.
Here's an example of the macro I'm working with, and it seems to work well with everything I'm doing(i.e. search and replace), but I can't figure out how to modify it to remove, delete or ignore specific crafts completely. I only pasted the top third of the macro as it's quite long and didn't feel it was necessary beyond the area in red font, which is what I mostly modify for search and replace.
Dim fromdateuserinput As String 'This variable will store the beginning date of the range of dates that will be kept in the data; it will be input by the user
Dim throughdateuserinput As String 'This variable will store the end date of the range of dates that will be kept in the data; it will be input by the user
Do Until IsDate(fromdateuserinput) = True 'This loop will continue until a date is captured with correct syntax, or until the user cancels the macro entirely
fromdateuserinput = InputBox("Please input the beginning date of the range of dates you desire to show in the Schedule (format mm/dd/yy)", _
"Input From Date", Format(Now(), "mm/dd/yy")) 'The InputBox creates a pop-up that will prompt the user for the beginning of the date range of WOs to be captured
If IsDate(fromdateuserinput) Then 'Checks if the input string from the user is a date
fromdateuserinput = Format(CDate(fromdateuserinput), "mm/dd/yy Hh:Nn:Ss AM/PM") 'If the string is a date, the formatting of it is changed to include hours, minutes, and seconds (for convenience while working in VBA)
ElseIf fromdateuserinput = "" Then 'If the string is empty, then the macro is cancelled. The string is always empty if the user cancels/closes the pop-up
MsgBox "Macro has been cancelled; user input was null."
Exit Sub
Else
MsgBox "Wrong date format...Try Again" 'If the string input by the user was not formatted correctly, the user will be informed, and the loop will repeat, prompting the user again for a date
End If
Loop
Do Until IsDate(throughdateuserinput) = True 'This loop will continue until a date is captured with correct syntax, or until the user cancels the macro entirely
throughdateuserinput = InputBox("Please input the ending date of the range of dates you desire to show in the Schedule (format mm/dd/yy)", _
"Input Through Date", Format(Now(), "mm/dd/yy")) 'The InputBox creates a pop-up that will prompt the user for the end of the date range of WOs to be captured
If IsDate(throughdateuserinput) Then 'Checks if the input string from the user is a date
throughdateuserinput = Format(CDate(throughdateuserinput), "mm/dd/yy Hh:Nn:Ss AM/PM") 'If the string is a date, the formatting of it is changed to include hours, minutes, and seconds (for convenience while working in VBA)
ElseIf throughdateuserinput = "" Then 'If the string is empty, then the macro is cancelled. The string is always empty if the user cancels/closes the pop-up
MsgBox "Macro has been cancelled; user input was null."
Exit Sub
Else
MsgBox "Wrong date format" 'If the string input by the user was not formatted correctly, the user will be informed, and the loop will repeat, prompting the user again for a date
End If
Loop
throughdateuserinput = DateAdd("h", 23, throughdateuserinput) '11 hours, 59 minutes, and 59s are added to the end date that the user input so that logic will correctly below
throughdateuserinput = DateAdd("n", 59, throughdateuserinput) 'For example, if the user input the date 5/7/2018 as the end date, this code will make sure that the logic below keeps data/WOs
throughdateuserinput = DateAdd("s", 59, throughdateuserinput) 'with schedule dates on 5/7/18 itself
Dim lastrowindex As Long
lastrowindex = Cells(Rows.Count, "A").End(xlUp).Row 'lastrowindex = number of rows of data on the sheet. It is based off the Status column (currently column F) because the status field always contains data when exporting
ActiveSheet.Range("A3:M" & lastrowindex).RemoveDuplicates Columns:=2, Header:=xlYes 'Remove Duplicates based on Work Order number data field. I replaced the fixed range that the Macro recorded with ("A1:J" & lastrowindex) which identifies all the data
lastrowindex = Cells(Rows.Count, "A").End(xlUp).Row 'reset the lastrowindex as number of rows may have decreased with the duplicates removed
Range("J4:J" & lastrowindex).Select 'Replacment in Periodicity column
Selection.Replace What:="NOT APPLICABLE", Replacement:=" ", LookAt:= _
xlWhole, SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Range("K4:K" & lastrowindex).Select
Selection.Replace What:="Escort Required", Replacement:="ESCORT", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="Cal Lab(1)", Replacement:="WSVS[1]", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="Cal Lab(2)", Replacement:="WSVS[2]", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="Contamination Control Technician", Replacement:="CCT", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="Carpenter(1)", Replacement:="CARP[1]", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="Carpenter(2)", Replacement:="CARP[2]", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Here are the row outputs, column K header is 'Resources' and this would be an example of what would appear in cells K1:K10:
<colgroup><col style="mso-width-source:userset;mso-width-alt:19968;width:410pt" width="546"> </colgroup><tbody>
[TD="class: xl72, width: 546"]Row
1 ET(4), EE(2), OC(4), DRVR(2), INSP(4), MT(10), SAF(1), FLTMECH(1) , ME(5)
[/TD]
[TD="class: xl72, width: 546"]2 FAB(1), WELD(1), PAINT(1)
[/TD]
[TD="class: xl72, width: 546"]3 EE(1), INSP(1), MACH(1)
[/TD]
[TD="class: xl72, width: 546"]4 FAB(1), ELECT(1)
[/TD]
[TD="class: xl72, width: 546"]5 FAB(1), INSP(1)
[/TD]
[TD="class: xl72, width: 546"]6 FAB(2), WELD(1), INSP(1), MT(2), MACH(1)
[/TD]
[TD="class: xl72, width: 546"]7 CARP(2)
[/TD]
[TD="class: xl72, width: 546"]8 FLTMECH(1)
[/TD]
[TD="class: xl72, width: 546"]9 ET(2), EE(1), OC(1), MT(2), TBMCI(1), ME(4)
[/TD]
[TD="class: xl72, width: 546"]10 OC(1), INSP(1), MT(1) , ME(1)
[/TD]
</tbody>
In the outputs listed above, I'd like to remove any instance of ME, EE, OC
Thanks in advance for your help!