Copy column from Sheet to another where value <>0

always_confused

Board Regular
Joined
Feb 19, 2021
Messages
68
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have two sheets: one with raw data, and one where I'd like to do some analysis. In Sheets("raw_data").Range("W:W") I have measured values. I would like to copy only the non 0 values to Sheets("analysis").Range("A:A") .

The way I'm doing it now is:

VBA Code:
Sub copy_data()
Dim lastrow As Long
lastrow = Sheets("raw_data").Range("W" & Rows.Count).End(xlUp).Row    'gets number of rows in raw_data

For i = 2 To lastrow     'copies non null values into A
    If Sheets("raw_data").Range("W" & i).Value <>0 Then
       Sheets("analysis").Range("A"&Rows.Count).End(xlUp).Row.Offset(1,0).Value = Sheets("raw_data").Range("W" & i).Value
    End If
Next i

End Sub

I would just like to know if there's a faster way of doing this, because my raw_data file is quite long....
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try this:

VBA Code:
Sub Filter_Me_Please()
'Modified  4/16/2021  8:59:36 AM  EDT
Application.ScreenUpdating = False
Dim lastrow As Long
Dim c As Long
c = 23 ' Column Number Modify this to your need
lastrow = Sheets("raw_data").Cells(Rows.Count, c).End(xlUp).Row

With Sheets("raw_data").Cells(1, c).Resize(lastrow)
    .AutoFilter 1, "<>0"
        counter = .Columns(c).SpecialCells(xlCellTypeVisible).Count
    If counter > 1 Then
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("analysis").Cells(1, 1)
    Else
        MsgBox "No values found"
    End If
    .AutoFilter
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this:

VBA Code:
Sub Filter_Me_Please()
'Modified  4/16/2021  8:59:36 AM  EDT
Application.ScreenUpdating = False
Dim lastrow As Long
Dim c As Long
c = 23 ' Column Number Modify this to your need
lastrow = Sheets("raw_data").Cells(Rows.Count, c).End(xlUp).Row

With Sheets("raw_data").Cells(1, c).Resize(lastrow)
    .AutoFilter 1, "<>0"
        counter = .Columns(c).SpecialCells(xlCellTypeVisible).Count
    If counter > 1 Then
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("analysis").Cells(1, 1)
    Else
        MsgBox "No values found"
    End If
    .AutoFilter
End With
Application.ScreenUpdating = True
End Sub
When I run this I just get the Msg "No values found". I don't quite understand how it works well enough to understand what's going wrong...
 
Upvote 0
Script looks in sheet named raw_data column W for any cell that does not have a zero entered.
So are we really looking for a zero or a empty cell?
 
Upvote 0
Is the results of zero a result of a formula. And we are looking in column w correct?
How did a zero get in the cell? Was it entered manually or the result of a formula?
It's not the result of a formula, it's from a MySQL import. I've tried it with sample sheets and sample data and it works great, but when I apply it to my sheets now it copies the whole "raw_data" sheet into "analysis"
 
Upvote 0
It's not the result of a formula, it's from a MySQL import. I've tried it with sample sheets and sample data and it works great, but when I apply it to my sheets now it copies the whole "raw_data" sheet into "analysis"
If it works on a sample sheet but not on another sheet then I would not know why.
I assume your running the script from the active sheet which is the sheet with your data in column W

I know nothing about:MySQL import
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,173
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top