VBA Data Sort (Keep Cell Formating while Only Moving Text)

scott_86_

New Member
Joined
Sep 27, 2018
Messages
40
Office Version
  1. 365
Platform
  1. Windows
Hi, I currently have a code that automatically sorts data on multiple worksheets when the workbook is open and it works fine.

Most cells in the E9:J28 Range have no fill colour; however, there are some cells (in rows) that are intentionally blacked out.

Can anyone help me out to tweak this VBA code so that:
- Data still gets sorted the same.
- ONLY the text moves.
- All cells in the E9:J28 range keep their current formatting (the only format difference in the range is fill colour as mentioned).

Thanks in advance!


Code:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In Worksheets
    If ws.Name <> "Data" Then

'Top Left
        ws.Sort.SortFields.Clear
        ws.Sort.SortFields.Add Key:=Range("J9:J28"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        ws.Sort.SortFields.Add Key:=Range("E9:E28"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ws.Sort
            .SetRange Range("E9:J28")
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    
End If
    
Next ws

   
    End Sub
 
Last edited:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
It might be easier to clear the formatting on each sheet, then reapply it after the sort (via the macro, of course)???......especially if there isn't much of it !!
 
Last edited:
Upvote 0
Try applying the colour by Conditional Formatting instead of direct manual formatting. For example, the red cell below is formatted with the simple Conditional Formatting formula shown.

Excel Workbook
EFGHIJ
9199419510
10171021226
11108191075
121915201565
137191721119
1413201016108
1591813141816
1661615597
179167201415
182015151158
19171361692
202018182108
2181242079
226128897
2312746115
24215175916
25151814191111
2693614153
2717102111213
288161831712
sort keep format
Cells with Conditional Formatting
CellConditionCell FormatStop If True
F101. / Formula is =1Abc




After using your code to sort, the sheet now looks like this:

Excel Workbook
EFGHIJ
9171361692
1093614153
11108191075
1212746115
131915201565
14171021226
1561615597
166128897
1713201016108
182015151158
192018182108
2081242079
21199419510
22151814191111
238161831712
2417102111213
259167201415
26215175916
2791813141816
287191721119
sort keep format
Cells with Conditional Formatting
CellConditionCell FormatStop If True
F101. / Formula is =1Abc
 
Upvote 0
Hi, thanks for your replies.

After Peter's response, I'm now thinking conditional formatting might work.

Is it possible to have a 'range of cells' with the rule:
- If a cell is not blank (text, numbers), it will have no fill.
- If a cell is blank, it will have a manual fill colour format.

Thanks again.
 
Upvote 0
Is it possible to have a 'range of cells' with the rule:
Yes, but the CF would still have to be applied directly to the relevant cells - and the following does not make sense to me.
- If a cell is not blank (text, numbers), it will have no fill.
- If a cell is blank, it will have a manual fill colour format.
Taking a smaller example as shown below, your description above indicates to me that you would colour F10:I10 and G13:J13 as they are blank.


Book1
EFGHIJ
9371627
1032
11563877
12146599
1336
14233395
sort keep format (2)



You said you wanted to sort the cells but keep the formatting where it is. The result would be as shown below. However, now the coloured cells are not empty. Is that what you want?


Book1
EFGHIJ
932
10233395
11371627
12563877
13146599
1436
sort keep format (2)
 
Upvote 0
Peter,

After a bit of trial and error I got it to work. Thanks everyone for your help!

The pathway I used was:

  • Home
  • Conditioning Formatting
  • Highlight Cell Rules
  • More Rules
    • Format only cells with:
    • No Blanks
    • Format
      • *Format as required...*
 
Upvote 0
Glad you got something that does what you want. :)
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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