Solved

VBA Excel to select and move rows to new worksheet

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

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 51

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 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

The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

688 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