简体   繁体   English

您如何使用Excel公式对颜色格式引用另一个工作簿中的单元格?

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

I have two worksheets from different workbooks: one I am editing (xlwkstTempSummary), and one from which I want to compare values (xlwkstSummary). 我有来自不同工作簿的两个工作表:一个正在编辑(xlwkstTempSummary),另一个要比较值(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: 我目前正在使用vlookup从导入的工作表中获取所需的值,并且直接在excel中编写表达式时可以正常工作,但是在运行程序时出现以下错误:

System.ArgumentException: The parameter is incorrect. System.ArgumentException:参数不正确。 (Exception from HRESULT: 0x80070057 (E_INVALIDARG)) at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData) (来自HRESULT的异常:0x80070057(E_INVALIDARG))位于System.RuntimeType.ForwardCallToInvokeMember(字符串memberName,BindingFlags标志,对象目标,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. 我能想到的唯一失败的原因是无法在我的vlookup调用中正确解析导入的工作簿的名称。 Could someone more familiar with the Microsoft.Office.Interop.Excel library shed some light on this issue? 可以让更熟悉Microsoft.Office.Interop.Excel库的人对此问题有所了解吗?

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. 我确实发现Path.GetPathDirectory(1)函数返回的文件路径末尾没有“ \\”,但是添加它并没有改变错误。

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. 我已经知道我的原始方法存在缺陷,因为VLookup仅在文件已经在excel中打开的情况下才返回值,否则会询问文件的位置。 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: 将数据从导入的工作簿复制到新工作表后,我只需要在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM