Trying to write and Excel VBA for searching a list of text strings for keywords based on an array and then writing specific values to adjacent cells

JLoewen

New Member
Joined
Jun 20, 2018
Messages
29
I am working on a program for assigning ASHRAE room types and specific building code standards to rooms based on their room name.

My approach has been to create worksheets - one with my list of keyword strings, the ASHRAE standard and the room type and another spreadsheet with my actual room names on it and the cells I want the VBA to fill in.

Here's the Room Library sheet - Key word string list, ASHRAE standard corresponding room type.
Key Word StringsAshrae StandardRoom type
1​
AlcoveASHRAE 170Patient Corridor
2​
AppointmentASHRAE 170Examination room
3​
ApptASHRAE 170Examination room
4​
BreakroomASHRAE 62.1Breakrooms
5​
CanteenASHRAE 62.1Cafeteria/fast-food dining
6​
ClassroomASHRAE 62.1Lecture Classroom
7​
CommunicationsASHRAE 62.1Office Space
8​
ConferenceASHRAE 62.1Conference/meeting
9​
ConsultASHRAE 170Examination room
10​
consultationASHRAE 170Examination room
11​
CopyASHRAE 62.1Office Copy, printing rooms
12​
CorridorASHRAE 170Patient Corridor
13​
CubicleASHRAE 62.1Office Space
14​
DockASHRAE 62.1SHIPPING/RECEIVING
15​
ExamASHRAE 170Examination room
16​
HallASHRAE 170Patient Corridor
17​
KioskASHRAE 62.1
18​
Kitchenette, LoungeASHRAE 62.1Breakrooms
19​
LactationASHRAE 62.1Office Space
20​
LockerASHRAE 62.1Locker for health care
21​
LoungeASHRAE 62.1Breakrooms
22​
MailASHRAE 62.1Office Copy, printing rooms
23​
MechASHRAE 62.1Not Regularly Occupied
24​
Medical, StorageASHRAE 170Sterile Storage
25​
MedicationASHRAE 170Medication room
26​
NurseASHRAE 170Patient Corridor
27​
OfficeASHRAE 62.1Office Space
28​
OperationsASHRAE 62.1Office Space
29​
ProcedureASHRAE 170Procedure Room (Class A Surgery)
30​
ProceduresASHRAE 170Procedure Room (Class A Surgery)
31​
ReceptionASHRAE 62.1Reception areas
32​
SecretaryASHRAE 62.1Office Space


Here's the sheet with my actual room names on it. The intent is to have a program that searches through the VENTILATION ZONE column for keywords and then assigns the ASHRAE Standard and Room type based on the string found.

Ventilation Zone
ASHRAE StandardSpace Use Category
0111 - EXAM, AUDIO BOOTH
0112 - VENDING
0113 - CANTEEN, PACT 3
0118 - LAB
0119 - COMBINED HYGIENE OPERATORY
0120 - MECH ROOM, DENTAL EQUIP
0121 - SOILED SPD CART
0124 - X-RAY
0126 - DTR
0134 - EXAM/CONSULT
0136 - HEARING AID FITTING
0137 - FITTING & IMMITANCE
0333 - EXAM/CONSULT
0334 - EXAM, AUDIO BOOTH
0335 - EXAM, AUDIO BOOTH
0336 - STORAGE, EQUIP
0341 - STORAGE, INSTRUMENT
0342 - COMBINED HYGIENE OPERATORY
0343 - DTR
0344 - DTR
0345 - DTR
0346 - WORKSTATION, DS
0347 - STAFF LOUNGE
0348 - OFFICE, DENTIST
0349 - WORKSTATION, DS
0350 - WORKSTATION, DS
0351 - RECEPTION
0352 - STORAGE, EQUIP
0353 - TLT, STAFF
0354 - TLT, PAT
0355 - CONFERENCE
0356 - OFFICE, FTE COMMUNITY
0357 - OFFICE, OI&T
0358 - CONFERENCE
0359 - OFFICE, FTE COMMUNITY
0360 - OFFICE, EDUCATOR
0361 - ALCOVE, PRINTER
0362 - OFFICE, EDUCATOR
0363 - STORAGE, EDU MATERIAL
0364 - CONFERENCE
0365 - OFFICE, OI&T
0366 - OFFICE, FMS
0367 - OFFICE, FMS
0368 - REPAIR SHOP, BIOMED
0369 - EXAM
0370 - EXAM, TRAINING
0371 - UTILITY, CLEAN
0372 - UTILITY, SOILED
0373 - OPTICAL SHOP


I have some (very little) experience with VBA programming and want to use this as a learning opportunity. The trouble is I do not know the basics of doing keyword search and match based on multiple arrays. Can you offer a few hints or suggestions?
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Why not just use a formula

+Fluff New.xlsm
ABCDEFGH
1Key Word StringsAshrae StandardRoom typeVentilation ZoneASHRAE StandardSpace Use Category
2AlcoveASHRAE 170Patient Corridor0111 - EXAM, AUDIO BOOTHASHRAE 170Examination room
3AppointmentASHRAE 170Examination room0112 - VENDING  
4ApptASHRAE 170Examination room0113 - CANTEEN, PACT 3ASHRAE 62.1Cafeteria/fast-food dining
5BreakroomASHRAE 62.1Breakrooms0118 - LAB  
6CanteenASHRAE 62.1Cafeteria/fast-food dining0119 - COMBINED HYGIENE OPERATORY  
7ClassroomASHRAE 62.1Lecture Classroom0120 - MECH ROOM, DENTAL EQUIPASHRAE 62.1Not Regularly Occupied
8CommunicationsASHRAE 62.1Office Space0121 - SOILED SPD CART  
9ConferenceASHRAE 62.1Conference/meeting0124 - X-RAY  
10ConsultASHRAE 170Examination room0126 - DTR  
11consultationASHRAE 170Examination room0134 - EXAM/CONSULTASHRAE 170Examination room
12CopyASHRAE 62.1Office Copy, printing rooms0136 - HEARING AID FITTING  
13CorridorASHRAE 170Patient Corridor0137 - FITTING & IMMITANCE  
14CubicleASHRAE 62.1Office Space0333 - EXAM/CONSULTASHRAE 170Examination room
15DockASHRAE 62.1SHIPPING/RECEIVING0334 - EXAM, AUDIO BOOTHASHRAE 170Examination room
16ExamASHRAE 170Examination room0335 - EXAM, AUDIO BOOTHASHRAE 170Examination room
17HallASHRAE 170Patient Corridor0336 - STORAGE, EQUIP  
18KioskASHRAE 62.10341 - STORAGE, INSTRUMENT  
19Kitchenette, LoungeASHRAE 62.1Breakrooms0342 - COMBINED HYGIENE OPERATORY  
20LactationASHRAE 62.1Office Space0343 - DTR  
21LockerASHRAE 62.1Locker for health care0344 - DTR  
22LoungeASHRAE 62.1Breakrooms0345 - DTR  
23MailASHRAE 62.1Office Copy, printing rooms0346 - WORKSTATION, DS  
24MechASHRAE 62.1Not Regularly Occupied0347 - STAFF LOUNGEASHRAE 62.1Breakrooms
25Medical, StorageASHRAE 170Sterile Storage0348 - OFFICE, DENTISTASHRAE 62.1Office Space
26MedicationASHRAE 170Medication room0349 - WORKSTATION, DS  
27NurseASHRAE 170Patient Corridor0350 - WORKSTATION, DS  
28OfficeASHRAE 62.1Office Space0351 - RECEPTIONASHRAE 62.1Reception areas
29OperationsASHRAE 62.1Office Space0352 - STORAGE, EQUIP  
30ProcedureASHRAE 170Procedure Room (Class A Surgery)0353 - TLT, STAFF  
31ProceduresASHRAE 170Procedure Room (Class A Surgery)0354 - TLT, PAT  
32ReceptionASHRAE 62.1Reception areas0355 - CONFERENCEASHRAE 62.1Conference/meeting
33SecretaryASHRAE 62.1Office Space0356 - OFFICE, FTE COMMUNITYASHRAE 62.1Office Space
340357 - OFFICE, OI&TASHRAE 62.1Office Space
350358 - CONFERENCEASHRAE 62.1Conference/meeting
360359 - OFFICE, FTE COMMUNITYASHRAE 62.1Office Space
370360 - OFFICE, EDUCATORASHRAE 62.1Office Space
380361 - ALCOVE, PRINTERASHRAE 170Patient Corridor
390362 - OFFICE, EDUCATORASHRAE 62.1Office Space
400363 - STORAGE, EDU MATERIAL  
410364 - CONFERENCEASHRAE 62.1Conference/meeting
420365 - OFFICE, OI&TASHRAE 62.1Office Space
430366 - OFFICE, FMSASHRAE 62.1Office Space
440367 - OFFICE, FMSASHRAE 62.1Office Space
450368 - REPAIR SHOP, BIOMED  
460369 - EXAMASHRAE 170Examination room
470370 - EXAM, TRAININGASHRAE 170Examination room
480371 - UTILITY, CLEAN  
490372 - UTILITY, SOILED  
500373 - OPTICAL SHOP  
List
Cell Formulas
RangeFormula
G2:H50G2=IFERROR(LOOKUP(10^10,1/(SEARCH($A$2:$A$33,$F2)),B$2:B$33),"")
 
Upvote 0
I considered a formula (that's something I am pretty good at writing) but decided it probably wasn't the best approach.

The purpose of the VBA is to take a first pass at assigning the room types and ASHRAE codes. I know it will not be able to fill in all of them and that I will be adding more room names/types to the list as I go. I want to be able to add stuff to that list and then re-run the routine.

If I use an excel formula I will have the end user (possibly someone besides myself) erasing excel formulas in two cells and then manually entering data.

With a VBA I can simply have those cells be blank and the user select from a drop-down box to fill in the cells not addressed by the VBA.
 
Upvote 0
Ok, based on the layout in post#2, how about
VBA Code:
Sub JLoewen()
   Dim Kary As Variant, Dary As Variant
   Dim i As Long, j As Long
   
   Kary = Range("A2", Range("A" & Rows.Count).End(xlUp).Offset(, 2)).Value
   Dary = Range("F2", Range("F" & Rows.Count).End(xlUp).Offset(, 2)).Value
   For i = 1 To UBound(Dary)
      For j = 1 To UBound(Kary)
         If InStr(1, Dary(i, 1), Kary(j, 1), vbTextCompare) > 0 Then
            Dary(i, 2) = Kary(j, 2)
            Dary(i, 3) = Kary(j, 3)
            Exit For
         End If
      Next j
   Next i
   Range("F2").Resize(UBound(Dary), 3).Value = Dary
End Sub
 
Upvote 0
I have tried your method and I think I have it duplicated (or at least close) but I am running into an error. The error is Run-time Error 1004 "Method 'Range' of object '_Worksheet' failed.

Here is my code:

VBA Code:
Sub AutoAssign()
    Dim RoomName As Variant, Assignments As Variant
    Dim i As Long, j As Long
  
    RoomName = Sheet1.Range("B15", Range("B" & Rows.Count).End(xlUp).Offset(, 3)).Value
    Assignments = Sheet6.Range("B2", Range("B" & Rows.Count).End(xlUp).Offset(, 3)).Value
    For i = 1 To UBound(RoomName)
        For j = 1 To UBound(Assignments)
            If InStr(1, RoomName(i, 1), Assignments(j, 1), vbTextCompare) > 0 Then
                RoomName(i, 3) = Assignments(j, 3)
                RoomName(i, 4) = Assignments(j, 4)
                Exit For
            End If
        Next j
    Next i
    Range("B15").Resize(UBound(RoomName), 3).Value = RoomName
  
End Sub

RoomName is what I am calling my list of room names from the project (your Dary variable). Assignments is what I am calling my ASHRAE room names and code assignments.

I am not very good at reading the syntax of VBA yet. If I am reading your code correctly you are offsetting by the total width of the array minus 1 (3-1 = 2).

I see you are providing code that would be flexible for the length of the input array. I don't anticipate ever working with a building that has more than 2000 rooms so the RoomName variable will likely never be more than cells long. Would it simplify the code if we locked the length?
 
Last edited:
Upvote 0
You need to fully qualify the ranges like
Rich (BB code):
    RoomName = Sheet1.Range("B15", Sheet1.Range("B" & Rows.Count).End(xlUp).Offset(, 3)).Value
    Assignments = Sheet6.Range("B2", Sheet6.Range("B" & Rows.Count).End(xlUp).Offset(, 3)).Value
Otherwise the Range.end(xlup) is trying to work on the active sheet.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,667
Messages
6,120,818
Members
448,990
Latest member
rohitsomani

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top