Solved

VBA Excel to select and move rows to new worksheet

Posted on 2014-04-28
4
1,688 Views
Last Modified: 2014-05-01
Hi Experts,

I am trying to select rows where country_code = US and dl_state is blank then move them to a new worksheet (cut and paste).  

Attached is dummy data.  I have the below code that works to delete the rows when dl_state is populate with something but nothing works for blank(its blank no null).  

Dim startrow As Long

'MODIFY YOUR START ROW HERE
startrow = 1

Cells(startrow, 1).Select

Do While ActiveCell.Value <> ""
    If Cells(startrow, 23).Value = "US" And Cells(startrow, 25).Value = " " Then
        rows(startrow).Delete (xlUp)
        'Don't Modify Start Row here because the rows just moved up
    Else
        startrow = startrow + 1
    End If
Loop


I am open to any suggestions to get this to work.  Thanks.
DataTest.xls
0
Comment
Question by:rhadash
  • 2
4 Comments
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 40028818
Hi,

with

Cells(startrow, 25).Value = " " 

Open in new window

you are not testing if it is blank but whether there is a space

pls try

Cells(startrow, 25).Value = ""

Open in new window

Regards
0
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40028861
Hi,

you could use filtering to select therows you want to cut

Sub Macro2()

    Set myRange = Range(Range("A1"), Range("Z" & Cells.Rows.Count).End(xlUp))
    myRange.Resize(1).AutoFilter
    myRange.AutoFilter Field:=23, Criteria1:="US"
    myRange.AutoFilter Field:=25, Criteria1:="="
    myRange.Offset(1).Resize(myRange.Rows.Count - 1).SpecialCells(xlVisible).Cut
    Sheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Paste
    Application.CutCopyMode = False
    myRange.AutoFilter
End Sub

Open in new window

Regards
0
 
LVL 31

Expert Comment

by:Rob Henson
ID: 40029233
Instead of Auto Filter, you could use the Advanced Filter function using the Copy to another location option.

Couple of quirks with Advanced Filter
1) If the destination for the filtered data is another sheet, before running the Advanced Filter Wizard, select a blank area on the destination sheet.
2) Copying to another location will overwrite formulas with values, this also applies to Auto Filter.
3) Copying to another location will overwrite results of previous filter results if present but only to the extent required for the new filter. If the new filter gives a smaller result set than previous filter, there may still be results from previous filter at bottome of list.

If you want more advice on Advanced FIlter, let us know. Running an Advanced Filter can be automated into VBA if so required.

Thanks
Rob H
0
 

Author Closing Comment

by:rhadash
ID: 40034517
Thank you!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Photo Albums in PowerPoint Photo Albums are a very useful tool in PowerPoint and allow you quickly add a large number of images. The images can be formatted in a variety of ways so that you are able to create a professional looking presentation v…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now