Solved

VBA Excel to select and move rows to new worksheet

Posted on 2014-04-28
4
1,702 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 49

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 49

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 32

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Companies keep a much closer eye on costs today, so changing to new Technology – Microsoft Office 365 is the smartest move to take.
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

910 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

22 Experts available now in Live!

Get 1:1 Help Now