+ Reply to Thread
Results 1 to 3 of 3

Macro for extracting data from a long column and insert it in its own column with a header

Hybrid View

  1. #1
    Registered User
    Join Date
    11-06-2014
    Location
    Norway
    MS-Off Ver
    2007
    Posts
    2

    Macro for extracting data from a long column and insert it in its own column with a header

    This is kind of difficult to explain in words, so please check my images to understand what I want done. Imagine, instead of 31 rows I have 2000. Converting takes too much time to do manually. If there was a way to define a macro, this would help me a lot. Sometimes, the size of the various columns differ, i.e. they will not always contain 10 rows. I guess I want to unstack my data.

    I would really appreciate any help on the matter!

    Sincerely,
    Anders

    P.S. Don't mind the missing number in the first image, I just forgot to add something there and didn't bother fixing it. I'm sure you understand the point.

    Feil.jpgRett.jpg
    Last edited by ChemistryStudent; 11-06-2014 at 10:01 AM.

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Macro for extracting data from a long column and insert it in its own column with a he

    Hi Anders,
    try
    Sub ertert()
    Dim x, y(), i&, j&, k&
    x = Sheets("Sheet1").Range("A1").CurrentRegion.Value
    ReDim y(1 To UBound(x), 1 To 4): j = 1: k = 1
    With CreateObject("Scripting.Dictionary")
        .CompareMode = 1
        For i = 2 To UBound(x)
            If .Exists(x(i, 1)) Then
                j = .Item(x(i, 1)) '#row
            Else
                j = j + 1: .Item(x(i, 1)) = j
                y(j, 1) = x(i, 1)
            End If
            If .Exists(x(i, 3)) Then
                k = .Item(x(i, 3)) '#column
            Else
                k = k + 1: .Item(x(i, 3)) = k
                If k > UBound(y, 2) Then ReDim Preserve y(1 To UBound(y), 1 To k)
                y(1, k) = x(i, 3)
            End If
            y(j, k) = x(i, 2)
        Next i
    End With
    y(1, 1) = "Data"
    With Sheets("Sheet2")
        .Range("A1").CurrentRegion.ClearContents
        .Range("A1").Resize(j, k).Value = y()
        .Activate
    End With
    End Sub

  3. #3
    Registered User
    Join Date
    11-06-2014
    Location
    Norway
    MS-Off Ver
    2007
    Posts
    2

    Re: Macro for extracting data from a long column and insert it in its own column with a he

    Thank you. How do I run this code? I am not that experienced with macros. I get an error: "Subscript out of range."
    Last edited by ChemistryStudent; 11-07-2014 at 04:44 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VBA macro to add header text, insert & copy-down formula, move column
    By shootstill in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-24-2014, 05:44 AM
  2. Extracting Row and Column Header Coordinates from a value search
    By Benko in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-06-2014, 11:39 PM
  3. VBA code to make a macro repeat as long as there is data in the column
    By dkassin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-28-2012, 06:42 PM
  4. algorithm/macro for working with long data column
    By murf58 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-11-2010, 02:29 AM
  5. Replies: 1
    Last Post: 05-02-2008, 04:57 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1