Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 15th, 2012, 06:20 PM
Registered User
Points: 14, Level: 1
Points: 14, Level: 1 Points: 14, Level: 1 Points: 14, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default Code for dynamic Range value in recorded macro

Hi all,

I recorded a macro that sorts a spreadsheet by 4 columns. Below is the VBA that was recorded. How do I replace the fixed range (for example: Range("B2:B867") ) with the row count instead of 867? Thank you!

Cells.Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add Key:=Range("B2:B867") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add Key:=Range("F2:F867") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add Key:=Range("C2:C867") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add Key:=Range("L2:L867") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:N867")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Reply With Quote
  #2 (permalink)  
Old February 29th, 2012, 09:22 PM
Friend of Wrox
Points: 2,950, Level: 22
Points: 2,950, Level: 22 Points: 2,950, Level: 22 Points: 2,950, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
Posts: 788
Thanks: 1
Thanked 51 Times in 47 Posts
Default

Hi

You can try something like this

Code:
i1 = ActiveWorkbook.Worksheets("Sheet1").Cells.SpecialCells(xlCellTypeLastCell).Row
SetRange Range("A1:N" & i1)
SpecialCells is one way to identify the last used cell in a sheet.

There are otherways like UsedRange, xlEnd etc

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
Reply With Quote
The Following User Says Thank You to Shasur For This Useful Post:
J.Suzuki (March 30th, 2012)
  #3 (permalink)  
Old March 30th, 2012, 04:26 PM
Registered User
Points: 14, Level: 1
Points: 14, Level: 1 Points: 14, Level: 1 Points: 14, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default Thank you.

Thank you, Shasur. The SpecialCells solution worked!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Use a macro to sort a user selected range rmccafferty Excel VBA 3 April 26th, 2011 04:32 PM
VBA Macro FIND THIS MONTH in a given range mvatoi Excel VBA 3 August 24th, 2007 08:12 AM
Excel Formula End Range Changes when Macro Is Run maaron Excel VBA 0 October 11th, 2005 10:56 AM
Excel Formula End Range Changes when Macro Is Run maaron Beginning VB 6 0 October 11th, 2005 10:33 AM
getting the un recorded faizan Classic ASP Basics 1 July 19th, 2005 06:47 PM



All times are GMT -4. The time now is 11:47 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.