Macro stop at Last Row

Lish rb

New Member
Joined
Apr 6, 2022
Messages
2
Office Version
  1. 2013
Platform
  1. Windows
Hello,

I'm attempting to write a macro where all populated cells in Column D are Trimmed and changed to Uppercase.
Column D is updated weekly and may contain blank cells.

Ex: 10 total rows in Column D total, but row 8 is blank. (I am new to writing macros so I may be in over my head here)

My current macro is not stopping at the last row. In the above example it should stop at row 10, but it isn't. Below is the Macro. Can anyone assist?

Sub Test()

Set JSheet = Worksheets("Sheet1")
Dim rng As Range
Set rng = [D:D]
Dim LastRow As Long

For Each Cell in rng
Cell.Value = WorksheetFunction.Trim(Cell)
Cell.Value = VBA.UCase(Cell)
Debug.Print Cell.Value

LastRow = JSheet.Cells(Rows.Count, 4).End(xlUp).Row

Next

End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Welcome to the Board!

You are setting it equal to a variable, but then are not doing anything with it.
VBA Code:
Sub Test()

Dim JSheet As Worksheet
Dim LastRow As Long
Dim rng As Range

Set JSheet = Worksheets("Sheet1")
LastRow = JSheet.Cells(JSheet.Rows.Count, 4).End(xlUp).Row
Set rng = JSheet.Range("D1:D" & LastRow)

For Each Cell In rng
    Cell.Value = WorksheetFunction.Trim(Cell)
    Cell.Value = VBA.UCase(Cell)
    Debug.Print Cell.Value
Next

End Sub
 
Upvote 0
Solution
Welcome to the Board!

You are setting it equal to a variable, but then are not doing anything with it.
VBA Code:
Sub Test()

Dim JSheet As Worksheet
Dim LastRow As Long
Dim rng As Range

Set JSheet = Worksheets("Sheet1")
LastRow = JSheet.Cells(JSheet.Rows.Count, 4).End(xlUp).Row
Set rng = JSheet.Range("D1:D" & LastRow)

For Each Cell In rng
    Cell.Value = WorksheetFunction.Trim(Cell)
    Cell.Value = VBA.UCase(Cell)
    Debug.Print Cell.Value
Next

End Sub
That worked perfectly. Thankyou so much for your help!
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,215,035
Messages
6,122,791
Members
449,095
Latest member
m_smith_solihull

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