Question

On a report I'm working on, after the SQL dumps the data into a new excel sheet I have to check for duplicate records by the Transaction Number column, column AE(31)

Here is part of the code that counts the rows used, defines the range, removes the duplicates and after re-counts and defines a new range for the next function.

//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;

Here is where things get odd.

The MyCount variable fails to find the last row used if the RemoveDuplicate function did not find any duplicates.

Case an point: Report 10701 has a total of 1415 rows (82 of which are duplicates) the RemoveDuplicate kicks in an removes them leaving 1333. For this report, trnsCount when watched correctly calculates 1415 rows and MyCount correctly calculates 1333 rows after the duplicates have been removed.

Report 14506 has a total of 12 rows (0 duplicates) so the RemoveDuplicate removes no rows from this report. For this report, trnsCount when watched correctly calculates 12 rows but MyCount only calculates 1 row.

Any ideas?

Était-ce utile?

La solution

Pay attention on your variable trnsCount:

// trnsCount = Rows.Count
int trnsCount = JobDataSheet.Rows.Count;
// trnsCount = 12
trnsCount = JobDataSheet.Cells[trnsCount, 1].End(Excel.XlDirection.xlUp).Row;

Then, after removing duplicates, you're using trnsCount (which is equal to 12) to get last row:

MyCount = JobDataSheet.Cells[trnsCount, 1].End(Excel.XlDirection.xlUp).Row;

and obviously this is incorrect, since you should use Rows.Count instead 12.

So, just change above line to:

MyCount = JobDataSheet.Cells[MyCount , 1].End(Excel.XlDirection.xlUp).Row;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top