开发者

sort a webquery result in excel vba

I have a webquery that I call by refreshing the cell where the query starts. something like the following:

Sheets(sheetBeingProcessed).Range(queryAdr).QueryTable.Refresh BackgroundQuery:=False

I refresh that using vba, one click, my macro goes through all the sheets, refreshes them one by one, each one comes back with different rows of data each days (sometimes 0 rows), that's all acceptable, and all looks good.

Now, I want to sort the results by the "maturity date" column of each sheet. Any idea how I can do that?

The 2 difficulties are, 1 how to tell where the "maturity date" column is, that is rather easy. The second problem... how can I sort the results I get??

I tried to activate each worksheet and select the range and sort, but that didn't work

ActiveWorkbook.Worksheets(sheet).Activate
ActiveWorkbook.Worksheets(sheet).Range(sortStartAddress).Select
Dim resultRange As Variant

Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Set resultRange = ActiveWorkbook.ActiveSheet.Selection

the last line in the below is failing...

I'm sure there must be a cleaner way without flipping between the worksheets activating the开发者_JS百科m and selecting ranges.. which looks ugly... any ideas?


I don't think you have to activate each sheet in order to sort them.

Also, you should be able to select the entire region by using the CurrentRegion option. See if this works for what you are attempting:

ActiveWorkbook.Worksheets(sheet).Range(sortStartAddress).Select
Dim resultRange as Range
set resultRange = selection.CurrentRegion.Select


ActiveWorkbook.Worksheets(sheet).Range(sortStartAddress).CurrentRegion.Sort 'add sort options...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜