VBA for if a cell contains certain number of characters

  • Thread starter Thread starter Legacy 143009
  • Start date Start date
L

Legacy 143009

Guest
Hi Guys!

I am looking for a very simple VBA that moves data (NOT COPY) from one colomn for ex. (K1:K256) to another (G1:G256) if cell contains a certain number of character (for ex.14 haracters).

Thanks a lot!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try

Code:
Sub ctest()
Dim i As Long
For i = 1 To 256
    With Range("K" & i)
        If Len(.Value) = 14 Then .Cut Destination:=.Offset(, -4)
    End With
Next i
End Sub
 
Upvote 0
Solution
Where is address G in this formula?

Try

Code:
Sub ctest()
Dim i As Long
For i = 1 To 256
    With Range("K" & i)
        If Len(.Value) = 14 Then .Cut Destination:=.Offset(, -4)
    End With
Next i
End Sub
Ahaa!! sorry!! Offset-4!
 
Upvote 0
In the offset

Rich (BB code):
If Len(.Value) = 14 Then .Cut Destination:=.Offset(, -4)
 
Upvote 0
OK, I will try to make it works correct. What if I had no row limitation and I want entire K colmn?
 
Upvote 0
Try:

HTML:
Sub CharCount()
    Dim cell As Range
    Dim bottomK As Integer
    bottomK = Range("k" & Rows.Count).End(xlUp).Row
    Dim rng As Range
    Set rng = Range("K1:K" & bottomK)
        For Each cell In rng
            If Len(cell) = 14 Then
                cell.Copy cell.Offset(0, -4)
                cell.ClearContents
            End If
        Next cell
End Sub
 
Upvote 0
capture3.png


As you see I have some date cells in K column which are exactly 14 characters but there is no change in the sheet. Is there any other way to trim them from K column to G column?
 
Upvote 0

Forum statistics

Threads
1,222,532
Messages
6,166,602
Members
452,054
Latest member
ibale

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