简体   繁体   中英

Excel Macro: cycle through rows, while cycling through sheets

I have a workbook with a few hundred worksheets, each laid out in the same way (ie possessing the same columns) though with varying numbers of rows of data.

I would like to cycle through each row in each sheet, and if the value in a certain column (column "L") is a specific value (in this case a string), I would like to copy that row to a new worksheet. So that at the end I have one large worksheet with every row existing in the larger workbook that has a certain specified value in column "L".

I don't want to also cycle through the sheet I'm copying my results to; perhaps I need to do this from a different workbook?

Thanks in advance.

Response:

So the file and code is at my office, but: if this were in 1 worksheet, I know several ways to go through the rows and return some or all of the corresponding fields. The trouble I have is (a) setting it up so that it cycles through different worksheets of varying row-counts, (b) returning it to a new worksheet without writing over what it's already written. If someone has a solution using ADO, that would be welcome as well because it is a very large worksheet as a whole. Thanks again, and my apologies if I ask large and unspecified questions.

Since your question is rather text based asking for opinions without codes, I am giving you my opinion and finding via this answer - fair enough :)

As per my comments,

So if you would like to do the searching on data book from another book, that would be a better idea given this BUG: Memory leak occurs when you query an open Excel worksheet by using ActiveX Data Objects (ADO)

In that way, you treat your data book as a database itself and query from Excel sheet. Even doing so, you may wan to make sure each of your searchable sheets that you are considering as dataset has a similar structure. If you had provided us with a sample data, then it's much better than we keep throwing stones at the target in dark... ;) You mentioned about searching rows. Are you searching rows of one column per sheet? If so do all your searchable sheets contain this column?

So for the rows that you want to search/scan, here is what Microsoft has to say, very important to notice:

"Rows to Scan: Excel does not provide ADO with detailed schema information about the data it contains, as a relational database would . Therefore, the driver must scan through at least a few rows of the existing data in order to make an educated guess at the data type of each column. The default for "Rows to Scan" is eight (8) rows. You can specify an integer value from one (1) to sixteen (16) rows, or you can specify zero (0) to scan all existing rows. This is done by adding the optional MaxScanRows= setting to the connection string, or by changing the Rows to Scan setting in the DSN configuration dialog box.

However, due to a bug in the ODBC driver , specifying the Rows to Scan (MaxScanRows) setting currently has no effect. In other words, the Excel ODBC driver (MDAC 2.1 and later) always scans the first 8 rows in the specified data source in order to determine each column's datatype. "

Further, if your searchable rows has mix data types--> that puts you in great danger. Microsoft also talks about that aspect too.

So there are many precautions you may take with the above process, but it is faster than a VBA Binary Search and a .Find . However if you do not want to take up ADO approach, then you can provide us with some designed you may have come up with. Happy to pitch in going forward on that as well. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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