+ Reply to Thread
Results 1 to 8 of 8

Identifying Column Number from Reference

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-03-2013
    Location
    Newport, England
    MS-Off Ver
    Excel 2013 Windows
    Posts
    144

    Identifying Column Number from Reference

    Hi, I'm not very good at this and am having real trouble. Been messing around with some Find functions & googling for help. My problem is very simple I believe (but this is not my forte!)

    I have attached an example sheet to illustrate what I mean.

    CODE HELP.xlsx

    Basically all I want to do is use cell B2 as a reference, to lookup the matching column to that ref in the array D:R. Then copy A2:A20 and paste in the second row of the appropriate column.

    :d

    TIA for any help!

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Identifying Column Number from Reference

    Sub Copy_to_Matched_Column()
        Dim Found As Range
        Set Found = Range("D1:R1").Find(What:=Range("B1").Value, _
                                        LookIn:=xlValues, _
                                        LookAt:=xlWhole, _
                                        SearchOrder:=xlByColumns, _
                                        SearchDirection:=xlNext, _
                                        MatchCase:=False)
        If Not Found Is Nothing Then
            Range("A2:A20").Copy Destination:=Found.Offset(1)
        Else
            MsgBox "No column match found. ", , "No Column Match"
        End If
    End Sub

  3. #3
    Forum Contributor
    Join Date
    01-03-2013
    Location
    Newport, England
    MS-Off Ver
    Excel 2013 Windows
    Posts
    144

    Re: Identifying Column Number from Reference

    Thank you so much! I see this works great in my example, and in my actual working sheet too. The only problem is that I just need it to paste the values. I've been trying to figure out how I could add in:

    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    But to no avail. Thanks for your help!

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Identifying Column Number from Reference

    Replace this...
    Range("A2:A20").Copy Destination:=Found.Offset(1)

    With this...
    Found.Offset(1).Resize(19).Value = Range("A2:A20").Value

  5. #5
    Forum Contributor
    Join Date
    01-03-2013
    Location
    Newport, England
    MS-Off Ver
    Excel 2013 Windows
    Posts
    144

    Re: Identifying Column Number from Reference

    That's amazing! Thank you so much! The only (very small) issue I have is that the range I'm copying has 14997 rows (A3:A15000). So I think I did the correct thing by setting the Resize(14997)? But obviously this takes a bit of time to process.

    About 500 per time produce data. I have looked into using 'Special Cells' but am not entirely sure if they are appropriate, or how to implement them. Is this or any other way worth investigating in order to speed up the macro? (I already turn off screen updating and calculation before running it)

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Identifying Column Number from Reference

    Quote Originally Posted by cloudwalking View Post
    That's amazing! Thank you so much! The only (very small) issue I have is that the range I'm copying has 14997 rows (A3:A15000). So I think I did the correct thing by setting the Resize(14997)? But obviously this takes a bit of time to process.

    About 500 per time produce data. I have looked into using 'Special Cells' but am not entirely sure if they are appropriate, or how to implement them. Is this or any other way worth investigating in order to speed up the macro? (I already turn off screen updating and calculation before running it)
    You're welcome.

    A3:A15000 = 14998 rows

    This should take practically no time (sub-second) to copy the values.
    Found.Offset(1).Resize(14998).Value = Range("A3:A15000").Value


    I didn't understand what this means;"About 500 per time produce data. "

  7. #7
    Forum Contributor
    Join Date
    01-03-2013
    Location
    Newport, England
    MS-Off Ver
    Excel 2013 Windows
    Posts
    144

    Re: Identifying Column Number from Reference

    Just means that each time I'm only copying over around 500 filled cells (within A3:A15000). The rest are blank (actually they have LOOKUP formula, but are returning blanks). I have also devised a method with a check box to turn Calculation off on this sheet alone. There's no conditional formatting wither. But it still seems to take a while to paste the data. :-(

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Identifying Column Number from Reference

    The copy value method is not the bottleneck. It's hard to say what is without seeing your actual workbook. If you copy the 15000 rows in your example workbook, you can see how fast it is.

    The code below suspends Calculations, Events, and Screenupdating while copying. This may improve the speed.

    Sub Copy_to_Matched_Column()
        
        Dim Found As Range, lCalcState As Long
        
        Set Found = Range("D1:R1").Find(What:=Range("B1").Value, _
                                        LookIn:=xlValues, _
                                        LookAt:=xlWhole, _
                                        SearchOrder:=xlByColumns, _
                                        SearchDirection:=xlNext, _
                                        MatchCase:=False)
        If Not Found Is Nothing Then
            With Application
                lCalcState = .Calculation
                .Calculation = xlCalculationManual
                .EnableEvents = False
                .ScreenUpdating = False
                    Found.Offset(1).Resize(14998).Value = Range("A3:A15000").Value
                .Calculation = lCalcState
                .EnableEvents = True
                .ScreenUpdating = True
            End With
        Else
            MsgBox "No column match found. ", , "No Column Match"
        End If
        
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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