+ Reply to Thread
Results 1 to 7 of 7

How to write a macro to concatenate 2 specific columns into another specific column

  1. #1
    Registered User
    Join Date
    05-27-2018
    Location
    Utah
    MS-Off Ver
    MS Office 2016
    Posts
    3

    How to write a macro to concatenate 2 specific columns into another specific column

    Hey guys I have been working on some code to concatenate 2 columns with data into another column. I want to be able to have a macro that is tied to a button that when I press the button it automatically concatenates 2 specific columns(Must be able to specify which columns in the code) into another specified column. For example, I would like to have a macro that concatenates column C2+M2 and gives me the values of that into O2. I'm kind of new to Excel programming and trying to learn how to use it for work purposes. I currently have a macro set up that does something similar but it is not set for specific columns, it only has a starting column, which will then concatenate the column to the right and give me the values into the following column if that makes sense. Here is the code I have so far:

    Sub concatecells()
    Dim scell As Range
    Set scell = Range("C2") '<==set start cell here.
    Dim os As Double: os = 0
    Do Until scell.Offset(os, 0) = ""
    scell.Offset(os, 2).Value = scell.Offset(os, 0).Value & " " & scell.Offset(os, 1).Value
    os = os + 1
    Loop
    End Sub

    Any help would be greatly appreciated.

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: How to write a macro to concatenate 2 specific columns into another specific column

    try this. If it is what you want, then you can attach it to a Form Controls command button.
    Please Login or Register  to view this content.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Registered User
    Join Date
    05-27-2018
    Location
    Utah
    MS-Off Ver
    MS Office 2016
    Posts
    3

    Re: How to write a macro to concatenate 2 specific columns into another specific column

    This seems to work! Although I am looking for a macro that would automatically concatenate the two columns and have a designated location for the values. I attached a picture as an example. For column C and D the button would automatically concatenate those to columns and designate those values in column E. I would also like to have another button(Probably would require multiple modules for each macro and I am worried about it slowing down the template) That would concatenate Column C and G and output those values in Column H for example. Sorry if this is confusing. Your code works very well but I am trying to create a template that has a button assigned to different macros that are specific for concatenating certain columns into other specific columns.

    Screen Shot 2018-05-27 at 3.00.27 PM.png

  4. #4
    Registered User
    Join Date
    05-27-2018
    Location
    Utah
    MS-Off Ver
    MS Office 2016
    Posts
    3

    Re: How to write a macro to concatenate 2 specific columns into another specific column

    Also does the column entry have to be numeric? Or could I have it ask me for the alphabetical figure for that specific column? Thanks again.

  5. #5
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: How to write a macro to concatenate 2 specific columns into another specific column

    Or similar but different.
    Please Login or Register  to view this content.
    Click anywhere in the first column for the first inputbox, anywhere in the second column for the second inputbox and anywhere in the column where you want the result for the third inputbox.
    You can run the same macro for as many concatenations as you want.

  6. #6
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: How to write a macro to concatenate 2 specific columns into another specific column

    Quote Originally Posted by Rechirax View Post
    Also does the column entry have to be numeric? Or could I have it ask me for the alphabetical figure for that specific column? Thanks again.
    VBA is not a clairvoyant software. It still has to be told by the code writer where to execute the code. You can put a button anywhere on the sheet, but it does not guarantee that the code will execute in the particular row or column where the button is located. That is why the input boxes were provided. With the input boxes you only need one button and the user can then designate the columns. If you want to use Alpha characters for the column designation, then on each if the lines for the inputboxes, remove the word 'Application' and ', Type:=1', to look like this
    Please Login or Register  to view this content.
    . Note also the wording change for the input box. You will also need to change the declaration for each column variable from 'Long' to 'String'. You should make similar wording changes to the other two lines of code.
    Last edited by JLGWhiz; 05-28-2018 at 02:34 PM.

  7. #7
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,072

    Re: How to write a macro to concatenate 2 specific columns into another specific column

    The site won't let me post the code so I've uploaded a file
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 8
    Last Post: 09-20-2017, 02:09 AM
  2. Auto filter find specific name and write next column its name through macro
    By kuldeepchoudhary in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-23-2016, 12:07 AM
  3. [SOLVED] macro to copy specific columns and rows for a specific month and week
    By Shellybelly in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-23-2015, 11:23 AM
  4. Function to write Data to DB from specific rows and colomuns Plus specific Timestamp
    By Mudhafar.M in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-18-2013, 08:01 PM
  5. Replies: 8
    Last Post: 04-04-2013, 08:02 PM
  6. [SOLVED] Macro code to Copy specific rows into specific columns
    By macrofan2012 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-11-2012, 11:24 AM
  7. Macro to copy specific data to specific columns from a pivot table
    By Raju Radhakrishnan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-09-2012, 07:24 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1