+ Reply to Thread
Results 1 to 15 of 15

Name cells consecutively in first row as a loop?

  1. #1
    Registered User
    Join Date
    07-03-2013
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    10

    Name cells consecutively in first row as a loop?

    I'm implementing a questionnaire with 45 items in excel, called UPPS.

    I need a vba code to name 45 cells (45 items of the UPPS) in the first row of sheet1 as follows, Cell A1 = upps_1, B1 = upps_2, C1 = upps_3 .... upps_45.

    I' m quite sure this have an obvious solution (and simple?), but after a couple of hours searching the net and tweaking a lot of codes, no luck.....

    Any help would be appreciated!

    The closest match is this code below, but it named the cells in COLUMN A with correct names, but I need the cells to be names in a row (horisontal).

    ________________________
    Sub NameCellsColumns()

    'how to name row, not colomns?

    Dim i As Long
    Dim rng As Range
    For i = 1 To 45

    Set rng = Range("A" & i)
    rng.Name = "upps_" & i

    Next i
    End Sub

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Why do you want to name the cells individually?

    You could name the entire range and then refer to each individual cell using Cells.
    Last edited by Norie; 05-23-2014 at 02:30 PM.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    07-03-2013
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Name cells consecutively in first row as a loop?

    Yes, individually. Each cell with its own name, e.g., upps_1, upps_2, upps_3, upps_4 and so on to upps_45.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Why name the cells individually though?

    You could name the entire range,
    Please Login or Register  to view this content.
    then you can refer to the individual cells like this.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    07-03-2013
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Name cells consecutively in first row as a loop?

    Thank you for answering..though - I'm not sure whether I understood how to implement you code..?

    With individual names, I only meant that each cell should have its own name, Cell A1 should be named upps_1, B1 named upps_2 and so on..
    I' m making a userform with 45 textboxes (for each UPPS item) as data entry for this UPPS questionnaire with 45 items. The userform is a way for our hospital to ensure that entry of data is easy for everyone.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Sorry, I don't see why you need to name each cell individually.

    What are you going to do with the names?

  7. #7
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,612

    Re: Name cells consecutively in first row as a loop?

    Do you want Named Ranges or are you adding column headers to your database sheet?
    Last edited by protonLeah; 05-23-2014 at 03:14 PM.
    Ben Van Johnson

  8. #8
    Registered User
    Join Date
    07-03-2013
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Name cells consecutively in first row as a loop?

    The results from the questionnaire (UPPS among many others) are exported as txt files that we export/import to statistical programs such as SPSS and Stata used in research. I have a macro that transforms the names of the cells to variable names. Because I have also a sheet with neuropsychological tests (about 250!!), in addition to many other questionnaires, some with 10, some with 75, some with 170 items, to keep some track of the workbook (it is enormous), I need to name cells. All questionnaires and neuropsych test also have sumscores with a lot of formulas beneath (vlookup, etc).

    But I see your point, I have asked myself, do I need to name the cells. I believe it is worth the effort. It is very useful also when I make the userforms for data entry with one textbox that have a code that places the entered value in the corresponding (and named) cell. Eg. The textbox for upps_1, I label "tb_upps_1", which then should insert the entered value in the worksheet cell named "upps_1" (instead of the default parameters "textbox1" and cell "A1" in sheet1.
    Last edited by perola.rike; 05-23-2014 at 03:20 PM.

  9. #9
    Registered User
    Join Date
    07-03-2013
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Name cells consecutively in first row as a loop?

    I need to name the 45 first cells in row 1 individually (upps_1, upps_2, upps_3, upps_4 etc), not ranges.

    Quote Originally Posted by protonLeah View Post
    Do you want Named Ranges or are you adding column headers to your database sheet?

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Name cells consecutively in first row as a loop?

    In the macro that transforms the named of the cells to variable names do you have 45 variables named upps_1, upps_2, upps_3 etc?

    If you do wouldn't it be an idea to use an array to store all 45 values?

    For example if you had named the 45 cells with a single name, eg UPPS you could put the values into an array like this.c
    Please Login or Register  to view this content.
    You can then easily manipulate the array, refer to individual items within it, output it's content to a text file/other worksheet etc.

  11. #11
    Registered User
    Join Date
    07-03-2013
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Name cells consecutively in first row as a loop?

    The macro that retrieves the names of the cells and transforms them to variable names - yes - they are named individually as you proposed (named upps_1, upps_2, upps_3 etc? though I'm still working on the UPPS...).

    If I make an array (with which I'm not that familiar with, but we have many array names), rather than individual naming, I believe it will be harder for our macros to retrieve the "names" of the cells and make variable names out of it in a simple way (?).

    The reason for the extensive naming (individual, not arrays) is also the following: We have many users of the sheet. All users use different tests/questionnaires. In the individual databases each user creates, the variable names are automatically generated from the named cells (which is different than the heading of the cells). E.g. the name for the cell containing the value for UPPS item 1 is "upps_1" (upps item one), but the "heading is UPPS 1. Named cells have no spaces which in required in statistcal programs (SPSS, Stata). UPPS 1 is unusable as a variable name...

    I'm not sure if I made it clearer...?
    Last edited by perola.rike; 05-23-2014 at 03:41 PM.

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Name cells consecutively in first row as a loop?

    If you used an array you wouldn't need 45 variables.

  13. #13
    Registered User
    Join Date
    07-03-2013
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Name cells consecutively in first row as a loop?

    I need to update on the advantages of arrays....! hopefully I may be useful?

    Quote Originally Posted by Norie View Post
    If you used an array you wouldn't need 45 variables.

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Name cells consecutively in first row as a loop?

    If you really want to name the individual cells you can use this.
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    07-03-2013
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Name cells consecutively in first row as a loop?

    The code worked fine. Though, you gave me some array-inputs that I will consider strongly in the further developments, thanks.

+ 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] Need formula to sum range of cells consecutively across a row not duplicating used cells
    By angie18a in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-28-2013, 03:46 PM
  2. Replies: 14
    Last Post: 05-10-2013, 03:27 PM
  3. Formula for counting cells consecutively
    By d1scopants in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-28-2012, 09:11 AM
  4. how do i average a series of cells consecutively
    By ajoros in forum Excel General
    Replies: 3
    Last Post: 06-16-2009, 04:04 PM
  5. [SOLVED] Copied Cells do not run Series consecutively...
    By Gucci in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-01-2006, 02:10 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