For Each Visible cell in range, get me the left 6 characters

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
801
Office Version
  1. 365
Platform
  1. Windows
Hi all, venturing into the wild world of For Each instead of Do Until loops, have the following:

Code:
Dim LastrowAD As LongDim cl As Range
Dim visrng As Range


LastrowAD = Cells(Rows.Count, "A").End(xlUp).Row


Set visrng = Range("A2:A" & LastrowAD)


For Each cl In visrng.SpecialCells(xlCellTypeVisible)
   '>>IF LEN OF CELL IN COLUMN A IS >6, THEN GET THE LEFT 6 CHARACTERS, OTHERWISE JUST REPEAT THE CELL<<
Next


What I'm struggling with is how to get it to loop through each visible cell. It seems to do the first visible cell, but no others in the range?

I'm sure once I've got that, I can just put in the IF LEN blah blah blah part.

Cheers!
 
That is writing to column W. cl is the visible cell at each iteration, and 22 columns to the right of column A is column W.
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
ActiveCell is a property of the worksheet, that won't change.

You should be using cl, the variable that your code has looping through the range of visible cells.

Instead of cl.Address, you could be testing Left(cl.Value, 6)
 
Upvote 0
I'm glad you got it sorted out. But I noticed something new.

You don't need to test for the length. Since Left will take the leftmost characters UP TILL the second argument (i.e. Left("abc",6) = "abc"), you can just use this, with no time consuming test.
Code:
For Each cl In visrng.SpecialCells(xlCellTypeVisible).Cells
    Range("W" & cl.Row).Value = Left(Range("A" & cl.Row).Value, 6)
Next cl

Also, when I use range objects, I don't like to keep taking their row or column properties. (I also like the With...End With construction, stylisticly), you might try

Code:
For Each cl In visrng.SpecialCells(xlCellTypeVisible).Cells
    With cl
        .EntireRow.Range("W1").Value = Left(.Value, 6)
    End With
Next cl
 
Last edited:
Upvote 0
Also, when I use range objects, I don't like to keep taking their row or column properties.
Another way to do it...
Code:
Sub GetLeftSixCharactersFromVisibleCellsInColumnA()
  Dim Ar As Range
  For Each Ar In Columns("A").SpecialCells(xlVisible).Areas
    Ar.Offset(, 22) = Evaluate("IF({1},LEFT(" & Ar.Address & ",6))")
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,764
Messages
6,174,362
Members
452,558
Latest member
jswan83

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