英文:
Excel Interop Error Message on For Each loop
问题
以下是您的翻译代码部分:
foreach (var facility in machine)
{
firstWS.Range["A1:I" + lastRow2].AutoFilter(3, machine, XlAutoFilterOperator.xlFilterValues, Type.Missing, Type.Missing);
Excel.Range visibleCells = firstWS.UsedRange.SpecialCells(Excel.XlCellType.xlCellTypeVisible, Type.Missing);
firstWS.AutoFilter.Range.Copy();
Excel.Worksheet newWS = xlApp2.Worksheets.Add(After: xlWorkbook2.Sheets[xlWorkbook2.Sheets.Count]);
newWS.AutoFilterMode = false;
newWS.Paste();
}
请注意,这是您在C#中的代码段的翻译。如果您需要任何其他帮助,请随时提出。
英文:
I'm trying to recreate an excel macro I had in c#, but am running into an error that I can't solve. Basically the program takes the raw data from the first worksheet, it is supposed to filter it based on column AA and then copy that range and paste it into it's own new worksheet. I keep getting this error: System.Runtime.InteropServices.COMException: 'The remote procedure call failed. (Exception from HRESULT: 0x800706BE)' on the line right after the for each statement, any ideas?
foreach (var facility in machine)
{
firstWS.Range["A1:I" + lastRow2].AutoFilter(3, machine, XlAutoFilterOperator.xlFilterValues, Type.Missing, Type.Missing);
Excel.Range visibleCells = firstWS.UsedRange.SpecialCells(Excel.XlCellType.xlCellTypeVisible, Type.Missing);
firstWS.AutoFilter.Range.Copy();
Excel.Worksheet newWS = xlApp2.Worksheets.Add(After: xlWorkbook2.Sheets[xlWorkbook2.Sheets.Count]);
newWS.AutoFilterMode = false;
newWS.Paste();
}
Below is the entire code if that helps, sorry it is a little messy, I'm very new to C#:
private void button3_Click(object sender, EventArgs e)
{
Excel.Application xlApp2;
Excel.Workbook xlWorkbook2;
Excel.Worksheet ws2;
Excel.Worksheet newSheetName;
Excel.Range rng3;
Excel.Range rng4;
long lastRow2;
long fullRow;
xlApp2 = new Excel.Application();
//open workbook
xlWorkbook2 = xlApp2.Workbooks.Open(fileExcel, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
//system hides the workbook from the user while the code runs
xlApp2.Visible = true;
//declare the first worksheet "Report" as firstWS
Worksheet firstWS = xlWorkbook2.Worksheets["Report"];
//Sort the worksheet by column C **work in progress**
fullRow = firstWS.Rows.Count;
lastRow2 = firstWS.Cells[fullRow, "I"].End(Excel.XlDirection.xlUp).Row;
rng3 = firstWS.Range["A1:I" + lastRow2];
rng3.Sort(rng3.Columns[3], XlSortOrder.xlAscending);
//firstWS.Columns["C"].Sort("C", XlSortOrder.xlAscending);
//Delete columns K:N, F:I and then update the column names of F1:I1
firstWS.Range["K:N"].Delete(XlDeleteShiftDirection.xlShiftToLeft);
firstWS.Range["F:I"].Delete(XlDeleteShiftDirection.xlShiftToLeft);
firstWS.Range["F1"].Value = "Current Qty";
firstWS.Range["G1"].Value = "NDC";
firstWS.Range["H1"].Value = "Pkg Sz of Comm. Containers";
firstWS.Range["I1"].Value = "Number of Full Comm. Containers";
//Sets the filter for Column C to use to name the different worksheets
firstWS.Columns[3].AdvancedFilter(XlFilterAction.xlFilterCopy, Type.Missing, CopyToRange: firstWS.Range["AA1"],Unique: true);
firstWS.Range["A1:I" + lastRow2].Copy();
Worksheet newerWSCopy = xlWorkbook2.Worksheets.Add();
newerWSCopy.Name = "Reporting";
rng4 = newerWSCopy.Range["A1"];
rng4.PasteSpecial(XlPasteType.xlPasteValues);
//machine = firstWS.Range["AA2", firstWS.Cells[fullRow, "AA"].End(Excel.XlDirection.xlUp)];
Excel.Range machine = firstWS.Range["AA2", firstWS.Cells[firstWS.Rows.Count, "AA"].End(Excel.XlDirection.xlUp)];
foreach (var facility in machine)
{
firstWS.Range["A1:I" + lastRow2].AutoFilter(3, machine, XlAutoFilterOperator.xlFilterValues, Type.Missing, Type.Missing);
Excel.Range visibleCells = firstWS.UsedRange.SpecialCells(Excel.XlCellType.xlCellTypeVisible, Type.Missing);
firstWS.AutoFilter.Range.Copy();
Excel.Worksheet newWS = xlApp2.Worksheets.Add(After: xlWorkbook2.Sheets[xlWorkbook2.Sheets.Count]);
newWS.AutoFilterMode = false;
newWS.Paste();
}```
I've tried reworking this line but keep getting this same error.
</details>
# 答案1
**得分**: 1
我能够弄清楚,我需要更改foreach变量和autofilter条件。
<details>
<summary>英文:</summary>
I was able to figure it out, I needed to change the foreach variable and the autofilter criteria.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论