Hi, Everyone,
I have to retrieve data from an Excel file that contains a large number of sheets. I want to search two specific columns in each sheet for values that fall into a certain range (rows that contain values in those columns that fall into these ranges will be designated as "valid" rows) and then place each "valid" row from these sheets into another file of any format, hopefully labeled with the name of the sheet. I'd also like to know how many such rows exist. Is there a quick way to do this using some kind of program/script? Sorry; I have no idea how to use Excel and I don't really know how to even begin doing this.
Alternatively, I can use Perl/C++/Java/etc. to do this but I'd need a quick way of combining the sheets into one file so that I can put the data into a text file (or a set of text files).
Thanks a lot for reading and for your help!
Last edited by harpseal; 06-16-2011 at 06:00 PM.
harpseal,
You can accomplish what you're requesting with a macro. To add a macro to a workbook:
- Save a copy of the Excel workbook you want to modify
- Always test macros in a copy so that the original is preserved in case the modifications don't go smoothly
- Open the copy of the Excel workbook you want to modify
- Use the keyboard shortcut ALT+F11 to open the Visual Basic Editor
- Insert -> Module
- Copy/Paste the code into that area
To run a macro in a workbook:
- In Excel (not the Visual Basic Editor) press the keyboard shortcut ALT+F8
- Double-click the desired macro (I named this one ExportDataMacro_for_harpseal)
Here's the macro code. It will save "valid" rows of a worksheet to your desktop as a .txt file:
Sub ExportDataMacro_for_harpseal() Const Col1 As String = "A" 'The first column to check Const Col2 As String = "B" 'The second column to check Const LowVal As Double = 1 'The low value you're looking for Const HiVal As Double = 10 'The high value you're looking for Dim SavePath As String: SavePath = Environ("UserProfile") & "\Desktop\" Dim FileName As String Dim wsDest As Worksheet: Set wsDest = Sheets.Add(after:=Sheets(Sheets.Count)) wsDest.Name = "Data dump for valid data" Dim ws As Worksheet, RowIndex As Long, rngValid As Range For Each ws In ActiveWorkbook.Sheets If ws.Name <> wsDest.Name Then wsDest.UsedRange.ClearContents ws.UsedRange.AutoFilter ws.UsedRange.AutoFilter Field:=ws.Cells(1, Col1).Column, _ Criteria1:=">=" & LowVal, Operator:=xlAnd, Criteria2:="<=" & HiVal ws.UsedRange.AutoFilter Field:=ws.Cells(1, Col2).Column, _ Criteria1:=">=" & LowVal, Operator:=xlAnd, Criteria2:="<=" & HiVal Set rngValid = ws.UsedRange.SpecialCells(xlCellTypeVisible) rngValid.Copy wsDest.[A1].PasteSpecial xlPasteAll ActiveSheet.SaveAs FileName:=SavePath & ws.Name & ".txt", _ FileFormat:=xlUnicodeText ws.UsedRange.AutoFilter End If Next ws Application.CutCopyMode = False Application.DisplayAlerts = False wsDest.Delete Application.DisplayAlerts = True End Sub
Hope that helps,
~tigeravatar
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks