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

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

njimack

Well-known Member
Joined
Jun 17, 2005
Messages
7,772
Code:
lastrow = Range("D4000").End(xlUp).Row
Range("D6:M" & lastrow).NumberFormat = "general"
 
Last edited:
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
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

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
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

Hlatigo

Well-known Member
Joined
Jun 3, 2002
Messages
677
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

Hlatigo

Well-known Member
Joined
Jun 3, 2002
Messages
677
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,191,552
Messages
5,987,232
Members
440,086
Latest member
Mahi786

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
Top