There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
bios black screen blue screen boot computer connection crash css dell display driver drivers email error excel firefox firefox 3 freeze game hard drive internet internet explorer itunes laptop lcd linux malware monitor network networking nvidia outlook outlook 2003 outlook express partition password printer problem router slow sound startup trojan usb video virus vista windows windows xp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
extracting certain rows only VBA


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
cjsmith22's Avatar
Member with 49 posts.
 
Join Date: Sep 2003
06-Nov-2004, 07:48 AM #1
extracting certain rows only VBA
hi, i have an excel worksheet of hospital patients(columns-patient,date referred,date received,date registered,date admitted,ward,diagnosis).Not all of the patients in the sheet have been admitted(ie have nothing in their date admitted column) How do i extract only the rows where they have been admitted(eg. 1:5,7,9,10:13 etc) into a new sheet? the problem is this must work for any similar set of data (ie. the number of patients can vary).
any help would be greatly appreciated.
coderitr's Avatar
Distinguished Member with 3,080 posts.
 
Join Date: Oct 2003
06-Nov-2004, 08:35 AM #2
Try testing if date admitted is not null
Kelly_in_LA's Avatar
Member with 47 posts.
 
Join Date: Jun 2004
Location: Los Angeles, California
Experience: Intermediate
06-Nov-2004, 02:36 PM #3
See if this will work for you...

NOTE: I have assumed (based on your description) that "Date Admitted" is found in column E, which is also column 5 for the sake of my macro.

When you run this macro, make sure that the sheet with the data is your "Active Sheet." Otherwise, the macro will operate on whichever sheet Excel thinks is the active one. In that case, the rows pasted to the new sheet will not be the rows you were expecting.

Code:
Sub GetOnlyThoseAdmitted()

'IF THE "DATE ADMITTED" COLUMN IS NOT THE FIFTH COLUMN,
'THEN PLEASE CHANGE THE FOLLOWING LINE:
Const DateAdmittedColumn = 5

Dim myRow As Range
Dim mySourceSheet As Worksheet
Dim myNewSheet As Worksheet
Dim TimeString As String
Dim r As Long

Set mySourceSheet = ActiveSheet
Set myNewSheet = ActiveWorkbook.Sheets.Add

'The following loop gives the new sheet a name.
'It also makes sure that the name we are attempting
'to give the sheet does not already exist.
Do
    On Error Resume Next
    TimeString = Replace(CStr(VBA.Time), ":", ".")
    myNewSheet.Name = "New Extracted Data " & TimeString
Loop While Err.Number = 1004


For Each myRow In mySourceSheet.UsedRange.Rows

    If Trim(CStr(myRow.Cells(1, DateAdmittedColumn).Value)) <> "" Then
    
        myRow.Copy
        r = r + 1
        myNewSheet.Cells(r, 1).Select
        myNewSheet.Paste
    
    End If

Next

Application.CutCopyMode = False
End Sub
cjsmith22's Avatar
Member with 49 posts.
 
Join Date: Sep 2003
07-Nov-2004, 10:10 AM #4
All sorted - much appreciated
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:28 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.