Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Looping thru cells in selected range with VBA

0

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 varibale). See sample file attached to post.

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 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.

Date

Amount

1/1/2023

36801.03

2/1/2023

21912.81

Sum of 3/1/2023

19369.54

Sum of 4/1/2023

29971.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.

Answer
Discuss

Answers

0

Please follow Don's tutorial Excel Macro Class 3 - Looping through Ranges & Working with Cells(in the Tutorial section of this site) and ask another question if you get stuck. Instead of ActiveCell you can use the variable c in the tutorial macro (and c.Value to change its value) so something like:

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


    c.Offset(0, 2).Value = Mid(c.Value, 8, 10) 
where the 2 in the Offset statement causes the value to be written 2 columns to the right of the cell c.
Discuss


Answer the Question

You must create an account to use the forum. Create an Account or Login