VBA Automate Re-sort Complex Table

liz123

New Member
Joined
Aug 26, 2016
Messages
8
Dear Community,


I would like assistance to resort a series of logistic regressions tables that include (1) the variable name; (2) the standard error; (3) EXP(B); and (4) a starred version of significance level.

I have a series of tables one after the other in a single sheet, which use the following format:


VarS.E.Exp(B)
red(.137)1.044
blue(.141).936
green(.150)1.427*
Constant(.197).195
VarS.E.Exp(B)
red(.194)1.522*
blue(.088).897
black(.078).787**
Constant(.187).200
VarS.E.Exp(B)
red(.195)1.518*
blue(.092).917
green(.079).779**
black(.100).898
Constant(.191).215

<tbody>
</tbody>

I would like to find a simple method to reformat these tables into a single table (variables, then EXP(B), then significance, and standard errors below EXP(B)) as follows:

Var123
red1.0441.522*1.518*
(.137)(.194)(.195)
blue.936.897.917
(.141)(.088)(.092)
green1.427*--.779**
(.150)--(.079)
black--.787**.898
--(.078)(.100)
Constant.195.200.215
(.197)(.187)(.191)

<tbody>
</tbody>


I can't seem to get my offset coding and logic correct for this, and would really appreciate it if someone could help me out. Any tips to keep the cell formatting using the offset command would be very helpful as well.

One caveat: There are a varied number of tables, and variables are not always ordered similarly. The final table always contains the correct variable order and full model, though. Therefore, I need for the structure of the model to be derived from the last model in the set. MickG helped me with a simpler version of this table previously: https://www.mrexcel.com/forum/excel...st-sorting-based-last-several-rows-sheet.html .

Thanks in advance to anyone who can help me out!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
This code replicates the results you want, except for green and black. The columns that have '--' values in your original post are filled with the values in the next column. So instead of

green1.427*--.779**
(.150)--(.079)

<tbody>
</tbody>

it's
green1.427.779**
(.150)(.079)

<tbody>
</tbody>


Not sure how big of a deal that is. Also, the code uses a class module so you'll need to create that as well.

Here is the main code. I have it looking at the sheet 'Original' where your data is. You'll need to change that part of the code to whatever the name of your sheet actually is. Also, it puts the results into a blank sheet called 'New'. So, you'll need a blank sheet for your results. You can name it new, or have a different name and adjust the code.

Code:
Sub Sort()
Dim Original As Worksheet 'Where your data is
Dim Output As Worksheet 'Blank sheet
Dim R As Range
Dim AR()
Dim dict As Object
Dim c As vColor
Dim i As Long


Set Original = Sheets("Original")
Set Output = Sheets("New")
Set dict = CreateObject("Scripting.Dictionary")
Set R = Original.UsedRange 'May need to change depending on what is in sheet
AR = R.value


For i = 1 To UBound(AR)
    If AR(i, 1) <> "Var" And AR(i, 1) <> "" Then
        If Not dict.Exists(AR(i, 1)) Then
            Set c = New vColor
            c.Name = AR(i, 1)
            c.ExpB = AR(i, 3) & " " & AR(i, 4)
            c.SE = AR(i, 2)
            dict.Add c.Name, c
        Else
            Set c = dict.Item(AR(i, 1))
            c.ExpB = AR(i, 3) & " " & AR(i, 4)
            c.SE = AR(i, 2)
        End If
    End If
Next i


Dim ro As Long
ro = 1


With Output
For Each v In dict.Items
    .Cells(ro, 1) = v.Name
    For i = 1 To v.Count
        v.Arg = i
        .Cells(ro, i + 1) = v.ExpB
        .Cells(ro + 1, i + 1) = v.SE
    Next i
    ro = ro + 2
Next v
End With


End Sub

Then you'll need to go to Insert-->Class Module. Then in the properties for the class, change the name to vColor. Then paste the following code.

Code:
Private SE_ As Collection
Private ExpB_ As Collection
Private Name_ As String
Private Arg_ As Integer


Private Sub Class_Initialize()
    Set ExpB_ = New Collection
    Set SE_ = New Collection
End Sub


Property Get Arg() As Integer
    Arg = Arg_
End Property


Property Let Arg(value As Integer)
    Arg_ = value
End Property


Function Count()
    Count = ExpB_.Count
End Function


Property Get SE() As Variant
    SE = SE_(Arg)
End Property


Property Let SE(value As Variant)
    SE_.Add value
End Property


Property Get ExpB() As Variant
    ExpB = ExpB_(Arg)
End Property


Property Let ExpB(value As Variant)
    ExpB_.Add value
End Property


Property Get Name() As String
    Name = Name_
End Property


Property Let Name(value As String)
    Name_ = value
End Property
 
Upvote 0
This code replicates the results you want, except for green and black. The columns that have '--' values in your original post are filled with the values in the next column. So instead of

green1.427*--.779**
(.150)--(.079)

<tbody>
</tbody>

it's
green1.427.779**
(.150)(.079)

<tbody>
</tbody>


Not sure how big of a deal that is. Also, the code uses a class module so you'll need to create that as well.


[/CODE]

Thank you for spending time compiling this lrobbo314. Unfortunately, it's really important for the structure of the blanks to remain the same. This wasn't an approach I had thought about, and I'll definitely try to work through this code a little bit and see if I can adjust it to account for those. I'm wondering if I could find a way to prepopulate some of the blank spots with a filler to retain the structure.

Does anyone else have any other ideas that would produce a table that retains the integrity of the blanks in the tables? I get the sense that offset commands will produce what I'm looking for -- but I'm not quite sure how exactly it should look. I'm totally stumped on this one.
 
Upvote 0
You can retain the format if you make sure that every table has the same colors in it. So, go through each table and make sure that there is a row for Red, Blue, Green, Black, and Constant. Some tables are missing colors. So, you would add a row and then just have blank values for the other values. e.g. Black,"","",""
 
Upvote 0
You can retain the format if you make sure that every table has the same colors in it. So, go through each table and make sure that there is a row for Red, Blue, Green, Black, and Constant. Some tables are missing colors. So, you would add a row and then just have blank values for the other values. e.g. Black,"","",""

Thank you for the suggestion. Because the tables are produced automatically from SPSS, and have atypical formations and variables, I can't anticipate the models for each. I'm hoping that someone will be able to approach this similar to what MickG did in this link: https://www.mrexcel.com/forum/excel...st-sorting-based-last-several-rows-sheet.html . He used a series of R.Offset commands that may work for this and retain the structure. I produce the tables so that each of the final tables includes the complete model structure, and I'm thinking working from the bottom up with offsets is the key to solving this.
 
Upvote 0

Forum statistics

Threads
1,215,248
Messages
6,123,867
Members
449,130
Latest member
lolasmith

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