Can I have a VBA code that will search all sheets in a workbook that have only three…
- User
Can I have a VBA code that will search all sheets in a workbook that have only three letter names. For each sheet found, it will search column I from row 5 downward and if a cell in Row I is empty and the adjoining cell in Row J is not empty it will list the value in Row J concatenated with the value of B1 from that sheet. Once it finds Row I and Row J to be both empty, it will move to the next Sheet that has a three letter name and do exactly the same thing. After it has searched all sheets with a three letter name, it will list in rows the results found
- Assistant
Sure, here's the code:
Sub SearchSheets() Dim ws As Worksheet Dim i As Long Dim resultRow As Long resultRow = 1 'initialize result row For Each ws In ActiveWorkbook.Worksheets 'loop through all sheets in workbook If Len(ws.Name) = 3 Then 'check if sheet name has only three letters For i = 5 To ws.Cells(ws.Rows.Count, "I").End(xlUp).Row 'loop through column I from row 5 downwards If ws.Cells(i, "I").Value = "" And ws.Cells(i, "J").Value <> "" Then 'check if cell in Row I is empty and adjoining cell in Row J is not empty Cells(resultRow, 1).Value = ws.Cells(i, "J").Value & ws.Range("B1").Value 'concatenate values and add to result row resultRow = resultRow + 1 'increment result row ElseIf ws.Cells(i, "I").Value = "" And ws.Cells(i, "J").Value = "" Then 'check if both cells in Row I and Row J are empty Exit For 'move to next sheet if both cells are empty End If Next i End If Next ws End SubTo use this code, open your workbook and press Alt+F11 to open the VBA Editor. Insert a new module and paste the code above into it. Then, go back to your Excel worksheet and press Alt+F8 to open the Macro dialog box. Double-click on "SearchSheets" to run the macro. It will search all sheets with three letter names and list the results found in rows.
- User
How can I make the list found be output starting from column B in the sheet Start Page
- Assistant
To output the list found starting from column B in the sheet named "Start Page", you can modify the code as follows:
Sub SearchSheets() Dim ws As Worksheet Dim i As Long Dim resultRow As Long resultRow = 1 'initialize result row For Each ws In ActiveWorkbook.Worksheets 'loop through all sheets in workbook If Len(ws.Name) = 3 Then 'check if sheet name has only three letters For i = 5 To ws.Cells(ws.Rows.Count, "I").End(xlUp).Row 'loop through column I from row 5 downwards If ws.Cells(i, "I").Value = "" And ws.Cells(i, "J").Value <> "" Then 'check if cell in Row I is empty and adjoining cell in Row J is not empty Worksheets("Start Page").Cells(resultRow, 2).Value = ws.Cells(i, "J").Value & ws.Range("B1").Value 'concatenate values and add to result row, starting from column B in sheet "Start Page" resultRow = resultRow + 1 'increment result row ElseIf ws.Cells(i, "I").Value = "" And ws.Cells(i, "J").Value = "" Then 'check if both cells in Row I and Row J are empty Exit For 'move to next sheet if both cells are empty End If Next i End If Next ws End Sub - User
from the search can we omit one sheet that are called GRN
- Assistant
Yes, you can modify the code to exclude the sheet named "GRN" from the search by adding an extra condition before looping through the sheet. Here's the modified code:
Sub SearchSheets() Dim ws As Worksheet Dim i As Long Dim resultRow As Long resultRow = 1 'initialize result row For Each ws In ActiveWorkbook.Worksheets 'loop through all sheets in workbook If Len(ws.Name) = 3 And ws.Name <> "GRN" Then 'check if sheet name has only three letters and is not named "GRN" For i = 5 To ws.Cells(ws.Rows.Count, "I").End(xlUp).Row 'loop through column I from row 5 downwards If ws.Cells(i, "I").Value = "" And ws.Cells(i, "J").Value <> "" Then 'check if cell in Row I is empty and adjoining cell in Row J is not empty Worksheets("Start Page").Cells(resultRow, 2).Value = ws.Cells(i, "J").Value & ws.Range("B1").Value 'concatenate values and add to result row, starting from column B in sheet "Start Page" resultRow = resultRow + 1 'increment result row ElseIf ws.Cells(i, "I").Value = "" And ws.Cells(i, "J").Value = "" Then 'check if both cells in Row I and Row J are empty Exit For 'move to next sheet if both cells are empty End If Next i End If Next ws End Sub