Dim excelPath As String = "C:\Users\\Documents\UiPath\ForumSolutions\test.xlsx" ' Path to the Excel file
Dim sheetName As String = "Sheet1" ' Sheet name
Dim highlightColor As Long = 65535 ' Example: Yellow (adjust this to your desired highlight color)
Dim outputPath As String = "C:\Users\\Documents\UiPath\ForumSolutions\tes.xlsx" ' Output file path
' Create Excel application and workbook objects
Dim excelApp As New Microsoft.Office.Interop.Excel.Application
Dim workbook As Microsoft.Office.Interop.Excel.Workbook = CType(excelApp.Workbooks.Open(excelPath), Microsoft.Office.Interop.Excel.Workbook)
Dim worksheet As Microsoft.Office.Interop.Excel.Worksheet = CType(workbook.Sheets(sheetName), Microsoft.Office.Interop.Excel.Worksheet)
' Get the last used row
Dim lastRow As Integer = CType(worksheet.Cells(worksheet.Rows.Count, 1), Microsoft.Office.Interop.Excel.Range).End(Microsoft.Office.Interop.Excel.XlDirection.xlUp).Row
' Create a new workbook for the filtered data
Dim newWorkbook As Microsoft.Office.Interop.Excel.Workbook = CType(excelApp.Workbooks.Add(), Microsoft.Office.Interop.Excel.Workbook)
Dim newWorksheet As Microsoft.Office.Interop.Excel.Worksheet = CType(newWorkbook.Sheets(1), Microsoft.Office.Interop.Excel.Worksheet)
Dim newRow As Integer = 1
' Loop through rows in the sheet
' Loop through rows in the sheet
For i As Integer = 1 To lastRow
' Get the cell in the first column
Dim cell As Microsoft.Office.Interop.Excel.Range = CType(worksheet.Cells(i, 1), Microsoft.Office.Interop.Excel.Range)
' Explicitly cast the Interior.Color property to Long
Dim cellColor As Long = CType(cell.Interior.Color, Long)
' Check if the cell color matches the highlight color
If cellColor = highlightColor Then
' Copy the entire row to the new worksheet
CType(worksheet.Rows(i), Microsoft.Office.Interop.Excel.Range).Copy(CType(newWorksheet.Rows(newRow), Microsoft.Office.Interop.Excel.Range))
newRow += 1
End If
Next
' Save the filtered data to a new Excel file
newWorkbook.SaveAs(outputPath)
newWorkbook.Close()
workbook.Close(False)
excelApp.Quit()
' Release resources
System.Runtime.InteropServices.Marshal.ReleaseComObject(newWorksheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(newWorkbook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)