Pls help on VBA code: Table to list etc

mrchonginhk

Well-known Member
Joined
Dec 3, 2004
Messages
679
Hi I have a table like this
VBAAssignment1.xls
ABCDE
1Q2
2Col1Col2Col3Col4
3Row11234
4Row25678
5Row39101112
6Row413141516
7Row517181920
Q2


Then, I need to write a FUNCTION called
Tableonly that returns a table as range without extraneous emoty rows and column. But, if user clicks a cell outside table, return only cell where user has clicked. Hints from my boss: Use COUNTA and range.EntireRow.

Then using above write program that selects only data from table (Bosss say use range.size method)

Then write another program select only col heading
Then write another select only row headings

Hence, write a program converting the table in a list. ie the table above should becomes:-
VBAAssignment1.xls
GHIJ
6Row1Col11
7Row2Col15
8Row3Col19
9Row4Col113
10Row5Col117
11Row1Col22
12Row2Col26
13Row3Col210
14Row4Col214
15Row5Col218
16Row1Col33
17Row2Col37
18Row3Col311
19Row4Col315
20Row5Col319
21Row1Col44
22Row2Col48
23Row3Col412
24Row4Col416
25Row5Col420
Q2


Pls help. Any one part will help ... Thanks.

:help:
 
Hi

would you like to try this one, too?
in Stardard module or ThisWorkbook module
Code:
Sub trans_P()
Dim Sr As Long, Lr As Long, Sc As Integer, Lc As Integer, nR As Long, nC As Integer
Dim a(), b(), i As Integer, ii As Long, iii As Long, z As Long
With Sheets("sheet1").Range("b3").CurrentRegion
    Sr = .Row
    nR = .Rows.Count
    Sc = .Column
    nC = .Columns.Count
End With
    Lr = Sr + nR - 1
    Lc = Sc + nC - 1
    ReDim a(Sr To Lr, Sc To Lc)
    ReDim b(Sr To nR * nC + Sr + 1, Sc To Lc)
With Sheets("sheet1")
    .Columns("j:l").Clear
    a = .Range("b3").CurrentRegion.Value
    iii = -1
    For i = Sc + 1 To Lc
        iii = iii + 1
        z = iii * (nR - 1)
        For ii = Sr To Lr - 1
            b(ii + z, 1) = a(ii, 1)
            b(ii + z, 2) = a(Sr - 1, i)
            b(ii + z, 3) = a(ii, i)
        Next
    Next
    With .Range("j2").Resize((nR - 1) * (nC - 1), 3)
        .Value = b
        .BorderAround Weight:=xlThin
        .Borders(xlInsideHorizontal).Weight = xlThin
        .Borders(xlInsideVertical).Weight = xlThin
    End With
End With
Erase a, b
End Sub

then following code onto sheet module
Code:
Dim base As Range
Private Sub Worksheet_Activate()
    Set base = Me.Range("a3").Cells(1, 1)
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Intersect(Target, Me.Range("a3").CurrentRegion) Is Nothing Then
        base.Activate
    Else
        Set base = Target
    End If
End Sub

rgds,
jindon
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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