Increment to next visible line via vba

kkjensen

Active Member
Joined
Jun 22, 2004
Messages
354
I'm creating another data file (KML for import into google earth) from data in a table. Pretty basic stuff.

I can't get this loop running because it keeps incrementing by exactly one line. The code works fine except in the scenario where I use excel's filter function to shorten the list.

How can I get this to increment the active cell to the next visible line after the filter is set? I've searched quite a bit and it seems like most people asking "how to go down in a filtered list with vba" are finishing their journey in another direction completely. I need to keep this process rolling for several thousand lines.

<code>
Range("D1").Select <---This is the header where the data filters reside
ActiveCell.Offset(1, 0).Select <-----should be the first visible line but just returns data from line2, even if it's filtered
Do While ActiveCell.Value <> 0
'cellValue = rng.Cells(i, j).Value
ItemType = ActiveCell.Offset(0, 0).Value
ItemName = ActiveCell.Offset(0, 1).Value
ItemCoords = ActiveCell.Offset(0, 2).Value
ItemLength = ActiveCell.Offset(0, 3).Value
ItemBranch = ActiveCell.Offset(0, 4).Value

'blah blah writing text to the file...

ActiveCell.Offset(1, 0).Select
Loop
</code>

A million thanks in advance! I'm a bit rusty on my vba prowess these days...
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Change to suit. Add the reference object as commented.
Code:
Sub Main()
  Dim r As Range, f%, p$
  
  p = Environ("temp") & "\Main.txt"
  
  Set r = ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible)
  r.Copy
  
  f = FreeFile
  Open p For Output As #f
  Print #f, getClipboard
  Close #f
  
  Application.CutCopyMode = False
  [A1].Select
End Sub

'Tools > References... > Microsoft Forms 2.0 Object
Function getClipboard()
    Dim MyData As DataObject
    On Error Resume Next
    Set MyData = New DataObject
    MyData.GetFromClipboard
    getClipboard = MyData.GetText
End Function
 
Upvote 0
Maybe
Code:
For Each cll In Range("D2", Range("D2").End(xlDown)).SpecialCells(xlCellTypeVisible)
    cll.Select
    ' your lines here
Next cll
 
Upvote 0
Ah...I think that will do it. One of the options I worked through previously was having the user select the range of data to be exported. I wanted to automate it more to dummy-proof it and this should do the trick.

Thanks!

Once it's working, I'll post the final code here so later searchers might find another option.
 
Upvote 0

Forum statistics

Threads
1,215,676
Messages
6,126,161
Members
449,295
Latest member
DSBerry

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