PPRuNe Forums

Go Back   PPRuNe Forums > Supplementary Forums > Computer/Internet Issues & Troubleshooting
Forgotten your Username/Password?
Register FAQ Calendar Advertise Mark Forums Read

Computer/Internet Issues & Troubleshooting Anyone with questions about the terribly complex world of computers or the internet should try here. We will also try and help with troubleshooting any technical problems you may have with the forums.


Reply
 
Thread Tools Display Modes
Old 4th November 2009, 12:53   #1 (permalink)
 
Join Date: Mar 2006
Location: Chez Sprog
Posts: 272
Any VBA aces here?

I have a spreadsheet that I use to control & record all the sales & operations data in my work. It starts the year at zero rows & ends it on anywhere between 1000-2500 rows & has something like 20 columns as below:



What I'm trying & failing to do is create a macro, or more accurately modify someone else's vba code to make the rows on this sheet sort automatically, in ascending date order by columns H, then K, which are collection date & delivery date, each time a new job is added, so that the newest jobs are at the bottom of the worksheet.

Anyone handy with this kind of thing? I have looked on Mr. Excel & a few other places without success.
Sprogget is offline   Reply
Old 4th November 2009, 13:14   #2 (permalink)
 
Join Date: Mar 2006
Location: Chez Sprog
Posts: 272
Ah, getting somewhere. I recorded a macro, giving me this code:
Code:
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 04/11/2009 by Sproggett
'
' Keyboard Shortcut: Ctrl+s
'
    Range("A6:V408").Select
    Selection.Sort Key1:=Range("H6"), Order1:=xlAscending, Key2:=Range("K6") _
        , Order2:=xlAscending, Header:=xlNo, OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:= _
        xlSortNormal
End Sub
The end range I have higlighted is now causing me a problem. If I add a row to the sheet to record a new sale, it only sorts up to row 408. So my new question is how do I ensure the macro continues to include new rows as the spreadsheet expands?
Sprogget is offline   Reply
Old 4th November 2009, 16:19   #3 (permalink)
 
Join Date: Apr 2002
Location: Deepest Oxfordshire
Posts: 203
Hi Sprogget

You need to use a named range and a bit of code to define the range variably according to the data content within. The actual code varies slightly according to the actual content you want to detect.

Try googling 'create expanding range in Excel'. There are plenty of ideas, including this 'official' one:

How to create a dynamic defined range in an Excel worksheet

Hope this helps

Gadget
Captain Gadget is offline   Reply
Old 4th November 2009, 16:52   #4 (permalink)
 
Join Date: Mar 2006
Location: Chez Sprog
Posts: 272
Thx Gadget. Looks like I'll have to do a bit of reading around OFFSET...
Sprogget is offline   Reply
Old 4th November 2009, 22:05   #5 (permalink)
 
Join Date: Jan 2006
Location: UK
Posts: 38
Hi Sprogget.

Take a look at worksheet.usedrange which should return all the rows used in the sheet. It can be a little temperemental if new rows have been added so if it's a possibility, you could add workbook.save to the beginning of the routine. In the majority of workbooks, especially those with perfectly rectangular ranges, as yours appears to have, usedrange is reliable after save.

Alternatively, here's a function which will calculate the used range, thereby providing accuracy.

VBA Express : Excel - True Used Range in Excel

Hope this helps.

Cheers

Simon
Simonta is offline   Reply
Old 4th November 2009, 22:16   #6 (permalink)
 
Join Date: Jan 2006
Location: UK
Posts: 38
Just noticed that you want to do this after a new row is added. I assume that the user has to remember to click a sort button or some such after adding a new row?

The coding gets a lot more complex but did you know that:

1. You can add a button called "New row", or similar, which would present the user with a nice form to fill in (complete with validity checks if needed),add the row then sort the sheet.
2. There are "events" in Excel which you can attach code to. One event is worksheet_changed which is called every time a change is made to the worksheet. This code could determine if a new row has been added then go ahead and sort.

Just FYI....

PS. I'm a nice guy ;-), if you fancy any of these approaches, PM me and I'd be happy to code this for you.
Simonta is offline   Reply
Reply


Thread Tools
Display Modes


Posting Rules
vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


All times are GMT +1. The time now is 20:42.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC7
© 1996-2009 The Professional Pilots Rumour Network

As these are anonymous forums the origins of the contributions may be opposite to what may be apparent. In fact the press may use it, or the unscrupulous, or sciolists*, to elicit certain reactions.

*"sciolist"... Noun, archaic. "a person who pretends to be knowledgeable and well informed".