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.
 
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
This looks more efficient than looping by putting them into an array. I will give it a try and Thank you!
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
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
This worked very well and was extremely fast with he data going into an array, Thanks so much and I learned something new!
 
Upvote 0

Forum statistics

Threads
1,214,659
Messages
6,120,786
Members
448,993
Latest member
Seri

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