[英]Rows.Count fails to count rows if RemoveDuplicates doesn't find duplicates
在我正在處理的報告上,SQL將數據轉儲到新的Excel工作表中后,我必須通過“事務號”列的AE(31)列檢查重復的記錄
這是部分代碼,用於對使用的行進行計數,定義范圍,刪除重復項並在重新計數后為下一個函數定義新范圍。
//Removed duplicate transactions
int trnsCount = JobDataSheet.Rows.Count;
trnsCount = JobDataSheet.Cells[trnsCount, 1].End(Excel.XlDirection.xlUp).Row;
Excel.Range transRng = JobDataSheet.get_Range("A1:AE" + trnsCount);
transRng.RemoveDuplicates(31);
//Check if report has been run for 1 or multiple Jobs
int MyCount = JobDataSheet.Rows.Count;
MyCount = JobDataSheet.Cells[trnsCount, 1].End(Excel.XlDirection.xlUp).Row;
這是事情變得奇怪的地方。
該mycount的變量無法找到,如果 RemoveDuplicate功能沒有發現任何重復使用的最后一排。
舉例說明:報告10701總共有1415行(其中82行是重復項),RemoveDuplicate插入后將其刪除,從而留下1333行。對於此報告,當正確觀看trnsCount時,將計算出1415行,而MyCount正確地計算出1333行。已被刪除。
報表14506總共有12行(0個重復),因此RemoveDuplicate不會從此報表中刪除任何行。 對於此報告,當正確觀看時,trnsCount將計算12行,而MyCount僅計算1行。
有任何想法嗎?
注意您的變量trnsCount
:
// trnsCount = Rows.Count
int trnsCount = JobDataSheet.Rows.Count;
// trnsCount = 12
trnsCount = JobDataSheet.Cells[trnsCount, 1].End(Excel.XlDirection.xlUp).Row;
然后,在刪除重復項之后,您將使用trnsCount
(等於12
)獲取最后一行:
MyCount = JobDataSheet.Cells[trnsCount, 1].End(Excel.XlDirection.xlUp).Row;
顯然這是不正確的,因為您應該使用Rows.Count
而不是12
。
因此,只需將上面的行更改為:
MyCount = JobDataSheet.Cells[MyCount , 1].End(Excel.XlDirection.xlUp).Row;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.