Can you please amend the VBA code below to do the following: In the same folder…
- User
Can you please amend the VBA code below to do the following: In the same folder location, open in the background the woorkbook 'Service Providers History'. Search all sheets that have three letters in the woorkbook 'Service Providers History' and write the results to the sheet HTasks in the workbook 'Service Providers' then close the workbook 'Service Providers History' without any notifications.
Sub FindAndCopyHRows() Dim selectedValue As String Dim ws As Worksheet Dim hTasksWs As Worksheet Dim lastRow As Long, pasteRow As Long ' Get the selected value from cell A1 selectedValue = Range("A1").Value ' Set the "CTasks" worksheet as the destination for copied rows Set cTasksWs = ThisWorkbook.Worksheets("HTasks") ' Clear the previous data in CTasks worksheet cTasksWs.Range("B2:J" & hTasksWs.Cells(Rows.Count, "B").End(xlUp).Row).Clear cTasksWs.Range("K2:K" & hTasksWs.Cells(Rows.Count, "K").End(xlUp).Row).Clear ' Loop through all sheets in the workbook For Each ws In ThisWorkbook.Worksheets ' Check if the sheet name has only three letters If Len(ws.Name) = 3 Then ' Find the matching value in column B lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row For i = 2 To lastRow If ws.Cells(i, "B").Value = selectedValue Then ' Copy the row B:J to CTasks sheet pasteRow = hTasksWs.Cells(hTasksWs.Rows.Count, "B").End(xlUp).Row + 1 ws.Range("B" & i & ":J" & i).Copy Destination:=hTasksWs.Range("B" & pasteRow & ":J" & pasteRow) ' Write the sheet name to column K in CTasks sheet hTasksWs.Cells(pasteRow, "K").Value = ws.Name End If Next i End If Next ws hTasksWs.Range("L2:L101").Calculate hTasksWs.Range("N1:X1").Copy Range("B1:L1") End Sub
- Assistant
Sub FindAndCopyHRows() Dim selectedValue As String Dim ws As Worksheet Dim hTasksWs As Worksheet Dim lastRow As Long, pasteRow As Long ' Get the selected value from cell A1 selectedValue = ThisWorkbook.Worksheets("HTasks").Range("A1").Value ' Set the "HTasks" worksheet as the destination for copied rows Set hTasksWs = ThisWorkbook.Worksheets("HTasks") ' Clear the previous data in HTasks worksheet hTasksWs.Range("B2:J" & hTasksWs.Cells(hTasksWs.Rows.Count, "B").End(xlUp).Row).Clear hTasksWs.Range("K2:K" & hTasksWs.Cells(hTasksWs.Rows.Count, "K").End(xlUp).Row).Clear ' Open the "Service Providers History" workbook in the background Application.ScreenUpdating = False Application.DisplayAlerts = False Workbooks.Open Filename:="Service Providers History.xlsx", UpdateLinks:=False ' Loop through all sheets in the "Service Providers History" workbook For Each ws In Workbooks("Service Providers History.xlsx").Worksheets ' Check if the sheet name has only three letters If Len(ws.Name) = 3 Then ' Find the matching value in column B lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row For i = 2 To lastRow If ws.Cells(i, "B").Value = selectedValue Then ' Copy the row B:J to HTasks sheet pasteRow = hTasksWs.Cells(hTasksWs.Rows.Count, "B").End(xlUp).Row + 1 ws.Range("B" & i & ":J" & i).Copy Destination:=hTasksWs.Range("B" & pasteRow & ":J" & pasteRow) ' Write the sheet name to column K in HTasks sheet hTasksWs.Cells(pasteRow, "K").Value = ws.Name End If Next i End If Next ws ' Calculate column L in HTasks sheet hTasksWs.Range("L2:L" & hTasksWs.Cells(hTasksWs.Rows.Count, "B").End(xlUp).Row).Calculate ' Copy column N to X from HTasks sheet to Service Providers sheet hTasksWs.Range("N1:X1").Copy Destination:=ThisWorkbook.Worksheets("Service Providers").Range("B1:L1") ' Close the "Service Providers History" workbook without any notifications Workbooks("Service Providers History.xlsx").Close SaveChanges:=False Application.ScreenUpdating = True Application.DisplayAlerts = True End Sub
- User
I am getting a subscript out of range error
- Assistant
The error is occurring because the workbook "Service Providers History.xlsx" is not found or not open. Make sure that the workbook is in the same folder as the workbook with the VBA code and that it is open.
If the workbook is open and still getting the error, try using the full file path instead of just the file name. Replace the line:
VBAWorkbooks.Open Filename:="Service Providers History.xlsx", UpdateLinks:=Falsewith:
VBAWorkbooks.Open Filename:=ThisWorkbook.Path & "\Service Providers History.xlsx", UpdateLinks:=FalseThis will specify the full file path and should prevent the error.
If you're still having trouble, please provide more information about the folder structure and the names of the workbooks, and I can assist you further.