VBA: copy + paste text of adjacent cell

lesternope

New Member
Joined
Jul 31, 2017
Messages
4
Hello,

I have a large table, in which I need to append to every line of column B (except the first) a comma + space key, and then add the contents of adjacent cell A right next to it.

so basically I want to go from this kind of table:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]A
[/TD]
[TD]B
[/TD]
[/TR]
[TR]
[TD]Mr.[/TD]
[TD]Smith, John[/TD]
[/TR]
[TR]
[TD]Mrs. [/TD]
[TD]Miller, Betty[/TD]
[/TR]
</tbody>[/TABLE]

to this kind of table

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]A
[/TD]
[TD]B
[/TD]
[/TR]
[TR]
[TD]Mr.[/TD]
[TD]Smith, John, Mr.[/TD]
[/TR]
[TR]
[TD]Mrs.[/TD]
[TD]Miller, Betty, Mrs.[/TD]
[/TR]
</tbody>[/TABLE]

anyone an idea on it? Thanks in advance!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Welcome to the board. Try:
Code:
Sub Append()

    Dim x       As Long
    Dim arr()   As Variant
    
    x = Cells(Rows.Count, 1).End(xlUp).row
    arr = Cells(2, 1).Resize(x - 1, 2).Value
    
    For x = LBound(arr, 1) To UBound(arr, 2)
        arr(x, 2) = arr(x, 2) & ", " & arr(x, 1)
    Next x
        
    Cells(2, 1).Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
    Erase arr
    
End Sub
 
Last edited:
Upvote 0
Welcome to the board. Try:
Code:
Sub Append()

    Dim x       As Long
    Dim arr()   As Variant
    
    x = Cells(Rows.Count, 1).End(xlUp).row
    arr = Cells(2, 1).Resize(x - 1, 2).Value
    
    For x = LBound(arr, 1) To UBound(arr, 2)
        arr(x, 2) = arr(x, 2) & ", " & arr(x, 1)
    Next x
        
    Cells(2, 1).Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
    Erase arr
    
End Sub

Thanks jack, this basically does as it is supposed to do, except, the original file has more than 2 rows and this code only works on those first 2. I'm not sure which numbers in the code to amend in order to get it to work on all rows of col B. Any pointers appreciated!
 
Upvote 0
This line in the code finds the row number of the last filled cell in column A:
Rich (BB code):
x = Cells(Rows.Count, 1).End(xlUp).row
Your reply suggests there is no more data after A3 in the csv sheet... The blue 1 is the column number it tries to find the last row on, i.e. 1 = column A, 2 = column B etc.
 
Last edited:
Upvote 0
This line in the code finds the row number of the last filled cell in column A:
Rich (BB code):
x = Cells(Rows.Count, 1).End(xlUp).row
Your reply suggests there is no more data after A3 in the csv sheet... The blue 1 is the column number it tries to find the last row on, i.e. 1 = column A, 2 = column B etc.


that's weird, since actually each row in A has a value (just as with B). The debugger also gives out x=2, which doesn't really make sense looking at the spreadsheet, as the current one I'm working at has 460 rows with text inside. you don't happen to know a snippet I could use in order to just manually enter the number of lines I want the macro to look at (unless that messes with the other parts of the code)?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top