Artificial Intelligence
Please fill the required field.

I want a VBA code to open workbook CTC in the following path: O:\TMO\GT-SN\GT-Daily Activities\Regions Daily Reports\SCRATCH Then clear tabs Full Positions (Sheet2), Positions in breach (Sheet3), and Positions in complaince (Sheet4). in the active workbok


To create the VBA code you requested, we'll break down your requirements step by step. The following code aims to accomplish the tasks described: 1. Open the workbook located at the specified path. 2. Clear specific sheets in the opened workbook. 3. Filter and copy data from the active workbook into specific tabs in the opened workbook. 4. Perform additional filtering in the opened workbook. Here's the complete VBA code to achieve what you need: ```vba Sub ProcessWorkbooks() Dim wbCTC As Workbook Dim wbActive As Workbook Dim wsActive As Worksheet Dim wsFullPositions As Worksheet Dim wsPositionsInBreach As Worksheet Dim wsPositionsInCompliance As Worksheet Dim wsPendings As Worksheet Dim lastRow As Long Dim today As Date Dim twoDaysAgo As Date Dim rng As Range ' Set the active workbook Set wbActive = ThisWorkbook Set wsPendings = wbActive.Sheets("Pendings") ' Assuming Pendings is Sheet4 in the active workbook ' Define the path to the CTC workbook Dim cPath As String cPath = "O:\TMO\GT-SN\GT-Daily Activities\Regions Daily Reports\SCRATCH\CTC.xlsx" ' Adjust extension if necessary ' Open the CTC workbook On Error Resume Next ' To handle any errors that occur during workbook opening Set wbCTC = Workbooks.Open(cPath) On Error GoTo 0 If wbCTC Is Nothing Then MsgBox "The CTC workbook could not be found.", vbExclamation Exit Sub End If ' Clear sheets in CTC workbook Set wsFullPositions = wbCTC.Sheets("Full Positions") Set wsPositionsInCompliance = wbCTC.Sheets("Positions in compliance") Set wsPositionsInBreach = wbCTC.Sheets("Positions in breach") wsFullPositions.Cells.Clear wsPositionsInCompliance.Cells.Clear wsPositionsInBreach.Cells.Clear ' Get today's date and calculate the date for the past 2 business days today = Date twoDaysAgo = today - 2 ' For simplicity; you might replace this logic with actual KSA business day logic ' Filter column G in Pendings sheet for "CTC" and copy to Full Positions With wsPendings .AutoFilterMode = False lastRow = .Cells(.Rows.Count, "G").End(xlUp).Row .Range("A1:G" & lastRow).AutoFilter Field:=7, Criteria1:="CTC" On Error Resume Next Set rng = .Range("A2:G" & lastRow).SpecialCells(xlCellTypeVisible) On Error GoTo 0 If Not rng Is Nothing Then rng.Copy wbCTC.Sheets("Full Positions").Range("A1") End If .AutoFilterMode = False End With ' Filter column A in active sheet (Pendings) for the past 2 business days and copy to Positions in Compliance With wsPendings .AutoFilterMode = False lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row .Range("A1:A" & lastRow).AutoFilter Field:=1, Criteria1:=">=" & twoDaysAgo On Error Resume Next Set rng = .Range("A2:A" & lastRow).SpecialCells(xlCellTypeVisible) On Error GoTo 0 If Not rng Is Nothing Then rng.Copy wbCTC.Sheets("Positions in compliance").Range("A1") End If .AutoFilterMode = False End With ' Filter Full Positions for dates not in the past 2 business days and copy to Positions in Breach With wsFullPositions .AutoFilterMode = False lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row .Range("A1:A" & lastRow).AutoFilter Field:=1, Criteria1:="<" & twoDaysAgo On Error Resume Next Set rng = .Range("A2:A" & lastRow).SpecialCells(xlCellTypeVisible) On Error GoTo 0 If Not rng Is Nothing Then rng.Copy wsPositionsInBreach.Range("A1") End If .AutoFilterMode = False End With ' Close the CTC workbook and save changes wbCTC.Close SaveChanges:=True MsgBox "Process completed successfully!", vbInformation End Sub ``` ### Notes: 1. **Workbook and Worksheet Names**: Ensure that the names of the workbooks and sheets match exactly with those in your files. If the names are different, replace them in the code above. 2. **Error Handling**: The code includes basic error handling to manage issues while opening the workbook and when copying ranges. Adjust error handling as per your needs. 3. **Date Logic**: The code assumes a simple calculation for the last 2 days (`today - 2`). You may need to implement business-day logic based on your specific needs, especially if holidays or weekends are involved. 4. **Saving Changes**: The CTC workbook is saved before closing. Make sure this behavior aligns with your expectations. 5. **Running the Code**: Place this code in a module in the VBA editor (press `ALT + F11`) in Excel, and you can run it from there or link it to a button in your workbook. By following these steps, this code will perform all of your specified tasks in the appropriate context.