improving this piece of code

Hlatigo

Well-known Member
Joined
Jun 3, 2002
Messages
677
I am trying to find a way where I can convert this piece of code into one where I can combine the ranges. Although I am ok with coding, i feel its time I start to learn how to write more efficient code. any help would be great.





Code:
 'BenchMark
    lastrow = Range("D4000").End(xlUp).Row
    Range("D6:D" & lastrow).Select
      With Selection
         .NumberFormat = "general"
      End With
        
    lastrow = Range("E4000").End(xlUp).Row
    Range("E6:E" & lastrow).Select
      With Selection
        .NumberFormat = "m/d/yyyy"
      End With
    
    'minorcurve1
    lastrow = Range("L4000").End(xlUp).Row
    Range("L6:L" & lastrow).Select
      With Selection
         .NumberFormat = "general"
      End With
        
    lastrow = Range("M4000").End(xlUp).Row
    Range("M6:M" & lastrow).Select
      With Selection
        .NumberFormat = "m/d/yyyy"
      End With
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Code:
lastrow = Range("D4000").End(xlUp).Row
Range("D6:M" & lastrow).NumberFormat = "general"
 
Last edited:
Upvote 0
Well for a start you can lose the Select and the With.
Code:
 'BenchMark
    lastrow = Range("D4000").End(xlUp).Row
    Range("D6:D" & lastrow).NumberFormat = "general"
    
    lastrow = Range("E4000").End(xlUp).Row
    Range("E6:E" & lastrow).NumberFormat = "m/d/yyyy"
      
    'minorcurve1
    lastrow = Range("L4000").End(xlUp).Row
    Range("L6:L" & lastrow).NumberFormat = "general"
        
    lastrow = Range("M4000").End(xlUp).Row
    Range("M6:M" & lastrow).umberFormat = "m/d/yyyy"
And I also wonder if you actually need to find the last row 4 times.:eek:
 
Upvote 0
Try this

Code:
lastrow = Activesheet.UsedRange.Row + ActiveSheet.UsedRange.Rows.Count - 1
Range("D6:D" & lastrow & ",L6:L" & lastrow).NumberFormat = "general"
Range("E6:E" & lastrow & ",M6:M" & lastrow).NumberFormat = "m/d/yyyy"

Hope that helps...
 
Upvote 0
Hi neil...thanks but wouldnt that format everything between those columns?


Hi Norie!
I thought about that as well, the first last row (benchmark) would predicate where the others will have their last row even if data continues past that row.

So is it possible to set all based on the benchmark.

the cose I show is only half. I have this duplicating 4 times right now and I assume people will request for more (minor curves), which would force me to duplicate over and over.
 
Upvote 0
thanks jonmo1

worked great. I just expanded it to include the other curves

Code:
lastrow = ActiveSheet.UsedRange.Row + ActiveSheet.UsedRange.Rows.Count - 1
    Range("D6:D" & lastrow & ",L6:L" & lastrow & ",U6:U" & lastrow & ",AD6:AD" & lastrow).NumberFormat = "general"
    Range("E6:E" & lastrow & ",M6:M" & lastrow & ",V6:V" & lastrow & ",AE6:AE" & lastrow).NumberFormat = "m/d/yyyy"
 
Upvote 0

Forum statistics

Threads
1,214,397
Messages
6,119,271
Members
448,882
Latest member
Lorie1693

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