Excel adding range of cells

Excel for Microsoft 365 Excel 2021 Excel 2019 Excel 2016 Excel 2013 Excel 2010 Excel 2007 Excel Starter 2010 More…Less

You can quickly create a named range by using a selection of cells in the worksheet.

Note: Named ranges that are created from selecting cells have a workbook-level scope.

  1. Select the range you want to name, including the row or column labels.

  2. Click Formulas > Create from Selection.

  3. In the Create Names from Selection dialog box, select the checkbox (es) depending on the location of your row/column header. If you have only a header row at the top of the table, then just select Top row. Suppose you have a top row and left column header, then select Top row and Left column options, and so on.

  4. Click OK.

Need more help?

You can always ask an expert in the Excel Tech Community or get support in the Answers community.

Need more help?

Excel concatenate() is seriously crippled, it can add 2 or more strings together, as long as they are supplied as separate parameters. This means, when you have a range of cells with text which you want to add up to create a large text, you need to write an ugly looking biggish concatenate() or use ‘&’ operator over and again.

I felt bored enough the other day to write a better concatenate(), one that can accept a range as input and output one text with all the contents of the input range. What more you can use this to delimit the input range with your own favorite character.

For example, if each of the 7 cells in a1:a7 have “a”, “b”, “c”, “d”, “e”, “f”, “g”, if you want to add all of them up using concatenate you would have to write concatenate(a1,a2,a3,a4,a5,a6,a7) which can be painful if you are planning to do this over a large range or something.

Instead, you can use concat(a1:a7) by installing the UDF (User defined function) I have written. Its nothing miraculous or anything, it just does the dirty job of going through the range for you. If you want to delimit the input range with a comma just use concat(a1:a7,",") to get the out of a,b,c,d,e,f,g Just download the concat() UDF excel add-in and double click on it to install it. If you are little weary of installing UDFs / Macros from third parties, copy past the below excel code in a new sheet’s VB editor and save the sheet as an excel addin (.xla extension)

Added on Aug 26, 2008: I have updated the code, copy it again if you have the old one


Function concat(useThis As Range, Optional delim As String) As String
' this function will concatenate a range of cells and return one string
' useful when you have a rather large range of cells that you need to add up
Dim retVal, dlm As String
retVal = ""
If delim = Null Then
dlm = ""
Else
dlm = delim
End If
For Each cell In useThis
if cstr(cell.value)<>"" and cstr(cell.value)<>" " then
retVal = retVal & cstr(cell.Value) & dlm
end if
Next
If dlm <> "" Then
retVal = Left(retVal, Len(retVal) - Len(dlm))
End If
concat = retVal
End Function

Did you find this useful? Are you looking for some other excel UDFs as well, drop a comment, I am a busy coffee drinker, but between the sips I can whip out ugly looking but functional vb code 😛


Share this tip with your colleagues

Excel and Power BI tips - Chandoo.org Newsletter

Get FREE Excel + Power BI Tips

Simple, fun and useful emails, once per week.

Learn & be awesome.




  • 144 Comments




  • Ask a question or say something…




  • Tagged under

    add-in, concat, Excel Tips, how to, microsoft, Microsoft Excel Formulas, technology, tips, tricks, udf, VBA




  • Category:

    hacks, Learn Excel, technology

Welcome to Chandoo.org

Thank you so much for visiting. My aim is to make you awesome in Excel & Power BI. I do this by sharing videos, tips, examples and downloads on this website. There are more than 1,000 pages with all things Excel, Power BI, Dashboards & VBA here. Go ahead and spend few minutes to be AWESOME.

Read my story • FREE Excel tips book

Advanced Excel & Dashboards training - Excel School is here

Excel School made me great at work.

5/5

Excel formula list - 100+ examples and howto guide for you

From simple to complex, there is a formula for every occasion. Check out the list now.

Calendars, invoices, trackers and much more. All free, fun and fantastic.

Advanced Pivot Table tricks

Power Query, Data model, DAX, Filters, Slicers, Conditional formats and beautiful charts. It’s all here.

Still on fence about Power BI? In this getting started guide, learn what is Power BI, how to get it and how to create your first report from scratch.

A smart technique to simplify lengthy nested IF formulas in Excel

  • Excel for beginners
  • Advanced Excel Skills
  • Excel Dashboards
  • Complete guide to Pivot Tables
  • Top 10 Excel Formulas
  • Excel Shortcuts
  • #Awesome Budget vs. Actual Chart
  • 40+ VBA Examples

Related Tips

144 Responses to “How to add a range of cells in excel – concat()”

  1. You are a god in Excel!

    I bow down to you o master of Excel…

    • Dazza says:

      You are an absolute legend!
      This saved me about 8 hours of ‘clicking’ not to mention the rsi!!

      • Doug Lampert says:

        Thank you, I can’t imagine how MS didn’t enable this in their concatenate function. What in the world is the point to a function that’s less useful than the & symbol.

        • Kim Wennerberg says:

          MS has added this function to Office 2016! It’s called TEXTJOIN.
          Thank you Chandoo for covering this while MS catches up! (And, of course, users of older versions of Excel still need you on this.)

  2. But… how do I use it?

    The formula does not appear when i type in ‘=conc…’ in a cell.

  3. @Hypnos .. thanks alot man 🙂
    I think you need to save the downloaded xla file in your excel add-in folder (just select save as and use the excel add-in as the file type, the folder will be shown in the save dialog automatically), let me know if this doesnt work.

  4. Duh!

    It didn’t work, that is why I wrote… give me some credit dude 🙂 I used to be in your ITCOM.

  5. hmm.. thats tricky, I read that it works that way, it actually did work that way for me, by saving the xla file in the addins folder, i could see and use the formulas. Btw, the formula may not appear when you type =concat… in the cell, but it works nevertheless. Anyways, can you try pasting the code in a module in your sheet and save that sheet as an addin instead… meanwhile I will investigate why this wouldn’t work…

  6. captsri says:

    that was a great lead to me, and thansk a ton. need you help in this problem. in eh above example if a3 has ‘c’ but the actual field length should be 8 how do i make use of the same program to create a fixed length text so that i can export it as *.edi file.
    In essence i want to create form a excel sheet an exi file withfixed text format having an option to enter headers

  7. @capstri .. thanks for the comments..

    let me see if I can help you…

    In the above code you can change the for loop to something like this to get the desired effect.

    For Each cell In useThis
    retVal = retVal + cell.Value + rept(» «,8-len(cell.Value))
    Next

    you can replace the «8» with whatever fixed lenght you have in mind. Also, I have not tested the above code, you may have to replace the rept() with something else if it throws an error. Let me know so that I will help you if I can… 🙂

  8. […] Concatenate a bunch of cells using simple formula, Generate tag clouds in excel using vba, Master your IFs and BUTs Tags: Analytics, count, excel, […]

  9. colebro says:

    When I add/save the xla file to my addins directory ‘C:Documents and SettingscolebroApplication DataMicrosoftAddIns’, i double click on the file to install it. When I enter the command =concat(a4:d4) I get the invalid value (#VALUE!). The ‘Excel-Udf-Concat’ addin is checked iin my add-ins available list. So, I copied your excel code and saved it as an xla file in the add-ins directory, made sure it was ‘checked’ and still nothing. What am I doing wrong? This add-in will save me a lot of time. — Also, what happens when I send my spreadsheet to another user that does not have your add-in, does the concat still work?

  10. @Colebro: Hey.. that is strange, I remember Amit having similar problem. Did you try copying the code and saving it in to your own excel file in a new module? If you do that, then even if the excel file is sent to another person, the function still works… but if you save it as an add-in then the other computer should also have the addin installed.

    Let me know if copying the code helps you… otherwise I will investigate in to this further….

  11. @colebro: Looks like this is not an error with the how you add the add-in but with the add-in it self…

    My udf would work fine as long as the input range has strings (text) in them, but when the range has numbers in them the udf would throw #value error. Here is a fix…

    replace this part of the code with:

    For Each cell In useThis
    retVal = retVal + cell.Value + dlm
    Next

    For Each cell In useThis
    retVal = retVal + cstr(cell.Value) + dlm
    Next

    essentially I am force converting each cell’s value to string before creating the concatenated value… this seems to work when you have numbers / dates etc in the cells. Let me know if this helps you…

  12. colebro says:

    I must be doing something real stupid because I can not get this to work. I removed the Add-in, changed the statement in the code above and re-added the xla to the add-in. I put a value (either numeric or alpha) and I get an invalid name (#NAME?)
    a b c d e
    =concat(a1:a6)
    #NAME?

  13. SheilaC says:

    BEAUTIFUL — I can’t say anything more. THANK YOU SO MUCH! Now, if the function could just skip blank cells…in other words, if in concat(b1:b8,»; «) there were 3 blank cells, it wouldn’t return something like this

    text 1; text 2; text 3; ; ; text 6; ; text 8

    but instead would return only

    text 1; text 2; text 3; text 6; text 8

    I would fall over and get rugburn on my nose. Even just what you wrote is a huge help — joining large cells, and getting real sick of having to join subsets, then aggregate the subsets in another «overall» concat statement.

    🙂 Bring on the coffee dude…this was HELPFUL. So simple, but practical. Now WTH doesn’t Excel just make this standard code???????????

    LOL — might be too practical for us, right???

  14. @colebro: Oops, I missed to respond to this comment, let me see if I can fix this for you…


    Function concat(useThis As Range, Optional delim As String) As String
    ' this function will concatenate a range of cells and return one string
    ' useful when you have a rather large range of cells that you need to add up
    Dim retVal, dlm As String
    retVal = ""
    If delim = Null Then
    dlm = ""
    Else
    dlm = delim
    End If
    For Each cell In useThis
    retVal = retVal + cstr(cell.Value) + dlm
    Next
    If dlm "" Then
    retVal = Left(retVal, Len(retVal) - Len(dlm))
    End If
    concat = retVal
    End Function

    this should work… if not try saving the function and your spreadsheet… often excel takes sometime to figure out that the udf is now present. let me know if this doesnt help…

  15. @SheilaC: Welcome to PHD, thanks for the awesome comments… I am happy you found this really useful…

    I have added an if condition in the loop to only add the cell if the contents are not blank… hope this helps you in getting that rugburn 😀


    Function concat(useThis As Range, Optional delim As String) As String
    ' this function will concatenate a range of cells and return one string
    ' useful when you have a rather large range of cells that you need to add up
    Dim retVal, dlm As String
    retVal = ""
    If delim = Null Then
    dlm = ""
    Else
    dlm = delim
    End If
    For Each cell In useThis
    if cstr(cell.value)<>"" and cstr(cell.value)<>" " then
    retVal = retVal + cstr(cell.Value) + dlm
    end if
    Next
    If dlm "" Then
    retVal = Left(retVal, Len(retVal) - Len(dlm))
    End If
    concat = retVal
    End Function

    let me know if this doesnt help…

  16. @SheilaC: you may want to replace the dirty quotes in the code with proper double quotes…

  17. SheilaC says:

    …Sheila would like to reply to Chandoo — unfortunately she is in the hospital with grade 1 rugburn…in fact, they are extracting rug from her nostrils as we speak.

    OMG Chandoo — if you were here — I WOULD HUG YOU SO BAD YOU’D SQUEEK! 🙂 Major help like you have no idea. 🙂

    Sheila

  18. MikeP says:

    Other quick edit

    change line:

    retVal + cstr(cell.Value) + dlm

    to:

    retVal & cstr(cell.Value) & dlm

    Otherwise Excel sends an error when cell.value is a number

  19. @SheilaC : Hope you are alright :P, I am happy this helped you.

    @MikeP : thanks for pointing it out, I have changed the code to include your suggestion. 🙂

  20. Navneet says:

    it was a great help. thanks!!

  21. […] on text processing using excel: Concat() UDF for adding several cells, Initials from names using excel formulas Categories : Excel Tips | ideas Tagged with: […]

  22. EStout says:

    Very interesting but my dilema is I have a formula in each cell that says VLOOKUP((TEXT(O3,»mm/dd/yy»)&A9),Monthly_Data,2) but when the string is not found (which is very likely the majoriity of the time) the result is #VALUE. Can I use an IF statement that says if the string is not in range enter a 0?

  23. @Navneet … Thanks for the comments 🙂

    @EStout … hmm, I guess you can change the UDF to include a condition to check if the cell has an error then skip it.

    here is how:


    if iserror(cell.value) = false and cstr(cell.value)“” and cstr(cell.value)” ” then
    retVal = retVal & cstr(cell.Value) & dlm
    end if

    Another way to get this is to modify your vlookup to include some error handling, like if(iserror(VLOOKUP((TEXT(O3,”mm/dd/yy”)&A9),Monthly_Data,2)),0,VLOOKUP((TEXT(O3,”mm/dd/yy”)&A9),Monthly_Data,2)).

    If you are using excel 2007 you can try is the iferror function.

  24. EStout says:

    Thank you so much!!! It worked.

  25. […] tip: If you are using formulas to create content in a cell by combining various text values and you want to introduce line breaks at certain points … For eg. you are creating an address […]

  26. […] the Concat VBA Function I have written can be used to concatenate a range of cells (along with a custom delimiter), it […]

    • SK says:

      The formula that you have shared is for cells that are in a continuous range. How can we edit this for selection of specific set of cells where a filter has been applied ? or the cells that are not in a continuous range?

  27. morgan says:

    you have no idea how much time this saved me!!!! thank you so much! i merged 1395 cells into one!

  28. cybpsych says:

    hi chandoo,

    a feedback on this UDF in XL2007.

    I’m facing problems exactly as colebro’s (August 9, 2008).

    getting #VALUE! when the range contains numbers (e.g. A, B, 1, D, E)

    Is this UDF XL2007-compatible?

  29. cybpsych says:

    ok, please ignore my previous post …

    the codes work when embedded in the target sheet.

    if i dump the XLA into Addins folder, it won’t work (properly).

    also, if the cells have errors (#value!, #div/0!), it gives me «Error 2007»

  30. cybpsych says:

    ok, please ignore the previous post. ..

    i got the codes work when embedding it in target sheet.

    if i dump the XLA into Addins folder, it won’t work (properly)

    Also, if the cells contain errors (#value!, #div/0!), concatenated cell shows as «Error 2007»

  31. @Cybpsych: cool… as you can see, the udf is fairly straightforward and simple. I could have made it complicated, but I thought of keeping it simple to handle text concatenation without worrying about all exceptions.

    @Morgan: You are welcome. I am happy you found this useful

  32. Daniel Strand says:

    Help I cannot get the last code to work. I get an error from the following code:

    if cstr(cell.value)“” and cstr(cell.value)” ” then

  33. @Daniel: you need to insert = between cstr(cell.value) and «». Same for the next one too. Let me know if you still face a problem. I will try to upload code in a downloadable format.

  34. SheilaC says:

    Ok, I am at my wits end. I have been given a template for my workbooks that I have to use. They include embedded images for headers/footers.

    Ex-post-facto development of a complex document, I have to figure out how to incorporate these two images as the left and right headers. However? I use VBA code to generate all my header/footers prior to printing. The workbook has like 40-something sheets.

    I can put the image directly into the left header, however it points to a file location for the image on my hard drive. This will cause the logo not to show up when the spreadsheet is downloaded off of the storage location it is placed into. Which is a violation of an official document. rrrrrrrrr!

    I tried to link my image to a cell, but there is not a feature to do that in Excel. I can size the image with a row, but it won’t show the image when I use code to refer to that cell — however, text in the cell does show up so it is including what Excel sees «in» the cell.

    I even tried (grasping here) using a comment with an image background.

    Repeat rows does not «see» the picture either.

    What can I do????

  35. @SheilaC: which version of excel are you using? In excel 2007, when I have an image in some rows and define those rows to be repeated at top while printing (from ribbon > page layout > print titles) they are properly repeated for each of the pages on the output.

    Since you say you are already using VBA to handle some stuff, may be you can think of an out of box solution like transporting excel output to word templates with images already in and then sending them to printers… That is if your version of excel doesnt support images in header rows…

  36. SheilaC says:

    I am using Office 2003. GOD I HATE 2007’s ribbons! Why cain’t we just keep our dang buttons Mama? Like good little mice we’ve learned how to push the lever for our pellet, and now — we have to use something that hogs 1/3 of our screen and requires the «Help» function constantly to figure out how to do stuff!!!

    NEways, I can’t figure it out. It is part of our markings requirements to have these images embedded. So — I’m just leaving off the Left & Right header generation statements in VBA (before print routine), and hard putting them in there using the picture icon in the Header & Footer menu.

    But I know, if anyone could’ve solved this — it’s you. You are a VBA GOD!!! 🙂 LOL. My nose is still flat from my last helping here.

    PS> How do I open a new thread?

    • @SheilaC: you said, you have written some VBA to insert images in to the footer. May be you can change code to insert few rows on the top, insert images there and make the rows repeated. That way images need not be copied along with the file.

      PS: Unfortunately we dont have any page where you can ask your questions. The usual process is locate the posts that talk about the topic you need help on, just post a comment on the latest post and you should get the response.

  37. jUGAD says:

    Can you please tell me how to add a description to your above UDF like the one that gets displayed in default functions available in excel 2007? Is it also possible to write a help topic on it?

    This blog post (along with its precious comments and the replies) contains the most comprehensive coverage of how to concatenate a range in excel (among all the posts I searched on this topic). Thus, I want to further add some nerdy facts to it. Using your above function I created cell references to put in the two default cell merging options in the excel i.e. (a) CONCATENATE function and the (b) using the ‘ampersand‘ sign ‘&‘. The column A had the data to be merged and column B had corresponding cell references of column A i.e. value in cell B1 was ‘A1’, B2 was ‘A2’ and likewise. I used the Chandoo’s VBA, i.e. =concat(B1:B9,»&»). Using Paste special—>Values and adding an ‘equal to‘ sign i.e. ‘=’ I obtained a formula that would work in excel by default so that there is no need of availability of VBA or the add-in if the file is used on some other systems. Following are some facts that I found,

    A formula can have a maximum of 8,192 characters

    You can give a maximum of 300 cell references in a single formula using the default «&» (ampersand) sign

    You can give a maximum of 30 cell references in a single formula using the default «Concatenate» function (though the function’s help states that it can take 255 cell references, my trial didn’t go beyond 30)

    A single cell can hold a maximum of 32,767 characters. Thus, you can re-Concatenate the above results, which will follow the above conditions regarding cell references, into a single cell till you hit the maximum limit of 32,767.

    P. P.S.: I don’t know anything about VBA

    P.S.: I use MS Excel 2007, you can download my excel worksheet here

    P.P.S.: Please do not forget to answer my 2 ques. written at the beginning of this comment 😉

  38. jUGAD says:

    Hi Chandoo,

    As I had already written in my previous comment that I do not know anything about VBA, further, I had already seen your suggested links before asking from you as I could not find them fruitful even after lots of trials from whatever I understood of them.

    It would be great if you could show the complete code here so that I can just copy and paste it 😉

  39. David says:

    This is AWESOME!! I love this add-in. Works great and saves a ton of work pasting in Word, adding characters, etc.

    Thank you for doing this!

  40. @David.. you are welcome 🙂

  41. PJ says:

    This is great!! Have you figured out how to comma or semicolon-delimit the resulting string? That would also be really helpful!

  42. @PJ: The function has an option delimiter parameter, you can pass «,» or «;» to it and it will delimit the values with that.

  43. PJ says:

    Thanks, that worked great! You just saved me a ton of time!

  44. Ed says:

    Many, many thanks. Saved me a huge effort too!

  45. Wade says:

    Also, if you have a range that has nothing in it, you might find the following useful:

    Change
    If dlm «» Then

    To
    If dlm «» And retVal «» Then

    That way when there is nothing in the range and you have a delimiter specified you don’t get an error when the code tries to subtract the delimiter length from and empty string.

    Wade

  46. Great add-in, but it would be even greater if you could add another condition to whas Sheila added. So here goes:
    I want to concatenate text in range A1:A100, only if the respective value from range B1:B100 equals to letter «M».
    So A1=»Sheila», A2=»Chandoo», A3=»Amer», A4=»David», A5=»PJ», A6=»Ed», A7=»Leah», etc.
    B1=»F», B2=»M», B3=»M», B4=»M», B5=»M», B6=»M», B7=»F», etc.
    C1=»F», C2=»M»
    I want D1 to be all values from column A where corresponding value from column B equals whatever is in C1 («Chandoo, Amer, David, PJ, Ed»)
    I want D2 to be all values from column B where corresponding value from column B equals to whatever is in C2 («Sheila», «Leah»).
    Also, this should work without the list being ordered by any of the columns.
    I guess the function should take 3 parameters (rangeToConcatenate, rangeToTestCondition, ValueToTestConditionAgainst)

    • Mairag says:

      Hi,
      is there a way to expand the solution to Amer’s question so that if there is also an «Age» column, it will concatenate only the M’s between the ages of 40&49 as an example?

      So A1=”Sheila”, A2=”Chandoo”, A3=”Amer”, A4=”David”, A5=”PJ”, A6=”Ed”, A7=”Leah”, etc.
      B1=”F”, B2=”M”, B3=”M”, B4=”M”, B5=”M”, B6=”M”, B7=”F”, etc.
      C1=»41″, C2=»46″, C3=»37″, C4=»59″, C5=»42″, C6=»23″, C7=»35″
      D1=”F”, C2=”M”
      E1=»40″, E2=»49″

      I want F1 to be all values from column A where corresponding value from column B equals to whatever is in C1 and where corresponding value from column C is greater than or equal to E1 and less than or equal to E2 (“Chandoo”, “PJ”).

      Is this possible?

          • @Mairag
            and I answered it 5 minutes after you posted it

      • @Mairag

        you can sue the following code

        Function ConcatIf(Src As Range, ChkRng1 As Range, myVal1 As String, Optional ChkRng2 As Range, Optional myVal2 As String, Optional Sep As String) As String
        Dim c As Range
        Dim retVal As String
        Dim i As Integer
        retVal = ""
        i = 1
        For Each c In ChkRng1
        Debug.Print i; c, myVal1, CStr(ChkRng2(i)), myVal2
        If c = myVal1 And ChkRng2(i) = myVal2 Then
        If WorksheetFunction.IsNumber(Src(i)) Then
        retVal = retVal + Trim(Str(Src(i))) + Sep
        Else
        retVal = retVal + Src(i) + Sep
        End If
        End If
        i = i + 1
        Next
        ConcatIf = Left(retVal, Len(retVal) - Len(Sep))
        End Function

        In use it will be
        =ConcatIf(Rangge to Concatenate, Rng1, Val1, Rng2, Val2, Separator)
        =ConcatIf(A1:A9,B1:B9,»h»,C1:C9,»i»,», «)
        Will concatenate A!:A9 where B1:B9=h and C1:C9=i with a , and a space as a separator

        =ConcatIf(A1:A9,B1:B9,»h»,,,», «)
        Will concatenate A1:A9 where B1:B9=h with a space as a separator

        =ConcatIf(A1:A9, B1:B9, «h»)
        Will concatenate A1:A9, where B1:B9=h with no seperator

  47. @Amer… you can use a simple IF along with helper column so that you show the value only if the testCondition is «M». Then pass the new helper column range to concat.

    Of course, you can write another UDF, but such a formula becomes less generic..

  48. Milco says:

    Thanks a lot for your formula. It flawlessly concatenated a range of 100 fields yesterday!

    — Milco

  49. i says:

    @Amer
    You could also use array formulas rather than helper columns. Just change the range’s type to variant so that it will accept an array as input.

    My application was to augment the rows of a pivottable (an inventory) with a list of textual codes (identifying the applications for which the item is used) taken from the rows in which the item’s name appears in the source table.

    So I use:

    {=IF(A5″»,concat(IF(A5=’Lesson Items’!$C$2:$C$250,’Lesson Items’!$A$2:$A$250,»»),» «),»»)}

    where ‘Items’ is the sheet with the items listed by application, with item names in C and application names in A; and where A5 holds the item name in the current row of the pivottable. I was really frustrated by the lack of ‘concatenate’ as an aggregation function in pivottables, but this method seems to work pretty well.

    For those unfamiliar, remember that you don’t actually type the curly brackets «{» and «}»; you type in the formula and hit «ctrl-shift-enter» rather than just «enter», and Excel processes it as an array formula, marking it as such with the brackets.

    Here’s the version of the function I used:

    Function Concat( _
    myRange As Variant, _
    Optional myDelim As String = «» _
    ) As String

    Dim myRetv As String

    For Each v In myRange
    If v «» Then
    myRetv = myRetv & v & myDelim
    End If
    Next v

    If myRetv «» And myDelim «» Then
    myRetv = Left(myRetv, Len(myRetv) — Len(myDelim))
    End If

    Concat = myRetv
    End Function

  50. i says:

    Sorry about the formatting of my last post. I didn’t realize whitespace wouldn’t be preserved.

  51. Greg says:

    Hey Chandoo/PHD! I just wanted to comment that this helped me out at work a TON! I spread the knowledge and it’s helped a few others as well.

    One question — is there any way to get the current UDF to IGNORE text values?

    EXAMPLE:
    =concat(A23:A420,»,»)

    The intent here is to simply grab each number in all of the cells — except in that range, there are text values too. I created merged cells as a sort of «header» breaking up sections of the sheet. So, I’m getting back…

    Week1,Task ID,982,989,1010,2221,Week2,Task ID,2213,3222,Week3,

    I want to IGNORE any text values and just have it grab the numbers so I get something like:
    982,989,1010,2221,2213,3222

    Any help is appreciated! AND! I’ve added your site to my iGoogle. This is awesome 😀

  52. Jive says:

    I added another IF/THEN/ELSE statement to avoid placing the last deliminator

    Function concat(useThis As Range, Optional delim As String) As String
    ‘ this function will concatenate a range of cells and return one string
    ‘ useful when you have a rather large range of cells that you need to add up

    Dim retVal, dlm As String

    retVal = «»
    If delim = Null Then
    dlm = «»
    Else
    dlm = delim
    End If

    For Each cell In useThis
    If CStr(cell.Value) «» And CStr(cell.Value) » » Then
    If retVal «» Then
    retVal = retVal + dlm + CStr(cell.Value)
    Else
    retVal = CStr(cell.Value)
    End If
    End If
    Next

    If dlm = «» Then
    retVal = Left(retVal, Len(retVal) — Len(dlm))
    End If

    concat = retVal

    End Function

  53. Jive says:

    and here’s a modification that will concat only if they contain specified content

    Function concatonly(useThis As Range, contains As String, location As Integer, Optional delim As String) As String
    ‘ this function will concatenate a range of cells if they contain a search string and return one string with optional deliminator
    ‘ useful when you have a rather large range of cells that you need to add up
    ‘ format is concatselect(range, search string, search type (0 includes, 1 begins with, 2 end with), Optional deliminator)

    Dim retVal, dlm As String
    Dim stringfound As Boolean
    Dim searchstring As String

    If location > 0 Then
    If location > 1 Then
    searchstring = «*» + contains
    Else
    searchstring = contains + «*»
    End If
    Else
    searchstring = «*» + contains + «*»
    End If

    retVal = «»
    If delim = Null Then
    dlm = «»
    Else
    dlm = delim
    End If

    For Each cell In useThis
    stringfound = cell.Value Like searchstring
    If stringfound = True Then
    If CStr(cell.Value) «» And CStr(cell.Value) » » Then
    If retVal «» Then
    retVal = retVal + dlm + CStr(cell.Value)
    Else
    retVal = CStr(cell.Value)
    End If
    End If
    End If
    Next

    If dlm = «» Then
    retVal = Left(retVal, Len(retVal) — Len(dlm))
    End If

    concatonly = retVal

    End Function

    ‘ By the way: Thanks for posting this code — it’s gotta be the best add-in I’ve seen

  54. Jennifer says:

    This worked perfectly…what a time saver! Thank you so much!

  55. brad says:

    hey mate, thanks for code…. BUT I’m trying to concatenate numbers
    eg:
    250
    260
    261
    262
    402
    448
    When i use your code it gives me the result: 250251252253254255256257258259260261262263264402448
    I don’t want the 251 — 259
    I should be seeing:
    250260261262402448
    Please help… I’ve got about 200 strings to concatenate by tomorrow each of about 120 characters!!
    Thanks 🙂

  56. Hui… says:

    Brad
    Try the following code which must be put in a Code Module
    To use =Concat(A1:A10)
    .
    .
    Function Concat(useThis As Range) As String
    Dim c As Range
    Dim retVal, dlm As String
    retVal = «»
    For Each c In useThis
    retVal = retVal + Trim(CStr(c.Value))
    Next
    concat = retVal
    End Function

  57. Rick Rothstein (MVP — Excel) says:

    Here is a function that I have posted in the past (and which Debra Dalgleish is hosting on her site) that others may find useful (you can intermingle cells, ranges and text constants along with an optional delimiter). See this link…

    http://www.contextures.com/rickrothsteinexcelvbatext.html#combine

  58. brad says:

    AWESOME!!!! thanks for quick response!!! exactly what i needed

  59. flex says:

    hiho!! really nice code ;D exactly what i needed too
    but i’m trying to aggregate something with that function and i can’t get it work… i you know a way, I wold be glad

    is to add an boolean, if its true it count the number of rows. if it’s TRUE it count cells with values and if is more than 1 in the last it remove comma before the value and put » and » before the value. like:

    January — March — October — … — December
    with the actual function: January, March, October, December
    what i’m looking:January, March, October and December

  60. Hui… says:

    @Flex
    like
    =Concat(A1:A10,» — «) or =Concat(A1:A10,B1)
    Just change the code to the following
    .
    Function Concat(useThis As Range, Optional sep As String) As String
    Dim c As Range
    Dim retVal, dlm As String
    retVal = “”
    For Each c In useThis
    retVal = retVal + Trim(CStr(c.Value)) + sep
    Next
    Concat = Left(retVal, Len(retVal) — Len(sep))
    End Function

  61. Hui… says:

    @Flex
    Didn’t read your full question
    The following should answer all your queries
    use as
    .
    =Concat(A1:A10)
    JanFebMarApr…Dec
    .
    =Concat(A1:A10,», «)
    Jan, Feb, Mar, Apr…, Dec
    .
    =Concat(A1:A10,», «,1)
    Jan, Feb, Mar, Apr…Nov and Dec
    .
    ===
    Function Concat(useThis As Range, Optional sep As String, Optional last As Integer) As String

    Dim c As Range
    Dim retVal, dlm As String
    Dim NE As Integer, NC As Integer
    Dim i As Integer

    retVal = “”

    NR = useThis.Rows.Count
    NC = useThis.Columns.Count
    noitems = Application.WorksheetFunction.Max(NR, NC)

    If last = 1 Then noitems = noitems — 1
    For i = 1 To noitems
    retVal = retVal + Trim(CStr(useThis(i))) + sep
    Next
    If last = 1 Then retVal = Left(retVal, Len(retVal) — Len(sep)) + » and » + useThis(noitems + 1) + sep

    Concat = Left(retVal, Len(retVal) — Len(sep))
    End Function

  62. flex says:

    answered really fast ;o

    oh, i was even close..! but MANY. THANKS the result was exact what was expecting to be.
    thanks for the code, will be really useful.

  63. flex says:

    hmm, here again!

    i found something.. when the last cell in the range is blank («») the function don’t put the » and «.
    And if the last cell in the range it’s a number, it returns #error.

    i have to stop here for today, but i changed the code using lines of the function on the top of the page, to ignore the blank cell in the range(not working properly if blank cell is the last).

    here is:
    Function Concat(UseThis As range, Optional sep As String, Optional last As Integer) As String

    Dim c As range
    Dim retVal, dlm As String
    Dim NE As Integer, NC As Integer
    Dim i As Integer

    retVal = «»

    NR = UseThis.Rows.Count
    NC = UseThis.Columns.Count
    noitems = Application.WorksheetFunction.Max(NR, NC)

    If last = 1 Then noitems = noitems — 1
    For i = 1 To noitems
    If CStr(UseThis(i).Value) «» And CStr(UseThis(i).Value) » » Then
    retVal = retVal + Trim(CStr(UseThis(i))) + sep
    End If
    Next
    If last = 1 Then
    If CStr(UseThis(i).Value) «» And CStr(UseThis(i).Value) » » Then
    retVal = Left(retVal, Len(retVal) — Len(sep)) + » and » + UseThis(noitems + 1) + sep
    End If
    End If
    Concat = Left(retVal, Len(retVal) — Len(sep))
    End Function
    »»»»»»»»»»»»»’

  64. Hui… says:

    @Flex
    People always find a case you don’t test for
    Try this
    ===
    Function Concat(useThis As Range, Optional sep As String, Optional last As Integer) As String

    Dim c As Range
    Dim retVal, dlm As String
    Dim NE As Integer, NC As Integer
    Dim i As Integer
    Dim noItems As Integer

    retVal = «»

    NR = useThis.Rows.Count
    NC = useThis.Columns.Count
    noItems = Application.WorksheetFunction.Max(NR, NC)

    If last = 1 Then noItems = noItems — 1

    For i = 1 To noItems
    retVal = retVal + Trim(CStr(useThis(i))) + sep
    Next
    If last = 1 Then retVal = Left(retVal, Len(retVal) — Len(sep)) + » and » + Format(useThis(noItems + 1), «General Number») + sep
    If last = 1 And useThis(noItems + 1) = «» Then retVal = Left(retVal, Len(retVal) — Len(sep) — 2)

    Concat = Left(retVal, Len(retVal) — Len(sep))
    End Function

  65. Mike says:

    Hi,
    I have tried various options offered, but have not seen a fit for my challenge.

    I have a large range of values in a column. ie B1=»Hello» B2=»There» b3= «How?» etc. I would like to concat them based on a value in another column. For example A1=1, B1=1, c1=1. Basically I want to use a formula to define my range as I have additional values I want to concat seperatly in lower cell ranges. ie b4=»good» b5=»Bye» A4=2, A5=2. Any suggestions?
    Thanks a million!

  66. @Mike
    If I understand you correct you want to have a Concat If function
    That is concatenate values if other values meet a criteria
    .
    I have written a small UDF below which will do just that
    .
    Use
    =Concatif(Concat Range, Validation Range, Validation, [Seperator])
    Concat Range is a range of Values/text you want to concatenate together
    Validation Range is a range of Values/Text you want to comapre to a Validation value
    Validation is a Text or Number you want to compare the Validation range against
    Seperator is an Optional seperator and is a Null if not supplied
    .
    eg:
    =concatif(B1:B5,A1:A5,1)
    Will concatenate the Values in B1:B5 where A1:A5 = 1, with no seperator
    =concatif(B1:B5,A1:A5,»Tom»,»-«)
    Will concatenate the Values in Columns B1:B5 where A1:A5 = «Tom», with a — seperator
    =concatif(C12:G12,C14:G14,»John»,»-«)
    Will concatenate the Values in Rows B1:B5 where A1:A5 = «John», with a — seperator
    =concatif(C12:G12,A1:A5,D1,»-«)
    Will concatenate the Values in Rows B1:B5 where Column A1:A5 = Cell D1, with a — seperator

    Function ConcatIf(Src As Range, ChkRng As Range, myVal As Variant, Optional Sep As String) As String
    Dim c As Range
    Dim retVal As String
    Dim i As Integer

    retVal = ""
    i = 1

    For Each c In ChkRng
    If c = myVal Then
    retVal = retVal + Src(i) + Sep

    End If
    i = i + 1
    Next

    ConcatIf = Left(retVal, Len(retVal) - Len(Sep))
    End Function

    .
    You may need to check the » and — characters in VBA

  67. Mike says:

    Hui,
    I greatly appreciate the help. You have no idea!
    I tried the function, your 4th example is the one I am attempting to use.
    Unfortunatly I am getting a compile Syntax error. On what looks like the last line :ConcatIf = Left(retVal, Len(retVal) – Len(Sep))

    I tried it on a simple mock up
    in cell D1= concatif(B1:B4,A1:a4,C1,»-«)

    Column A Column B column C
    123 X 123
    123 Y
    456 Z
    456 Q

    Any other thoughts

  68. Yes
    Your formula is ok
    Note my very last line after the code
    .
    Retype the — sign on the line
    ConcatIf = Left(retVal, Len(retVal) – Len(Sep))
    even though it looks like a — it probably isn’t
    .
    In VBA if a line is highlighted Red there is something wrong with the syntax

  69. Mike says:

    Awesome! Awesome! Awesome! I bow down to you Sir. Thank you.

  70. Alex says:

    Thank you so much! I kept getting a #VALUE! error, when there was too much text (only like 200 characters… excel 2010), when I would delete certain parts, like parentheses out of the text it would work, however due to the nature of the text, I needed them. Anyways, this code did the trick, also I got the range code added it, sure saved a long list of cells!!!

  71. Wouter says:

    This works perfectly for letters, but when I try to use numbers, I get a #VALUE! error. Any ideas? Using excel 2010.

  72. @Wouter
    Try the following which has been slightly modified

    Function ConcatIf(Src As Range, ChkRng As Range, myVal As Variant, Optional Sep As String) As String
    Dim c As Range
    Dim retVal As String
    Dim i As Integer
    retVal = ""
    i = 1
    For Each c In ChkRng
    If c = myVal Then
    If WorksheetFunction.IsNumber(Src(i)) Then
    retVal = retVal + Trim(Str(Src(i))) + Sep
    Else
    retVal = retVal + Src(i) + Sep
    End If
    End If
    i = i + 1
    Next
    ConcatIf = Left(retVal, Len(retVal) - Len(Sep))
    End Function

  73. Pablo says:

    Wonderful code. I have just started using excel 2010 and use the code to create a string that the «get external data — msn stock quote function» works from. With msn now no longer supporting the stock quote addin (excel 2003) I was using, I found this code a god send. I have large list of stocks and this code has saved me from doing a massive concatenate formula. Brilliant, love it, thank you.

  74. Andrea says:

    More than 3 years after the original post and this is still helping people! I took the version you (Chandoo) wrote for Sheila and then added the bit that Jive contributed to leave out the last delimiter (at the end) and it is absolutely perfect and saved me from what would definitely have been a major migraine. THANK YOU!!! FYI, I used this in MS Excel 2010.

  75. Mihajlo says:

    This is a great function and I’m impressed with the discussion too.
    I did not find in the comments way to concatenate numeric cells as strings.
    I have custom format in Excel (“Text”0000, which gives me numbering Text0001, Text0002, etc) that actually stores numbers in cells. The concat UDF returned 1, 2, 3, … as a result instead of Text0001, Text0002, Text0003, …
    Well, I googled another solution, instead of using cell.value I used cell.text, it worked.
    So, these lines
    [code]For Each cell In useThis
    if cstr(cell.value)»» and cstr(cell.value)» » then
    retVal = retVal & cstr(cell.Value) & dlm
    end if [/code]
    I changed to these
    [code]For Each cell In useThis
    if cstr(cell.text)»» and cstr(cell.text)» » then
    retVal = retVal & cstr(cell.text) & dlm
    end if [/code]
    Cheers

  76. Mihajlo
    Well done & Correct
    As you’ve discovered
    cell.Value returns the Value
    cell.Text returns the displayed Text
    This is great where you want date strings

  77. floydbloke says:

    Thanks for this.

    I had a need to concatenate a range, quickly discovered the limitations of the built-in function, quick Google search, found your solution, pasted the code into the VB editor and voila.

    You’ve saved me hours of head scratching and frustrations.

  78. Chris says:

    Thanks! This worked for me and it was exactly what I needed to use in Excel 2010.

  79. Rick says:

    Thank you! You’re doing the lord’s work!

  80. Joe says:

    Hey there! This is a great function and has proved to be very useful, but I’ve run into what seems to be the same problem that Alex posted on May 2nd. I’m using concat() to dynamically concatenate a range of cells (because I didn’t know about the ConcatIf() function before finding it in these comments, but the work is basically done and I don’t feel like changing it if I don’t have to :P) and once I get over 100 cells that meet my criteria I get a #VALUE error. I need it to concatenate up to ~180 cells at most, so at the moment I can’t use this function almost half of the time. HALP! Thanks again!

  81. @Joe
    .
    There should be no limit (until you run out of memory) to the number of cells you can Contaif
    .
    I am able to easily Concatif 500 cells of 10 characters together using the Code from Post No. 70.
    =Concatif(A1:A500:B1:B500,1)
    .
    Can you be more specific about what your doing or send me your file?

  82. ScottieO says:

    This is awesome. Thank you so much for creating it. You saved me soooooo much time.

  83. Kevin says:

    This was so IMMENSELY useful. Thank you so very much for posting this Chandoo!!!

  84. Steve Warner says:

    When using this add in, I’m trying to use it on at least 15 cells, but it gives me a value error. If I reduce it to 5 cells, it will work. Any thoughts?

  85. Dave says:

    Thanks for the CONCAT() plug in, very helpful! I’ve concatenated a HUGE range (over 400 cells) and the result is almost 12,000 characters. Excel tends to truncate the _display_ of all characters however. I’m convinced the formula works as advertised because if I copy the cell into Word or Notepad, all the characters are there. This is not a complaint! 🙂 Just an fyi for anyone who might experience the same behavior.
    Thanks. Dave

  86. Ryan says:

    Thank you guys for such a great code, I found the concatif very helpful, but would you please help me with my case?

    I have the following list to concate

    #   Code     Value
    1   22         1001
    2   22         1002
        (Blank)  (Blank)
    3   22         1003
    4   22         1004
    5   22         1005
        (Blank)  (Blank)
    7   22         1006
    .
    .
    so on

    How can I concate Value with Code=22 and start from #3 to the last of the list?
    ConcatIf(Value, Code, 22, «, «, StartFrom (#3)) 

  87. Niegel says:

    Wonderful, just what I needed! Thanks for sharing!

  88. OdgeUK says:

    This really helped! Thanks! I had a column of data (numbers wrapped in quotes and suffixed with a comma) and this enabled me to place them all in one cell, one one line so I could then export this list into a WQL/SQL query. Saved a lot of time. Thanks again.

  89. Just found this great function from a google search. It Rocks!! It works easily and will especially help me when I do concatenated text data sends back into our FP&A system.
    I also love that it can give me just the results of cells that are full when I check very long ranges with blanks in it. Plus it is fast too.
    Thanks so much. You Rock!! This site has given me so much over the years. Keep up the great work.

  90. […] Maybe: How to add a range of cells in excel – concat() | Chandoo.org — Learn Microsoft Excel Online […]

  91. sushant harit says:

    I want to do this without VBA, is it possible ?? when i use the concatenate() func and input a cell range in a column it give a #Value error. however when i manually input cells seperated my comma it is working fine. help me out please.

    Thanks
    Sushant

    • xlpadawan says:

      Use the ADDRESS function nested within the SUBSTITUTE function to get the addresses for the cells you wish to concat (e.g. A5 B5 C5 D5 … BB5 BC5). Type =A5:BC5, replace braces {} with () and «,» with , and type CONCATENATE in front of the left parenthesis. Oh, and delete any remaining » inside the parentheses.

      • xlpadawan says:

        I’m sorry, it should have read:

        Type =A5:BC5—press F9—, replace braces {} …

  92. Jim2k says:

    had I read down the comments first I would have seen that @Hui had already written a concatif() function, however I did not do that before creating my own using the original concat() as inspiration, so here’s my own version I hope you find useful

    Public Function CONCATIF(criteria As Variant, criteria_range As range, Optional concat_range As range, Optional delim As String) As String
    ‘this function will concatenate a range of cells that meet the specified criteria and return one string
    ‘credit to chandoo.org for the original concat() function extended here to accept criteria evaluaition
    ‘How to add a range of cells in excel – concat() — http://chandoo.org/wp/2008/05/28/how-to-add-a-range-of-cells-in-excel-concat/

    Dim retVal, dlm As String
    Dim i As Integer

    retVal = «»
    If delim = Null Then
    dlm = «»
    Else
    dlm = delim
    End If

    If concat_range Is Nothing Then
    Set concat_range = criteria_range
    End If

    i = 1

    For Each cell In concat_range
    If criteria_range.Rows(i).Value = criteria Then
    If CStr(cell.Value) «» And CStr(cell.Value) » » Then
    retVal = retVal & CStr(cell.Value) & dlm
    End If
    End If
    i = i + 1
    Next

    If dlm «» Then
    retVal = Left(retVal, Len(retVal) — Len(dlm))
    End If

    CONCATIF = retVal

    End Function

    Regards,

    Jim

  93. Sunny says:

    Name Coins
    Ken Douglas 500
    Ken Douglas 400
    Maria Jones 111
    Warren Mayfield 245
    Maria Jones 344

    Hi,

    please look into the above data, I need a favor if anyone plz let me know how should I look coins value in an other column name wise. If I use vlookup It shows me only first value (500) for Ken Douglas.

  94. Litty says:

    hi,

    can you please help me to concatenate 2 strings in which one string is italics and i want the same format after concatenation. Is this possible?

  95. Denis says:

    If you add Chr(10) you can keep page breaks (alt+enter). So the text keep formatting.

    Sub FormMergeCells()
    Dim result As String

    For Each cell In Selection.Cells
    If Not cell.Value = vbNullString Then
    result = result & Chr(10) & Trim(cell.Value) & ””
    End If
    Next

    Application.CutCopyMode = False
    With Selection
    .Clear
    .HorizontalAlignment = xlLeft
    .VerticalAlignment = xlTop
    .WrapText = True
    .MergeCells = True
    End With

    Selection.Cells(1, 1).Value = result
    End Sub

  96. jas says:

    thank u !!

  97. Bob Arnett says:

    Concat is great. I was wondering, however, how one could suppress extra separators when a cell is empty. I keep ending up with results like: «Jim,,, Sally, Debbie, Carl,,,,,,» when there are empty cells within the range.

    • @Bob
      Chandoo’s original code does exactly that, suppresses the blank cells

      Which version from above are you using?

      Hui…

      • Bob Arnett says:

        I used the link to the installer «xla» file. I don’t know which version that is. I could copy the original code but I didn’t understand where to put it.

        • @Bob

          I think Chandoo updated the code just below the Addin but didn’t upgrade the addin.

          I have now updated the addin and so the functionality should be as you require.

          Hui…

  98. Bob Arnett says:

    Thanks worked great.

  99. jo says:

    Thanks! you’ve just saved me so much aggro.
    Note to self — time to pick up some vba skills.

  100. Amar says:

    Thanks chandoo… saved lot of time

  101. Ajith says:

    you sir are a genius and a gentleman 🙂

    Thanks a ton!

  102. Eva says:

    Thanks so much mate!

  103. Eric says:

    Hello, I have a little comment about the following line :

    Dim retVal, dlm As String

    What actually happens here is retVal is declared as a variant type variable, and dlm is declared as a string variable. It is the same as writing this :

    Dim retVal As Variant, dlm As String

    Try running the code and setting a break point on the next line, you will see this in the immediate window.

    What you should use is something like this :

    Dim retVal As String, dlm As String

    or even better, for better understanding and readability :

    Dim retVal As String
    Dim dlm As String

    • @Eric
      Although you are correct, there is also nothing wrong with using default values where appropriate
      Personally I don’t like mixing variable types on one line and so i will keep all my variants on one dim line and strings on another line

  104. I came across your solution as I was trying to code a solution of my own. However, I had a unique complication: I needed to accept an Array as an input instead of Range. Why? Because the inputted Array was being calculated using If statements on arrays of cells. For example:
    =IF(Table1[Status]=»OK»,Table1[ID])
    If you press control+shift+enter when entering this formula, Excel returns the array of all values in the column field of Table1 where that row has a status of «OK». Combining this with the concept you introduced above, you could generate a comma-separated list of all IDs where the row is OK.

    Because I had started writing my script before I found yours, I used different variable names and slightly different methods. But you can use my technique to extend your script if you wish in order to support either arrays or ranges of cells as an input:

    Function ConcatList(ValueRange As Variant, Optional Delimiter As String) As String

    On Error Resume Next

    Dim xCell As Range
    Dim ConcatValue As String
    Dim xVal As Variant

    If Delimiter = Null Then
    Delimiter = «»
    End If

    Debug.Print VarType(ValueRange)

    If VarType(ValueRange) = 8204 Then
    For Each xVal In ValueRange
    If xVal False Then
    ConcatValue = ConcatValue & Delimiter & CStr(xVal)
    End If
    Next xVal
    Else
    For Each xCell In ValueRange
    If LenB(xCell.Value) 0 Then
    ConcatValue = ConcatValue & Delimiter & CStr(xCell.Value)
    End If
    Next xCell
    End If

    ConcatValue = Right(ConcatValue, Len(ConcatValue) — Len(Delimiter))

    ConcatList = ConcatValue

    End Function

  105. Simon says:

    I love your concat command — that really helped me out. Thanks!

  106. Kim Wennerberg says:

    (Re-post to correct email for notification.)
    I have been using this great UDF for a while now.
    Seems that I always need to sort the values before concatenating them with CONCAT().
    Could the function be altered to have an optional argument to sort?
    While you are at it, another optional argument for length of cell would be nice. Negative value would take from right, positive value would take from left.

  107. Navin Agarwal says:

    I am able to use this seamlessly by installing this UDF i.e (Just download the concat() UDF excel add-in and double click on it to install it). But i need to share the tool i am building with others and since i have used this formula in my tool, it will not work on others’ computer i suppose. Is there a way i can embedd this in my excel file it self so that even if i pass my file to someone else, they can continue to use to file without installing the UDF on their end? Thanks

  108. Sol says:

    I have to leave a comment — thank you so much for your code, you have no idea how much time you have saved for me… Thank you again!

  109. ExcelN00b says:

    Hi Chandoo,

    First let me say that your function has saved me countless hours.
    Thank you for that.

    I just have one tiny issue that I have been trying to get around with no luck.

    Your function works perfectly for what I need, but it is also returning duplicate values. Is there a way to edit the function so it doesn’t return duplicate values?

    Example:

    A1 = 100
    A2 = 100, 200, 300
    A3 = 200, 400
    A4 = 200
    A5 = 400

    Using your function in A6 = 100, 100, 200, 300, 200, 400, 200, 400

    Is there a way to edit the function only unique values are displayed?
    Which would make A6 = 100, 200, 300, 400
    Any help would be appreciated.

    Here is the version of the function I am using:

    Function ConCat(useThis As Range, Optional delim As String) As String
    ‘ This function will concatenate a range of cells and return one string
    ‘ Useful when you have a rather large range of cells that you need to add up
    Dim retVal, dlm As String
    retVal = «»
    If delim = Null Then
    dlm = «»
    Else
    dlm = delim
    End If
    For Each cell In useThis
    If CStr(cell.Value) «» And CStr(cell.Value) » » Then
    retVal = retVal & CStr(cell.Value) & dlm
    End If
    Next
    If dlm «» Then
    retVal = Left(retVal, Len(retVal) — Len(dlm))
    End If
    ConCat = retVal
    End Function

  110. This formula is great. Is there a way to make it FILTER smart? I have a long list that needs concatenating, but only after it is filtered by one of several criteria.
    When I apply the filter to show only one category, then use the =concat(B2:B43) formula at the bottom of the filtered list, the results include all rows between B2 & B43, not just the rows that match the filter.
    Any help would be appreciated.

  111. Justin says:

    The simplest way to do this is to use the TEXTJOIN formula.

    =TEXTJOIN(«delimiter»,boolean remove null cells, RANGE)

    • KIM WENNERBERG says:

      Is TEXTJOIN is a built-in Excel function? Unbeknownst to you, you have been provided with your own custom User Defined Function that someone called TEXTJOIN. If you looked at the code in TEXTJOIN you’d likely see something very similar to CONCAT.

      • Justin says:

        TEXTJOIN is a built in feature for Exvel2016. It was released in February. If you use older versions of Excel you still have to use the UDF or other solutions here.

        • KIM WENNERBERG says:

          That’s great that Excel now has that «concatenate a range» function built in, but I still find Excel versions back to 2012 to be very common. Companies are now being slow to upgrade. I won’t be holding my breath on that brand new function.

  112. LJP says:

    Sorry, I can’t get this to work in Excel 2010 32-bit.

    I have successfully created the .xlam add-in (xla didn’t work), it «finds» the formula, however it never works it comes up with «compile error variable not defined» error message, highlighting «cell» in «For Each cell In useThis»

    Please help, thanks

    Lyndon

    _________________________________________________________

    Function concat(useThis As Range, Optional delim As String) As String
    ‘ this function will concatenate a range of cells and return one string
    ‘ useful when you have a rather large range of cells that you need to add up
    Dim retVal, dlm As String
    retVal = «»
    If delim = Null Then
    dlm = «»
    Else
    dlm = delim
    End If
    For Each cell In useThis
    If CStr(cell.Value) «» And CStr(cell.Value) » » Then
    retVal = retVal & CStr(cell.Value) & dlm
    End If
    Next
    If dlm «» Then
    retVal = Left(retVal, Len(retVal) — Len(dlm))
    End If
    concat = retVal
    End Function

  113. tototime says:

    Hello, I seem to have trouble having this function reference a range to concatenate in a different cell. For example, I have a value in A1 that is the reference for the range I’d like to concatenate (i.e. Sheet1!C1:Sheet1C300). If I were to write the function in B1 as «=ConCat(CELL(«contents»,A1),» «)», I receive a value error. When I step into the function, it looks like it returns the A1 value as within quotes, which returns a value error for the ConCat function. Is there a way to rectify this?

  114. If I have a list like this
    03005021
    03005022
    03005042
    03006023
    03006024
    03006025
    and run the =concat(A1:A6,»|»)
    I get
    3005021|3005022|3005042|3006023|3006024|3006025
    The leading zero is dropped. I must havr this leading digit whether it is zero or not.
    I have tried everything I can think of.
    Is there a way to concat a list without excel doing this truncating?

    • @TMBadmin: Welcome to Chandoo.org and thanks for your comment.

      Replace cell.value with cell.text in the code to get leading zeros.

  115. Chandoo Fan says:

    Years after you write it — you’re STILL a legend!!

  116. Reza says:

    Thank you soooooooooooo much

  117. Kapil Jain says:

    I want to concatenate column A and column B cells value with separator using vba. My result will be shown on cell D2

  118. Kelvin says:

    Hi, this is an awesome bit of code, is there any way to insert a line break between each value?

    Thanks

    • @Kelvin

      If you continue to read the Comments below the post there are several examples of alternative Concat and Concatif versions of the code
      You can always add a Line Feed using char(10)

  119. Nadine says:

    Hi guys,
    just came across this and wanted to add that Excel now has a was easier way of doing this with the TextJoin function. I’m sure you’re aware of that already, but thought it might be helpful to others that read this thread.

    Cheers
    Nadine

Leave a Reply

Cell, Row, Column | Range Examples | Fill a Range | Move a Range | Copy/Paste a Range | Insert Row, Column

A range in Excel is a collection of two or more cells. This chapter gives an overview of some very important range operations.

Cell, Row, Column

Let’s start by selecting a cell, row and column.

1. To select cell C3, click on the box at the intersection of column C and row 3.

Select a Cell

2. To select column C, click on the column C header.

Select a Column in Excel

3. To select row 3, click on the row 3 header.

Select a Row

Range Examples

A range is a collection of two or more cells.

1. To select the range B2:C4, click on cell B2 and drag it to cell C4.

Range in Excel

2. To select a range of individual cells, hold down CTRL and click on each cell that you want to include in the range.

A Range of Individual Cells

Fill a Range

To fill a range, execute the following steps.

1a. Enter the value 2 into cell B2.

Enter a Value

1b. Select cell B2, click on the lower right corner of cell B2 and drag it down to cell B8.

Drag it down

Result:

Drag Down Result

This dragging technique is very important and you will use it very often in Excel. Here’s another example.

2a. Enter the value 2 into cell B2 and the value 4 into cell B3.

A pattern

2b. Select cell B2 and cell B3, click on the lower right corner of this range and drag it down.

Pattern Dragged Down

Excel automatically fills the range based on the pattern of the first two values. That’s pretty cool huh!? Here’s another example.

3a. Enter the date 6/13/2016 into cell B2 and the date 6/16/2016 into cell B3.

Date Pattern

3b. Select cell B2 and cell B3, click on the lower right corner of this range and drag it down.

Date Pattern Dragged Down

Note: visit our page about AutoFill for many more examples.

Move a Range

To move a range, execute the following steps.

1. Select a range and click on the border of the range.

Move a Range

2. Drag the range to its new location.

Move Result

Copy/Paste a Range

To copy and paste a range, execute the following steps.

1. Select the range, right click, and then click Copy (or press CTRL + c).

Copy/Paste a Range

2. Select the cell where you want the first cell of the range to appear, right click, and then click Paste under ‘Paste Options:’ (or press CTRL + v).

Copy/Paste Result

Insert Row, Column

To insert a row between the values 20 and 40 below, execute the following steps.

1. Select row 3.

Insert a Row

2. Right click, and then click Insert.

Click Insert

Result.

Inserted Row

The rows below the new row are shifted down. In a similar way, you can insert a column.

На чтение 18 мин. Просмотров 74.6k.

VBA Range

сэр Артур Конан Дойл

Это большая ошибка — теоретизировать, прежде чем кто-то получит данные

Эта статья охватывает все, что вам нужно знать об использовании ячеек и диапазонов в VBA. Вы можете прочитать его от начала до конца, так как он сложен в логическом порядке. Или использовать оглавление ниже, чтобы перейти к разделу по вашему выбору.

Рассматриваемые темы включают свойство смещения, чтение
значений между ячейками, чтение значений в массивы и форматирование ячеек.

Содержание

  1. Краткое руководство по диапазонам и клеткам
  2. Введение
  3. Важное замечание
  4. Свойство Range
  5. Свойство Cells рабочего листа
  6. Использование Cells и Range вместе
  7. Свойство Offset диапазона
  8. Использование диапазона CurrentRegion
  9. Использование Rows и Columns в качестве Ranges
  10. Использование Range вместо Worksheet
  11. Чтение значений из одной ячейки в другую
  12. Использование метода Range.Resize
  13. Чтение Value в переменные
  14. Как копировать и вставлять ячейки
  15. Чтение диапазона ячеек в массив
  16. Пройти через все клетки в диапазоне
  17. Форматирование ячеек
  18. Основные моменты

Краткое руководство по диапазонам и клеткам

Функция Принимает Возвращает Пример Вид
Range адреса
ячеек
диапазон
ячеек
.Range(«A1:A4») $A$1:$A$4
Cells строка,
столбец
одна
ячейка
.Cells(1,5) $E$1
Offset строка,
столбец
диапазон .Range(«A1:A2»)
.Offset(1,2)
$C$2:$C$3
Rows строка (-и) одна или
несколько
строк
.Rows(4)
.Rows(«2:4»)
$4:$4
$2:$4
Columns столбец
(-цы)
один или
несколько
столбцов
.Columns(4)
.Columns(«B:D»)
$D:$D
$B:$D

Введение

Это третья статья, посвященная трем основным элементам VBA. Этими тремя элементами являются Workbooks, Worksheets и Ranges/Cells. Cells, безусловно, самая важная часть Excel. Почти все, что вы делаете в Excel, начинается и заканчивается ячейками.

Вы делаете три основных вещи с помощью ячеек:

  1. Читаете из ячейки.
  2. Пишите в ячейку.
  3. Изменяете формат ячейки.

В Excel есть несколько методов для доступа к ячейкам, таких как Range, Cells и Offset. Можно запутаться, так как эти функции делают похожие операции.

В этой статье я расскажу о каждом из них, объясню, почему они вам нужны, и когда вам следует их использовать.

Давайте начнем с самого простого метода доступа к ячейкам — с помощью свойства Range рабочего листа.

Важное замечание

Я недавно обновил эту статью, сейчас использую Value2.

Вам может быть интересно, в чем разница между Value, Value2 и значением по умолчанию:

' Value2
Range("A1").Value2 = 56

' Value
Range("A1").Value = 56

' По умолчанию используется значение
Range("A1") = 56

Использование Value может усечь число, если ячейка отформатирована, как валюта. Если вы не используете какое-либо свойство, по умолчанию используется Value.

Лучше использовать Value2, поскольку он всегда будет возвращать фактическое значение ячейки.

Свойство Range

Рабочий лист имеет свойство Range, которое можно использовать для доступа к ячейкам в VBA. Свойство Range принимает тот же аргумент, что и большинство функций Excel Worksheet, например: «А1», «А3: С6» и т.д.

В следующем примере показано, как поместить значение в ячейку с помощью свойства Range.

Sub ZapisVYacheiku()

    ' Запишите число в ячейку A1 на листе 1 этой книги
    ThisWorkbook.Worksheets("Лист1").Range("A1").Value2 = 67

    ' Напишите текст в ячейку A2 на листе 1 этой рабочей книги
    ThisWorkbook.Worksheets("Лист1").Range("A2").Value2 = "Иван Петров"

    ' Запишите дату в ячейку A3 на листе 1 этой книги
    ThisWorkbook.Worksheets("Лист1").Range("A3").Value2 = #11/21/2019#

End Sub

Как видно из кода, Range является членом Worksheets, которая, в свою очередь, является членом Workbook. Иерархия такая же, как и в Excel, поэтому должно быть легко понять. Чтобы сделать что-то с Range, вы должны сначала указать рабочую книгу и рабочий лист, которому она принадлежит.

В оставшейся части этой статьи я буду использовать кодовое имя для ссылки на лист.

code name worksheet

Следующий код показывает приведенный выше пример с использованием кодового имени рабочего листа, т.е. Лист1 вместо ThisWorkbook.Worksheets («Лист1»).

Sub IspKodImya ()

    ' Запишите число в ячейку A1 на листе 1 этой книги    
     Sheet1.Range("A1").Value2 = 67

    ' Напишите текст в ячейку A2 на листе 1 этой рабочей книги
    Sheet1.Range("A2").Value2 = "Иван Петров"

    ' Запишите дату в ячейку A3 на листе 1 этой книги
    Sheet1.Range("A3").Value2 = #11/21/2019#

End Sub

Вы также можете писать в несколько ячеек, используя свойство
Range

Sub ZapisNeskol()

    ' Запишите число в диапазон ячеек
    Sheet1.Range("A1:A10").Value2 = 67

    ' Написать текст в несколько диапазонов ячеек
    Sheet1.Range("B2:B5,B7:B9").Value2 = "Иван Петров"

End Sub

Свойство Cells рабочего листа

У Объекта листа есть другое свойство, называемое Cells, которое очень похоже на Range . Есть два отличия:

  1. Cells возвращают диапазон только одной ячейки.
  2. Cells принимает строку и столбец в качестве аргументов.

В приведенном ниже примере показано, как записывать значения
в ячейки, используя свойства Range и Cells.

Sub IspCells()

    ' Написать в А1
    Sheet1.Range("A1").Value2 = 10
    Sheet1.Cells(1, 1).Value2  = 10

    ' Написать в А10
    Sheet1.Range("A10").Value2 = 10
    Sheet1.Cells(10, 1).Value2  = 10

    ' Написать в E1
    Sheet1.Range("E1").Value2 = 10
    Sheet1.Cells(1, 5).Value2  = 10

End Sub

Вам должно быть интересно, когда использовать Cells, а когда Range. Использование Range полезно для доступа к одним и тем же ячейкам при каждом запуске макроса.

Например, если вы использовали макрос для вычисления суммы и
каждый раз записывали ее в ячейку A10, тогда Range подойдет для этой задачи.

Использование свойства Cells полезно, если вы обращаетесь к
ячейке по номеру, который может отличаться. Проще объяснить это на примере.

В следующем коде мы просим пользователя указать номер столбца. Использование Cells дает нам возможность использовать переменное число для столбца.

Sub ZapisVPervuyuPustuyuYacheiku()

    Dim UserCol As Integer
    
    ' Получить номер столбца от пользователя
    UserCol = Application.InputBox("Пожалуйста, введите номер столбца...", Type:=1)
    
    ' Написать текст в выбранный пользователем столбец
    Sheet1.Cells(1, UserCol).Value2 = "Иван Петров"

End Sub

В приведенном выше примере мы используем номер для столбца,
а не букву.

Чтобы использовать Range здесь, потребуется преобразовать эти значения в ссылку на
буквенно-цифровую ячейку, например, «С1». Использование свойства Cells позволяет нам
предоставить строку и номер столбца для доступа к ячейке.

Иногда вам может понадобиться вернуть более одной ячейки, используя номера строк и столбцов. В следующем разделе показано, как это сделать.

Использование Cells и Range вместе

Как вы уже видели, вы можете получить доступ только к одной ячейке, используя свойство Cells. Если вы хотите вернуть диапазон ячеек, вы можете использовать Cells с Range следующим образом:

Sub IspCellsSRange()

    With Sheet1
        ' Запишите 5 в диапазон A1: A10, используя свойство Cells
        .Range(.Cells(1, 1), .Cells(10, 1)).Value2 = 5

        ' Диапазон B1: Z1 будет выделен жирным шрифтом
        .Range(.Cells(1, 2), .Cells(1, 26)).Font.Bold = True

    End With

End Sub

Как видите, вы предоставляете начальную и конечную ячейку
диапазона. Иногда бывает сложно увидеть, с каким диапазоном вы имеете дело,
когда значением являются все числа. Range имеет свойство Address, которое
отображает буквенно-цифровую ячейку для любого диапазона. Это может
пригодиться, когда вы впервые отлаживаете или пишете код.

В следующем примере мы распечатываем адрес используемых нами
диапазонов.

Sub PokazatAdresDiapazona()

    ' Примечание. Использование подчеркивания позволяет разделить строки кода.
    With Sheet1

        ' Запишите 5 в диапазон A1: A10, используя свойство Cells
        .Range(.Cells(1, 1), .Cells(10, 1)).Value2 = 5
        Debug.Print "Первый адрес: " _
            + .Range(.Cells(1, 1), .Cells(10, 1)).Address

        ' Диапазон B1: Z1 будет выделен жирным шрифтом
        .Range(.Cells(1, 2), .Cells(1, 26)).Font.Bold = True
        Debug.Print "Второй адрес : " _
            + .Range(.Cells(1, 2), .Cells(1, 26)).Address

    End With

End Sub

В примере я использовал Debug.Print для печати в Immediate Window. Для просмотра этого окна выберите «View» -> «в Immediate Window» (Ctrl +  G).

ImmediateWindow

ImmediateSampeText

Свойство Offset диапазона

У диапазона есть свойство, которое называется Offset. Термин «Offset» относится к отсчету от исходной позиции. Он часто используется в определенных областях программирования. С помощью свойства «Offset» вы можете получить диапазон ячеек того же размера и на определенном расстоянии от текущего диапазона. Это полезно, потому что иногда вы можете выбрать диапазон на основе определенного условия. Например, на скриншоте ниже есть столбец для каждого дня недели. Учитывая номер дня (т.е. понедельник = 1, вторник = 2 и т.д.). Нам нужно записать значение в правильный столбец.

VBA Offset

Сначала мы попытаемся сделать это без использования Offset.

' Это Sub тесты с разными значениями
Sub TestSelect()

    ' Понедельник
    SetValueSelect 1, 111.21
    ' Среда
    SetValueSelect 3, 456.99
    ' Пятница
    SetValueSelect 5, 432.25
    ' Воскресение
    SetValueSelect 7, 710.17

End Sub

' Записывает значение в столбец на основе дня
Public Sub SetValueSelect(lDay As Long, lValue As Currency)

    Select Case lDay
        Case 1: Sheet1.Range("H3").Value2 = lValue
        Case 2: Sheet1.Range("I3").Value2 = lValue
        Case 3: Sheet1.Range("J3").Value2 = lValue
        Case 4: Sheet1.Range("K3").Value2 = lValue
        Case 5: Sheet1.Range("L3").Value2 = lValue
        Case 6: Sheet1.Range("M3").Value2 = lValue
        Case 7: Sheet1.Range("N3").Value2 = lValue
    End Select

End Sub

Как видно из примера, нам нужно добавить строку для каждого возможного варианта. Это не идеальная ситуация. Использование свойства Offset обеспечивает более чистое решение.

' Это Sub тесты с разными значениями
Sub TestOffset()

    DayOffSet 1, 111.01
    DayOffSet 3, 456.99
    DayOffSet 5, 432.25
    DayOffSet 7, 710.17

End Sub

Public Sub DayOffSet(lDay As Long, lValue As Currency)

    ' Мы используем значение дня с Offset, чтобы указать правильный столбец
    Sheet1.Range("G3").Offset(, lDay).Value2 = lValue

End Sub

Как видите, это решение намного лучше. Если количество дней увеличилось, нам больше не нужно добавлять код. Чтобы Offset был полезен, должна быть какая-то связь между позициями ячеек. Если столбцы Day в приведенном выше примере были случайными, мы не могли бы использовать Offset. Мы должны были бы использовать первое решение.

Следует иметь в виду, что Offset сохраняет размер диапазона. Итак .Range («A1:A3»).Offset (1,1) возвращает диапазон B2:B4. Ниже приведены еще несколько примеров использования Offset.

Sub IspOffset()

    ' Запись в В2 - без Offset
    Sheet1.Range("B2").Offset().Value2 = "Ячейка B2"

    ' Написать в C2 - 1 столбец справа
    Sheet1.Range("B2").Offset(, 1).Value2 = "Ячейка C2"

    ' Написать в B3 - 1 строка вниз
    Sheet1.Range("B2").Offset(1).Value2 = "Ячейка B3"

    ' Запись в C3 - 1 столбец справа и 1 строка вниз
    Sheet1.Range("B2").Offset(1, 1).Value2 = "Ячейка C3"

    ' Написать в A1 - 1 столбец слева и 1 строка вверх
    Sheet1.Range("B2").Offset(-1, -1).Value2 = "Ячейка A1"

    ' Запись в диапазон E3: G13 - 1 столбец справа и 1 строка вниз
    Sheet1.Range("D2:F12").Offset(1, 1).Value2 = "Ячейки E3:G13"

End Sub

Использование диапазона CurrentRegion

CurrentRegion возвращает диапазон всех соседних ячеек в данный диапазон. На скриншоте ниже вы можете увидеть два CurrentRegion. Я добавил границы, чтобы прояснить CurrentRegion.

VBA CurrentRegion

Строка или столбец пустых ячеек означает конец CurrentRegion.

Вы можете вручную проверить
CurrentRegion в Excel, выбрав диапазон и нажав Ctrl + Shift + *.

Если мы возьмем любой диапазон
ячеек в пределах границы и применим CurrentRegion, мы вернем диапазон ячеек во
всей области.

Например:

Range («B3»). CurrentRegion вернет диапазон B3:D14

Range («D14»). CurrentRegion вернет диапазон B3:D14

Range («C8:C9»). CurrentRegion вернет диапазон B3:D14 и так далее

Как пользоваться

Мы получаем CurrentRegion следующим образом

' CurrentRegion вернет B3:D14 из приведенного выше примера
Dim rg As Range
Set rg = Sheet1.Range("B3").CurrentRegion

Только чтение строк данных

Прочитать диапазон из второй строки, т.е. пропустить строку заголовка.

' CurrentRegion вернет B3:D14 из приведенного выше примера
Dim rg As Range
Set rg = Sheet1.Range("B3").CurrentRegion

' Начало в строке 2 - строка после заголовка
Dim i As Long
For i = 2 To rg.Rows.Count
    ' текущая строка, столбец 1 диапазона
    Debug.Print rg.Cells(i, 1).Value2
Next i

Удалить заголовок

Удалить строку заголовка (т.е. первую строку) из диапазона. Например, если диапазон — A1:D4, это возвратит A2:D4

' CurrentRegion вернет B3:D14 из приведенного выше примера
Dim rg As Range
Set rg = Sheet1.Range("B3").CurrentRegion

' Удалить заголовок
Set rg = rg.Resize(rg.Rows.Count - 1).Offset(1)

' Начните со строки 1, так как нет строки заголовка
Dim i As Long
For i = 1 To rg.Rows.Count
    ' текущая строка, столбец 1 диапазона
    Debug.Print rg.Cells(i, 1).Value2
Next i

Использование Rows и Columns в качестве Ranges

Если вы хотите что-то сделать со всей строкой или столбцом,
вы можете использовать свойство «Rows и
Columns
» на рабочем листе. Они оба принимают один параметр — номер строки
или столбца, к которому вы хотите получить доступ.

Sub IspRowIColumns()

    ' Установите размер шрифта столбца B на 9
    Sheet1.Columns(2).Font.Size = 9

    ' Установите ширину столбцов от D до F
    Sheet1.Columns("D:F").ColumnWidth = 4

    ' Установите размер шрифта строки 5 до 18
    Sheet1.Rows(5).Font.Size = 18

End Sub

Использование Range вместо Worksheet

Вы также можете использовать Cella, Rows и Columns, как часть Range, а не как часть Worksheet. У вас может быть особая необходимость в этом, но в противном случае я бы избегал практики. Это делает код более сложным. Простой код — твой друг. Это уменьшает вероятность ошибок.

Код ниже выделит второй столбец диапазона полужирным. Поскольку диапазон имеет только две строки, весь столбец считается B1:B2

Sub IspColumnsVRange()

    ' Это выделит B1 и B2 жирным шрифтом.
    Sheet1.Range("A1:C2").Columns(2).Font.Bold = True

End Sub

Чтение значений из одной ячейки в другую

В большинстве примеров мы записали значения в ячейку. Мы
делаем это, помещая диапазон слева от знака равенства и значение для размещения
в ячейке справа. Для записи данных из одной ячейки в другую мы делаем то же
самое. Диапазон назначения идет слева, а диапазон источника — справа.

В следующем примере показано, как это сделать:

Sub ChitatZnacheniya()

    ' Поместите значение из B1 в A1
    Sheet1.Range("A1").Value2 = Sheet1.Range("B1").Value2

    ' Поместите значение из B3 в лист2 в ячейку A1
    Sheet1.Range("A1").Value2 = Sheet2.Range("B3").Value2

    ' Поместите значение от B1 в ячейки A1 до A5
    Sheet1.Range("A1:A5").Value2 = Sheet1.Range("B1").Value2

    ' Вам необходимо использовать свойство «Value», чтобы прочитать несколько ячеек
    Sheet1.Range("A1:A5").Value2 = Sheet1.Range("B1:B5").Value2

End Sub

Как видно из этого примера, невозможно читать из нескольких ячеек. Если вы хотите сделать это, вы можете использовать функцию копирования Range с параметром Destination.

Sub KopirovatZnacheniya()

    ' Сохранить диапазон копирования в переменной
    Dim rgCopy As Range
    Set rgCopy = Sheet1.Range("B1:B5")

    ' Используйте это для копирования из более чем одной ячейки
    rgCopy.Copy Destination:=Sheet1.Range("A1:A5")

    ' Вы можете вставить в несколько мест назначения
    rgCopy.Copy Destination:=Sheet1.Range("A1:A5,C2:C6")

End Sub

Функция Copy копирует все, включая формат ячеек. Это тот же результат, что и ручное копирование и вставка выделения. Подробнее об этом вы можете узнать в разделе «Копирование и вставка ячеек»

Использование метода Range.Resize

При копировании из одного диапазона в другой с использованием присваивания (т.е. знака равенства) диапазон назначения должен быть того же размера, что и исходный диапазон.

Использование функции Resize позволяет изменить размер
диапазона до заданного количества строк и столбцов.

Например:

Sub ResizePrimeri()
 
    ' Печатает А1
    Debug.Print Sheet1.Range("A1").Address

    ' Печатает A1:A2
    Debug.Print Sheet1.Range("A1").Resize(2, 1).Address

    ' Печатает A1:A5
    Debug.Print Sheet1.Range("A1").Resize(5, 1).Address
    
    ' Печатает A1:D1
    Debug.Print Sheet1.Range("A1").Resize(1, 4).Address
    
    ' Печатает A1:C3
    Debug.Print Sheet1.Range("A1").Resize(3, 3).Address
    
End Sub

Когда мы хотим изменить наш целевой диапазон, мы можем
просто использовать исходный размер диапазона.

Другими словами, мы используем количество строк и столбцов
исходного диапазона в качестве параметров для изменения размера:

Sub Resize()

    Dim rgSrc As Range, rgDest As Range
    
    ' Получить все данные в текущей области
    Set rgSrc = Sheet1.Range("A1").CurrentRegion

      ' Получить диапазон назначения
    Set rgDest = Sheet2.Range("A1")
    Set rgDest = rgDest.Resize(rgSrc.Rows.Count, rgSrc.Columns.Count)
    
    rgDest.Value2 = rgSrc.Value2

End Sub

Мы можем сделать изменение размера в одну строку, если нужно:

Sub Resize2()

    Dim rgSrc As Range
    
    ' Получить все данные в ткущей области
    Set rgSrc = Sheet1.Range("A1").CurrentRegion
    
    With rgSrc
        Sheet2.Range("A1").Resize(.Rows.Count, .Columns.Count) = .Value2
    End With
    
End Sub

Чтение Value в переменные

Мы рассмотрели, как читать из одной клетки в другую. Вы также можете читать из ячейки в переменную. Переменная используется для хранения значений во время работы макроса. Обычно вы делаете это, когда хотите манипулировать данными перед тем, как их записать. Ниже приведен простой пример использования переменной. Как видите, значение элемента справа от равенства записывается в элементе слева от равенства.

Sub IspVar()

    ' Создайте
    Dim val As Integer

    ' Читать число из ячейки
    val = Sheet1.Range("A1").Value2

    ' Добавить 1 к значению
    val = val + 1

    ' Запишите новое значение в ячейку
    Sheet1.Range("A2").Value2 = val

End Sub

Для чтения текста в переменную вы используете переменную
типа String.

Sub IspVarText()

    ' Объявите переменную типа string
    Dim sText As String

    ' Считать значение из ячейки
    sText = Sheet1.Range("A1").Value2

    ' Записать значение в ячейку
    Sheet1.Range("A2").Value2 = sText

End Sub

Вы можете записать переменную в диапазон ячеек. Вы просто
указываете диапазон слева, и значение будет записано во все ячейки диапазона.

Sub VarNeskol()

    ' Считать значение из ячейки
    Sheet1.Range("A1:B10").Value2 = 66

End Sub

Вы не можете читать из нескольких ячеек в переменную. Однако
вы можете читать массив, который представляет собой набор переменных. Мы
рассмотрим это в следующем разделе.

Как копировать и вставлять ячейки

Если вы хотите скопировать и вставить диапазон ячеек, вам не
нужно выбирать их. Это распространенная ошибка, допущенная новыми пользователями
VBA.

Вы можете просто скопировать ряд ячеек, как здесь:

Range("A1:B4").Copy Destination:=Range("C5")

При использовании этого метода копируется все — значения,
форматы, формулы и так далее. Если вы хотите скопировать отдельные элементы, вы
можете использовать свойство PasteSpecial
диапазона.

Работает так:

Range("A1:B4").Copy
Range("F3").PasteSpecial Paste:=xlPasteValues
Range("F3").PasteSpecial Paste:=xlPasteFormats
Range("F3").PasteSpecial Paste:=xlPasteFormulas

В следующей таблице приведен полный список всех типов вставок.

Виды вставок
xlPasteAll
xlPasteAllExceptBorders
xlPasteAllMergingConditionalFormats
xlPasteAllUsingSourceTheme
xlPasteColumnWidths
xlPasteComments
xlPasteFormats
xlPasteFormulas
xlPasteFormulasAndNumberFormats
xlPasteValidation
xlPasteValues
xlPasteValuesAndNumberFormats

Чтение диапазона ячеек в массив

Вы также можете скопировать значения, присвоив значение
одного диапазона другому.

Range("A3:Z3").Value2 = Range("A1:Z1").Value2

Значение диапазона в этом примере считается вариантом массива. Это означает, что вы можете легко читать из диапазона ячеек в массив. Вы также можете писать из массива в диапазон ячеек. Если вы не знакомы с массивами, вы можете проверить их в этой статье.

В следующем коде показан пример использования массива с
диапазоном.

Sub ChitatMassiv()

    ' Создать динамический массив
    Dim StudentMarks() As Variant

    ' Считать 26 значений в массив из первой строки
    StudentMarks = Range("A1:Z1").Value2

    ' Сделайте что-нибудь с массивом здесь

    ' Запишите 26 значений в третью строку
    Range("A3:Z3").Value2 = StudentMarks

End Sub

Имейте в виду, что массив, созданный для чтения, является
двумерным массивом. Это связано с тем, что электронная таблица хранит значения
в двух измерениях, то есть в строках и столбцах.

Пройти через все клетки в диапазоне

Иногда вам нужно просмотреть каждую ячейку, чтобы проверить значение.

Вы можете сделать это, используя цикл For Each, показанный в следующем коде.

Sub PeremeschatsyaPoYacheikam()

    ' Пройдите через каждую ячейку в диапазоне
    Dim rg As Range
    For Each rg In Sheet1.Range("A1:A10,A20")
        ' Распечатать адрес ячеек, которые являются отрицательными
        If rg.Value < 0 Then
            Debug.Print rg.Address + " Отрицательно."
        End If
    Next

End Sub

Вы также можете проходить последовательные ячейки, используя
свойство Cells и стандартный цикл For.

Стандартный цикл более гибок в отношении используемого вами
порядка, но он медленнее, чем цикл For Each.

Sub PerehodPoYacheikam()
 
    ' Пройдите клетки от А1 до А10
    Dim i As Long
    For i = 1 To 10
        ' Распечатать адрес ячеек, которые являются отрицательными
        If Range("A" & i).Value < 0 Then
            Debug.Print Range("A" & i).Address + " Отрицательно."
        End If
    Next
 
    ' Пройдите в обратном порядке, то есть от A10 до A1
    For i = 10 To 1 Step -1
        ' Распечатать адрес ячеек, которые являются отрицательными
        If Range("A" & i) < 0 Then
            Debug.Print Range("A" & i).Address + " Отрицательно."
        End If
    Next
 
End Sub

Форматирование ячеек

Иногда вам нужно будет отформатировать ячейки в электронной
таблице. Это на самом деле очень просто. В следующем примере показаны различные
форматы, которые можно добавить в любой диапазон ячеек.

Sub FormatirovanieYacheek()

    With Sheet1

        ' Форматировать шрифт
        .Range("A1").Font.Bold = True
        .Range("A1").Font.Underline = True
        .Range("A1").Font.Color = rgbNavy

        ' Установите числовой формат до 2 десятичных знаков
        .Range("B2").NumberFormat = "0.00"
        ' Установите числовой формат даты
        .Range("C2").NumberFormat = "dd/mm/yyyy"
        ' Установите формат чисел на общий
        .Range("C3").NumberFormat = "Общий"
        ' Установить числовой формат текста
        .Range("C4").NumberFormat = "Текст"

        ' Установите цвет заливки ячейки
        .Range("B3").Interior.Color = rgbSandyBrown

        ' Форматировать границы
        .Range("B4").Borders.LineStyle = xlDash
        .Range("B4").Borders.Color = rgbBlueViolet

    End With

End Sub

Основные моменты

Ниже приводится краткое изложение основных моментов

  1. Range возвращает диапазон ячеек
  2. Cells возвращают только одну клетку
  3. Вы можете читать из одной ячейки в другую
  4. Вы можете читать из диапазона ячеек в другой диапазон ячеек.
  5. Вы можете читать значения из ячеек в переменные и наоборот.
  6. Вы можете читать значения из диапазонов в массивы и наоборот
  7. Вы можете использовать цикл For Each или For, чтобы проходить через каждую ячейку в диапазоне.
  8. Свойства Rows и Columns позволяют вам получить доступ к диапазону ячеек этих типов

Combining values with CONCATENATE is the best way, but with this function, it’s not possible to refer to an entire range.

You need to select all the cells of a range one by one, and if you try to refer to an entire range, it will return the text from the first cell.

In this situation, you do need a method where you can refer to an entire range of cells to combine them in a single cell.

So today in this post, I’d like to share with you 5 different ways to combine text from a range into a single cell.

[CONCATENATE + TRANSPOSE] to Combine Values

The best way to combine text from different cells into one cell is by using the transpose function with concatenating function.

Look at the below range of cells where you have a text but every word is in a different cell and you want to get it all in one cell.

Below are the steps you need to follow to combine values from this range of cells into one cell.

  1. In the B8 (edit the cell using F2), insert the following formula, and do not press enter.
    • =CONCATENATE(TRANSPOSE(A1:A5)&” “)
  2. Now, select the entire inside portion of the concatenate function and press F9. It will convert it into an array.
  3. After that, remove the curly brackets from the start and the end of the array.
  4. In the end, hit enter.
concatenate a range of cells with concatenate transpose

That’s all.

How this formula works

In this formula, you have used TRANSPOSE and space in the CONCATENATE. When you convert that reference into hard values it returns an array.

In this array, you have the text from each cell and a space between them and when you hit enter, it combines all of them.

Combine Text using the Fill Justify Option

Fill justify is one of the unused but most powerful tools in Excel. And, whenever you need to combine text from different cells you can use it.

The best thing is, that you need a single click to merge text. Have look at the below data and follow the steps.

  1. First of all, make sure to increase the width of the column where you have text.
  2. After that, select all the cells.
  3. In the end, go to Home Tab ➜ Editing ➜ Fill ➜ Justify.

This will merge text from all the cells into the first cell of the selection.

TEXTJOIN Function for CONCATENATE Values

If you are using Excel 2016 (Office 365), there is a function called “TextJoin”. It can make it easy for you to combine text from different cells into a single cell.

Syntax:

TEXTJOIN(delimiter, ignore_empty, text1, [text2], …)

  1. delimiter a text string to use as a delimiter.
  2. ignore_empty true to ignore blank cell, false to not.
  3. text1 text to combine.
  4. [text2] text to combine optional.

how to use it

To combine the below list of values you can use the formula:

=TEXTJOIN(" ",TRUE,A1:A5)

Here you have used space as a delimiter, TRUE to ignore blank cells and the entire range in a single argument. In the end, hit enter and you’ll get all the text in a single cell.

Combine Text with Power Query

Power Query is a fantastic tool and I love it. Make sure to check out this (Excel Power Query Tutorial). You can also use it to combine text from a list in a single cell. Below are the steps.

  1. Select the range of cells and click on “From table” in data tab.
    4-concatenate-a-range-of-cells-with-power-query-from-table-min
  2. If will edit your data into Power Query editor.
    5-concatenate-a-range-of-cells-with-power-query-editor-min
  3. Now from here, select the column and go to “Transform Tab”.
  4. From “Transform” tab, go to Table and click on “Transpose”.
    6-concatenate-a-range-of-cells-with-power-query-transpose-min
  5. For this, select all the columns (select first column, press and hold shift key, click on the last column) and press right click and then select “Merge”.
    8-concatenate-a-range-of-cells-with-power-query-merge-cells-min
  6. After that, from Merge window, select space as a separator and name the column.
    9-concatenate-a-range-of-cells-with-power-query-new-column-name-min
  7. In the end, click OK and click on “Close and Load”.

Now you have a new worksheet in your workbook with all the text in a single cell. The best thing about using Power Query is you don’t need to do this setup again and again.

When you update the old list with a new value you need to refresh your query and it will add that new value to the cell.

VBA Code to Combine Values

If you want to use a macro code to combine text from different cells then I have something for you. With this code, you can combine text in no time. All you need to do is, select the range of cells where you have the text and run this code.

 Sub combineText()
 Dim rng As Range
 Dim i As String
 For Each rng In Selection
 i = i & rng & " "
 Next rng
 Range("B1").Value = Trim(i)
 End Sub 

Make sure to specify your desired location in the code where you want to combine the text.

In the end,

There may be different situations for you where you need to concatenate a range of cells into a single cell. And that’s why we have these different methods.

All methods are easy and quick, you need to select the right method as per your need. I must say that give a try to all the methods once and tell me:

Which one is your favorite and worked for you?

Please share your views with me in the comment section. I’d love to hear from you, and please, don’t forget to share this post with your friends, I am sure they will appreciate it.

Понравилась статья? Поделить с друзьями:
  • Excel adding not working
  • Excel adding drop downs
  • Excel adding columns to a table
  • Excel addin in vba
  • Excel add to table vba