Copy rows based on char length

emukiss10

Board Regular
Joined
Nov 17, 2017
Messages
201
Hello,

I need help with macro to copy rows to new sheet based on length.

if cell in column J has 9 or 14 characters than copy entire row to new sheet.

I have something like this for another column and that is working but now I need length for another..


Code:
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "XXX"


    Sheets(1).Select
    Rows("1:1").Select
    Selection.Copy
    Sheets("XXX").Select
    Rows("1:1").Select
    ActiveSheet.Paste
    Sheets(1).Select


Dim cell1 As Range
Dim lastRow1 As Long, i As Long


lastRow1 = Range("H" & Rows.Count).End(xlUp).row
i = 2


For Each cell1 In Sheets(1).Range("H1:H" & lastRow1)
    If cell1.Value = "3" Or cell1.Value = "4" Then
        cell1.EntireRow.Copy Sheets("XXX").Cells(i, 1)
        i = i + 1
    End If
Next
Worksheets("XXX").Columns("A:BB").AutoFit
    Range("A1").Select

Help :)
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Something like this should work. change sheet names and ranges as need.

Code:
lr = Sheets(1).Cells(Rows.Count, "J").End(xlUp).Row

Dim cell As Range
i = 2
For Each cell In Sheets(1).Range("J2:J" & lr)
    If Len(cell) >= 9 And Len(cell) <= 14 Then
        cell.EntireRow.Copy Sheets("XXX").Range("A" & i)
        i = i + 1
    End If
Next cell
 
Upvote 0
I have a bunch of data that is not uniform - would this work to copy all cells with less than 30 characters to another sheet? I wouldn't want the whole row, just the cell containing the less than 30 characters.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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