Question : VBA - Array Looping datatype Long, String, and worksheet code name

Hi, I find the hardest part of coding in my line of work is finding out which column (of row 1) a particular string is positioned in. In this example, if I have to search for 3 strings across 3 different worksheets and save the column location in a unqiue VBA variable, I would normally drive myself mad and type:

Dim lngRV2MLSecColumn As Long, _
    lngRV2NotionalColumn As Long, _
    lngRV2BookColumn As Long, _
    lngBloombergMLSecColumn As Long, _
    lngBloombergNotionalColumn As Long, _
    lngBloombergBookColumn As Long
    lngODRMLSecColumn As Long, _
    lngODRNotionalColumn As Long, _
    lngODRBookColumn As Long

lngRV2MLSecColumn = shtRV2.Rows(1).Find("ML Sec No", LookIn:=xlValues, lookat:=xlWhole).Cells.Column
lngRV2NotionalColumn = shtRV2.Rows(1).Find("Bond Notional", LookIn:=xlValues, lookat:=xlWhole).Cells.Column
lngRV2BookColumn = shtRV2.Rows(1).Find("Book", LookIn:=xlValues, lookat:=xlWhole).Cells.Column

lngBloombergMLSecColumn = shtBloomberg.Rows(1).Find("ML Sec", LookIn:=xlValues, lookat:=xlWhole).Cells.Column
lngBloombergNotionalColumn = shtBloomberg.Rows(1).Find("Bond Notional", LookIn:=xlValues, lookat:=xlWhole).Cells.Column
lngBloombergBookColumn = shtBloomberg.Rows(1).Find("Book", LookIn:=xlValues, lookat:=xlWhole).Cells.Column

lngODRMLSecColumn = shtODR.Rows(1).Find("ML Sec No", LookIn:=xlValues, lookat:=xlWhole).Cells.Column
lngODRNotionalColumn = shtODR.Rows(1).Find("Bond Notional", LookIn:=xlValues, lookat:=xlWhole).Cells.Column
lngODRBookColumn = shtODR.Rows(1).Find("Book", LookIn:=xlValues, lookat:=xlWhole).Cells.Column

Before I even do any real coding, this process is starting to make me weary. I tried to implement an array loop but I have proved time and time again to be array-loop deficiently challenged.

All this typing before even attempting to do any work. Please, if anyone can assist or simply tell me "IT CANNOT BE DONE", then I would gracefully quit my ambitions and accept that fact that I must out every line of code. Thank you in advance.

Before is my failed attempt to loop through 3 worksheets.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
Dim lngRV2MLSecColumn As Long, _
    lngRV2NotionalColumn As Long, _
    lngRV2BookColumn As Long, _
    lngBloombergMLSecColumn As Long, _
    lngBloombergNotionalColumn As Long, _
    lngBloombergBookColumn As Long
    lngODRMLSecColumn As Long, _
    lngODRNotionalColumn As Long, _
    lngODRBookColumn As Long
 
Dim arrayVariable()
ReDim arrayVariable(1 To 3)
arrayVariable(1) = lngRV2MLSecColumn
arrayVariable(2) = lngRV2NotionalColumn
arrayVariable(3) = lngRV2BookColumn
 
Dim arrayFind() As String
ReDim arrayFind(1 To 3)
arrayFind(1) = "ML Sec No"
arrayFind(2) = "Bond Notional"
arrayFind(3) = "Book"
 
Dim arraySheets() As String
ReDim arraySheets(1 To 3)
arraySheets(1) = "shtRV2"
arraySheets(2) = "shtBloomberg"
arraySheets(3) = "shtODR"
 
 
Dim i As Integer
For i = 1 To 3
arrayVariable(i) = arraySheets(i).Rows(1).Find(arrayFind(i), LookIn:=xlValues, lookat:=xlWhole).Cells.Column
Next i
Open in New Window Select All

Answer : VBA - Array Looping datatype Long, String, and worksheet code name

You could use a function such as the attached, and simply write a loop for each of the variables you want to search on. The function returns an array of sheet name and column. It might have multiple found values if the value appears in more than one sheet.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
Function find_value(val As String)
 
    Dim location(1 To 100, 2) As String
 
    i = 1
 
    For Each ws In ActiveWorkbook.Worksheets()
    
        If Not (ws.Cells.Find(val, LookIn:=xlValues, lookat:=xlWhole) Is Nothing) Then
            location(i, 1) = ws.Name
            location(i, 2) = ws.Cells.Find(val, LookIn:=xlValues, lookat:=xlWhole).Column
            i = i + 1
        End If
    Next ws
 
    find_value = location
    
End Function
Open in New Window Select All
Random Solutions  
 
programming4us programming4us