How do I rearrange columns based on cell value and not headers?

Knockoutpie

Board Regular
Joined
Sep 10, 2018
Messages
116
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

Is there a way to rearrange the columns based on values in the cell and not based on the headers?

for example the first rearrange would be Username, Hint, Password, Site, the second rearrange would be Password, Username, Site, Hint.

If i'm not being clear enough, please let me know and i'll try and be more descriptive.

1324
2143
4132
UsernamePasswordHintSite

<tbody>
</tbody>
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
With your data in "A1:D4" then
Place this code in a Button as per code.
Each time the code runs the data will be shown starting "F1", With columns rearranged as per rows.
Code:
Private [COLOR="Navy"]Sub[/COLOR] CommandButton3_Click()
[COLOR="Navy"]Dim[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] K, num [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
Ray = Range("A1:D4")
Static c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
 c = c + 1
 c = IIf(c = UBound(Ray, 1), 1, c)
 [COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
    [COLOR="Navy"]For[/COLOR] Ac = 1 To UBound(Ray, 2)
        [COLOR="Navy"]If[/COLOR] Not .Exists(Ray(c, Ac)) [COLOR="Navy"]Then[/COLOR]
            .Add Ray(c, Ac), Ac
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR]

ReDim nRay(1 To 4)
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
        n = n + 1
        num = Application.Small(.keys(), n)
        nRay(n) = .Item(num)
    [COLOR="Navy"]Next[/COLOR] K

Range("F1").Resize(4, 4).Value = Application.Index(Ray, Evaluate("Row(1:" & UBound(Ray, 1) & ")"), nRay)
[COLOR="Navy"]End[/COLOR] With

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,511
Messages
6,114,054
Members
448,543
Latest member
MartinLarkin

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