VBA sort is moving the form control buttons along with the data

MattCSI

New Member
Joined
May 11, 2013
Messages
15
I have about 200 rows of data, each with multiple form control (not the ActiveX control) buttons. I create the buttons named according to their row, and set the .placement property to "don't move or size with cells"

Code:
bBtn.Placement = xlFreeFloating

That part is working, and I can see that the buttons are named and placed properly. The problem arises when I sort the rows. The buttons move, regardless of the .placement setting. I'm using this code to sort:

Code:
set wb = ThisWorkbook
set ws = ThisWorksheet
set rFullRange = ws.Range(Cells(lFirstRow, 1), Cells(lLastRow, lLastCol))
rFullRange.Sort key1:=ws.Columns(lSortCol), order1:=xlAscending, key2:=ws.Columns(lJobNumCol), order2:=xlAscending, Header:=xlNo

I didn't include the full code - the variables correspond to column and row numbers. The sorting code is also working fine. My problem is just figuring out how to not have the buttons move when I sort.

Is there any way to accomplish that?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I've continued to work on this today, taking another approach to sorting, and I found something strange and frustrating.

For background, my range begins on row 14, and that row is a filtered header row. I found this morning that if I use Excel's UI dropdown at the top of a column to sort that column, it sorts the whole range very quickly and the buttons don't move. That's perfect.

Hoping I was on to something, I recorded a macro of that process. I adapted the code, and used it to replace the sorting code in my previous subroutine. But when I ran that subroutine, it sorted slower, visibly selected the entire range (which didn't happen using the UI), and most frustratingly, it moved the buttons.

Assuming I had messed up in adapting the code, I recorded a fresh copy of the same macro.

This time I ran the macro as-is. I did not modify it in any way, and did not add it to my own code. When I ran it, instead behaving as it did while I was recording, it selected the whole range, sorted the data, and moved the buttons.

Why would the recorded code behave so differently than the operation I recorded it from? Does anyone know how to make sort work exactly like it does when using the UI?

Here is the full unedited macro for reference:
Code:
Sub autofiltersort3()
'
' autofiltersort3 Macro
'

'
    ActiveWorkbook.Worksheets("Main").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Main").AutoFilter.Sort.SortFields.Add Key:=Range( _
        "A14:A175"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("Main").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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