Looping thru cels in a dynamic range

U4kaDW3Qv1

New Member
Joined
Feb 1, 2017
Messages
5
I have a range (2 columns) that will be dynamic (meaning the columns the data is in will be variable AND the length in number of rows in the range will also be variable).

I have a short bit of code (see below) that is intended to strip out leading text from the first column, leaving only the date that is in the traiing part of the cell. This is in a Sub that would be called through the loop (I'd imagine) to go through all the cells i this dynamic range.

----------------------------------

Sub DoIt()

'Beginning in active cell, offsets results by two columns, then strips out leading text and returns the text eginning at character 8 and returns the next 10 characters.

' this allows a date with up to suceeding 10 characters to be returned.

ActiveCell.Offset(0, 0) = Mid(ActiveCell, 8, 10)

End Sub

----------------------------------

The data looks like this. with the first two rows appearing as I would like after applying the code above and the succeeding rows as they would be beforehand.

DateAmount
1/1/2023
36801.03
2/1/202321912.81
Sum of 3/1/202319369.54
Sum of 4/1/202329971.53

But, I have been uable to get a looping macro that goes through this range to work (and I've searched and tried a lot).

Any sample code you could suggest would be appreciated.
Thank you.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
VBA Code:
Sub LoopColumn()
'
    Dim c       As Range
    Dim LastRow As Long
'
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
'
    For Each c In Range("A1:A" & LastRow)
        c = Mid(c, 8, 10)
    Next c
End Sub
 
Upvote 0
Solution
This was very helpful. Thank you. Since my data is created dynamically, I never know where the starting column will be, so I needed to modify the code some rather than referring to a fixed column. This worked for me, but I need to test some other scenarios with different versions of the file. But the main thing I needed help with, was the looping, which works perfectly.
-------------------------------------
Sub LoopColumn1()
'
Dim rngMyRange As Range

Range(Selection, Selection.End(xlDown)).Select

Set rngMyRange = Selection
Dim c As Range
'
Range(ActiveCell, ActiveCell.End(xlDown)).Select
'
For Each c In rngMyRange
c = Mid(c, 8, 10)
Next c

'formats date in short date

rngMyRange.NumberFormat = "mm/dd/yyyy"
End Sub
-------------------------------------
Thanks again, Very much appreciated.
 
Upvote 0
But the main thing I needed help with, was the looping,
What about doing them all at once instead of looping a row at a time?

VBA Code:
Sub ExtractDate()
  With Range(ActiveCell, ActiveCell.End(xlDown))
    .NumberFormat = "mm/dd/yyyy"
    .TextToColumns DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 9), Array(7, 1))
  End With
End Sub
 
Upvote 0
.. or probably a bit simpler again

VBA Code:
Sub ExtractDate_v2()
  With Range(ActiveCell, ActiveCell.End(xlDown))
    .NumberFormat = "mm/dd/yyyy"
    .Replace What:="* ", Replacement:="", LookAt:=xlPart
  End With
End Sub
 
Upvote 0
@Peter_SSs last code doesn't work. ;)
That tells me nothing about what you have and in what way it did not work for you. ;)
Could it be that you forgot to select an appropriate cell before running the code?

It worked for my test data.
 
Upvote 0
Book1
AB
111111111/1/2023
222222222/1/2023
333333333/1/2023
444444444/1/2023
5
Sheet1


;)
 
Upvote 0
My suggestions were both based on the format of sample data as provided by the OP. ;)
 
Upvote 0

Forum statistics

Threads
1,215,398
Messages
6,124,693
Members
449,179
Latest member
kfhw720

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