?
Solved

VBA Excel to select and move rows to new worksheet

Posted on 2014-04-28
4
Medium Priority
?
1,756 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 52

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 52

Accepted Solution

by:
Rgonzo1971 earned 2000 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 33

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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

765 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