Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How do I modify this line in VBA to only select the columns that have values?

Set rng = Range("A1", Range("A65536").End(xlUp)).SpecialCells(xlCellTypeVisible)

I don't think I'm doing something right since the CountLarge property is several billion cells

Here is a sample of my data

enter image description here

share|improve this question
2  
I assume you mean VBA (run from Excel) rather than a vbscript? –  brettdj Apr 7 '12 at 3:32
4  
Lastly, if you want cells with values in then you need to be using SpecialCells with xlConstants and/or XlFormulas. Your current code looks for visible cells, rather than non-empty cells. An example of how your data looks would help. –  brettdj Apr 7 '12 at 4:02
    
I agree with brettdj. Plus, you might consider using Intersect too and/or End(xlToLeft) –  JMax Apr 7 '12 at 6:24
    
Range("A65536") I assume that you are using Excel 2003 (Tag says 2007)? I agree with brettdj however the only problem is that there is a limit of 8192 areas that SpecialCells can handle in Excel 2003. See this link. support.microsoft.com/default.aspx?scid=kb;en-us;832293. Till the time it is under that limit, you'll be good to go. Noncontiguous cells that can be selected in Excel 2010 is 2,147,483,648 cells. So if you are using Excel 2010 then if you have even less to worry about :) –  Siddharth Rout Apr 7 '12 at 12:41
2  
@makerofthings7: The correct way is to use Rows.Count for all Excel Versions instead of hardcoding the numbers. For example Range(A65536) can be written as Range("A" & Rows.Count), which will work for all excel versions :) –  Siddharth Rout Apr 7 '12 at 14:52

1 Answer 1

up vote 3 down vote accepted

@SiddharthRout Yes I only need the rows that have data. I think I have it working now with End(xlToLeft) from @JMax ... Now that I'm iterating over the cells, I can just quit the For each loop once the last row is reached. I might have this working now. – makerofthings7 14 mins ago

For this neither you need .SpecialCells nor do you need to loop through the rows :)

Here is a sample code. This will copy all the rows which have data to Sheet2 (TRIED AND TESTED)

Sub Sample()
    Dim ws As Worksheet
    Dim rng As Range
    Dim LastRow As Long, LastCol As Long

    Set ws = Sheets("Sheet1")

    With ws
        LastRow = .Cells.Find(What:="*", After:=.Range("A1"), Lookat:=xlPart, _
        LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row

        LastCol = .Cells.Find(What:="*", After:=.Range("A1"), Lookat:=xlPart, _
        LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, _
        MatchCase:=False).Column

        With .Range("A1:" & Split(Cells(, LastCol).Address, "$")(1) & LastRow)
            .AutoFilter Field:=1, Criteria1:="<>"
            Set rng = ws.AutoFilter.Range
            rng.Offset(1, 0).Resize(rng.Rows.Count - 1).Copy _
            Destination:=Sheets("Sheet2").Range("A1")
        End With
    End With
End Sub

SNAPSHOT

enter image description here

I am assuming that all cells in a particular row will have data and there won't be a case like this

@makerofthings7: I think I know what exactly you are trying to do :) you don't need to use loops to achieve what you want. Just a quick question. Is it possible that say Cell C10 might have a value but B10 might not? – Siddharth Rout 12 mins ago

If there is then we will have to set the autofilter criteria accordingly.

Edit: *WAY 2*

The other way would be to sort your data, pushing the blanks way down and then copying the resulting range :)

HTH

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.