Solved

VBA Excel to select and move rows to new worksheet

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

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 50

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

Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

Question has a verified solution.

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

As freelancing is becoming more and more common in the tech industry, certain obstacles are proving to be a challenge to those who are used to more traditional, structured employment. This article is meant to help identify such obstacles and offer a…
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 Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

726 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