How do you reference cells in another workbook using an excel formula for Color Formatting?

0

I have two worksheets from different workbooks: one I am editing (xlwkstTempSummary), and one from which I want to compare values (xlwkstSummary). I want to apply a background color to a single column in the temporary sheet based on the values from the imported sheet. Here is a snippet of my current code:

Excel.Workbook xlwkbkImport = books.Open(file_location,misValue, true, 
    misValue, misValue, misValue, true, misValue, misValue, misValue, false, 
    misValue, misValue, misValue, 0);

Excel.Sheets sheets = xlwkbkImport.Worksheets;
Excel.Worksheet xlwkstSummary = (Excel.Worksheet)sheets.get_Item(1);

Excel.Range r = xlwkstTempSummary.get_Range("F" + summary_start_row.ToString(), "F" + (summary_start_row + clinic_row_count - 1).ToString());
string file_name = file_location.Name;
string file_directory = Path.GetDirectoryName(file_location);
Excel.FormatConditions r_format = r.FormatConditions;

Excel.FormatCondition c1 = r_format.Add((Excel.XlFormatConditionType)2) /*format based on expression*/,
                misValue,
                @"=AND(AND($G" + summary_start_row + @"<>""4 Star"", $G" + 
                summary_start_row + @"<>""5 Star"", $G" +
                summary_start_row + @"<>""75th"", $G" + 
                summary_start_row + @"<>""90th""), $F" +
                summary_start_row + @"<VLOOKUP($C" + summary_start_row + 
                @",'" + file_directory + @"[" + file_name + @"]Summary'!$A$9:$D$41,4,FALSE))", 
                misValue, misValue, misValue, misValue, misValue); 
Excel.Interior c1_interior = c1.Interior;
c1_interior.Color = Color.FromArgb(216, 49, 49); //Red

I am currently using a vlookup to get the value I need from the imported sheet, and the expressions work fine when written in directly in excel, but when I run the program, I get the following error:

System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG)) at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)

The only reason I can think of for it failing is failing to properly resolve the name of the imported workbook in my vlookup call. Could someone more familiar with the Microsoft.Office.Interop.Excel library shed some light on this issue?

c#
excel
excel-formula
excel-interop
cell-formatting
asked on Stack Overflow Mar 23, 2018 by kuhlde1337 • edited Mar 26, 2018 by kuhlde1337

1 Answer

0

Ok, so I was not able to figure out why my reference to the imported workbook was not working. I did discover that the Path.GetPathDirectory(1) function returns the File path without the "\" at the end, but adding it did not change the error.

I have come to learn that my original method was flawed regardless since the VLookup would only return values if the file was already open in excel, and would ask for the file's location otherwise. This is obviously not what I was looking for as this is meant to be an automated process.

The solution to my problem was to create a new sheet with a "Very Hidden" visibility:

Excel.Worksheet xlwkstTempDataStore = TempSheets.Add(misValue, xlwkstTempCareGaps, misValue, misValue);
xlwkstTempDataStore.Name = "data";
xlwkstTempDataStore.Visible = (Excel.XlSheetVisibility)2; //Very Hidden

After copying the data from the imported workbook to my new sheet, I just needed to reference the very hidden sheet in my VLookup:

Excel.FormatCondition c1 = r_format.Add((Excel.XlFormatConditionType)2 /*format based on expression*/,
                                misValue,
                                @"=AND(AND($G" + summary_start_row + @"<>""4 Star"", $G" + summary_start_row + @"<>""5 Star"", $G" +
                                    summary_start_row + @"<>""75th"", $G" + summary_start_row + @"<>""90th""), $F" +
                                    summary_start_row + @"<VLOOKUP($C" + summary_start_row + @",data!$A$9:$D$" + last_data_store_row + @",4,FALSE))", 
                                misValue, misValue, misValue, misValue, misValue); 
                Excel.Interior c1_interior = c1.Interior;
                c1_interior.Color = Color.FromArgb(216, 49, 49); //Red
answered on Stack Overflow Mar 26, 2018 by kuhlde1337

User contributions licensed under CC BY-SA 3.0