+ Reply to Thread
Results 1 to 14 of 14

Finding a column via header search and then adding everything in it into an array.

  1. #1
    Forum Contributor
    Join Date
    02-13-2017
    Location
    Durham
    MS-Off Ver
    2013
    Posts
    126

    Finding a column via header search and then adding everything in it into an array.

    Hi All,

    I'm currently adding various columns/cells into arrays and then transposing them into a separate workbook (An updated template format). The code is going to be generic so I can use it on all old templates to import to the new. This issue I have is I have a column that's dynamic as many people didn't follow the original template and created their own columns.

    Because of this I cannot simple state - x = Range("A2:A50").value etc as the data I need may not be in column B in a template etc.

    Not a problem in regards to finding the column as the title never changes meaning I can search for that column as shown below:

    Please Login or Register  to view this content.
    The above code finds my title (Yay) and even copies the column into an array (yay) however it also copies the title! I've tried to create a .offset(1) at the end of the Set SelRange line but it comes up with an error. It lets me offset a row strangely....

    Any ideas on how I can add that entire row without the title into an array? If it helps, the title is always in 'X1,2, 3 etc'

    Thanks in advance
    Last edited by Scoobster_doo; 02-14-2017 at 08:16 AM.

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Finding a column via header search and then adding everything in it into an array.

    Welcome to the forum.
    Please read the forum rules first here (Rules No. 3 in this case) : http://www.excelforum.com/forum-rule...rum-rules.html

    Please Login or Register  to view this content.
    Attached Files Attached Files
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Forum Contributor
    Join Date
    02-13-2017
    Location
    Durham
    MS-Off Ver
    2013
    Posts
    126

    Re: Finding a column via header search and then adding everything in it into an array.

    HI Karedog,

    Apologies, I've edited my post to correspond with the rules

    Thanks for the reply, I shall test it tomorrow and hopefully it sorts my annoying issue

  4. #4
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Finding a column via header search and then adding everything in it into an array.

    Hi Scoobster_doo,

    Thanks for adding the code tags. Please tell me the result of the code.

    Regards

  5. #5
    Valued Forum Contributor
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,257

    Re: Finding a column via header search and then adding everything in it into an array.

    Sir karedog what the mean if err.number <> 0 then exit sub
    What mean err. Number

  6. #6
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Finding a column via header search and then adding everything in it into an array.

    Sir daboho,

    When you enable an error-handling routine using "On Error ...." statement, VBA will start to reporting to "Err object" for every statements after "On Error ...." line.
    There are several properties of Err object, such as .Number property and .Description, that can tell us what error is produced.
    A page that lists these errors is :
    http://onlinelibrary.wiley.com/doi/1...57616.app3/pdf

    "Err.Number = 0" means no error is occurred, anything else means an error is occurred.

    An example :
    Please Login or Register  to view this content.
    As for the "If Err.Number <> 0 Then Exit Sub" statement on post #2, using "Application.WorksheetFunction.Match()" will not always return a number, it will return an error if nothing is matched (found), so, if this is happenned then Exit Sub immediately.

  7. #7
    Valued Forum Contributor
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,257

    Re: Finding a column via header search and then adding everything in it into an array.

    thankyou very match sir karedog
    Please Login or Register  to view this content.

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Finding a column via header search and then adding everything in it into an array.

    Instead of the On Error construction you could also use.

    Please Login or Register  to view this content.
    Also note that it's only when using the Application object your code doesn't go into Debug when no match is found whereas using the WorksheetFunction the code errors out.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  9. #9
    Forum Contributor
    Join Date
    02-13-2017
    Location
    Durham
    MS-Off Ver
    2013
    Posts
    126

    Re: Finding a column via header search and then adding everything in it into an array.

    Hi Karedog,

    Thanks for that, the code works perfectly fine on it's own (With a rejig of my own). However would there be a way to incorporate this into an already made sub rather than its own?

    Again, Thanks for your help!

  10. #10
    Forum Contributor
    Join Date
    02-13-2017
    Location
    Durham
    MS-Off Ver
    2013
    Posts
    126

    Re: Finding a column via header search and then adding everything in it into an array.

    Hi Again,

    It doesn't matter I was able to figure it out Thanks again for your help with it all

    Now I just have to figure out how to remove the #N/A when I pull form the array - But that's a different story!

  11. #11
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Finding a column via header search and then adding everything in it into an array.

    @daboho : You are wellcome sir.

    @Scoobster_doo :
    You are welcome. Do you want to replace the #N/A with blank, or the data must be shifted up when #N/A occurred ?

  12. #12
    Forum Contributor
    Join Date
    02-13-2017
    Location
    Durham
    MS-Off Ver
    2013
    Posts
    126

    Re: Finding a column via header search and then adding everything in it into an array.

    Hi Karedog

    I want to replace the #N/A with blank cells.

    However I'm going to run my arrays through various validations before it puts anything into the cells. Hopefully that will remove the #N/A

  13. #13
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Finding a column via header search and then adding everything in it into an array.

    You can check each member of array whether it is an error, using IsError() function and replace it with vbNullString.
    After that, you can continue adding your validating code.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  14. #14
    Forum Contributor
    Join Date
    02-13-2017
    Location
    Durham
    MS-Off Ver
    2013
    Posts
    126

    Re: Finding a column via header search and then adding everything in it into an array.

    Forgot to say, Thanks for your help Karedog +rep!

+ 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. Help in Copy Header Colum data from one workbook to another
    By Bud Wilkinson in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-21-2015, 02:44 PM
  2. Finding all Text and adding a inputbox to all search
    By Excelnoub in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-20-2014, 02:21 PM
  3. Replies: 0
    Last Post: 08-29-2013, 10:07 PM
  4. [SOLVED] Return date last attended (colum header) from multiple worksheets
    By M4rk in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-01-2012, 01:21 PM
  5. Finding a value in an array and display header
    By ryfy in forum Excel General
    Replies: 0
    Last Post: 07-27-2011, 11:41 AM
  6. adding a date search to an array
    By stevie01 in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 07-18-2009, 04:46 AM
  7. Help finding colum with VBA
    By Jennifer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-03-2005, 08:06 PM

Tags for this Thread

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