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

Knockoutpie

New Member
Joined
Sep 10, 2018
Messages
14
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>
 

Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
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:

Watch MrExcel Video

Forum statistics

Threads
1,102,448
Messages
5,486,964
Members
407,574
Latest member
Greso

This Week's Hot Topics

Top