VBA Replace

Ezguy4u

Active Member
Joined
Feb 10, 2010
Messages
336
Office Version
  1. 365
Platform
  1. Windows
So I just want to join the letters in column B to the numbers in columns C, D & E. See examples below. I would like to use the VBA code below but the .Value line gives me a list and separator error. It could be the syntax or maybe I am just doing it all wrong. Any help would be appreciated and thank you for your time.
VBA Code:
Sub test3B()

With Range("C1:E2")

.Value = Replace("Range("B1:B2")" & "&@", "@", .Address)

  End With

End Sub

Input
06 Replace Evaluate If rev a.xlsm
BCDE
1A123
2B456
3C789
Count Rows Col workin 1

Output
06 Replace Evaluate If rev a.xlsm
BCDE
1AA1A2A3
2BB4B5B6
3CC7C8C9
Count Rows Col workin 1
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
One way:
VBA Code:
Sub add()
Dim cell As Range
    For Each cell In Range("C1:E3")
        cell.Value = Cells(cell.Row, "B") & cell
    Next
End Sub
 
Upvote 0
Give this non-looping macro a try...
VBA Code:
Sub Ezguy4u()
  Dim RngToModify As Range
  With Range("B1").CurrentRegion
    Set RngToModify = .Offset(, 1).Resize(, .Columns.Count - 1)
    RngToModify = Evaluate(.Columns(1).Address & "&" & RngToModify.Address)
  End With
End Sub
 
Upvote 0
Solution
Try just making this change to your existing code - won't then matter if there is data in column A (or column F)

Rich (BB code):
Sub test3B()
  With Range("C1:E2")
    .Value = Replace(Range("B1:B2")" & "&@", "@", .Address)
    .Value = Evaluate(.Columns(0).Address & "&" & .Address)
  End With
End Sub

.. though you would also need to change from "C1:E2" to "C1:E3" to process all three rows. ;)
 
Upvote 0
These are all great solutions to my problem. I like bebo because it is simple and effective. The looping does take some time to run. Peter, your one line of code is amazing. I really like Rick’s because I want to keep learning VBA techniques and syntax. Plus it looks really professional.
Thank you all
Arnold
 
Upvote 0
You're welcome. Glad we could help. :)

I assume then that you do not have any data in column A? If you do, the code that you have marked as the solution will not produce the results you have requested.
 
Upvote 0
Peter, you are absolutely correct. It wouldn’t work if there was data in column A. But part of why I use Mr. Excel is to learn and experiment with the solutions. So if I modify the line, With Range("B1").CurrentRegion, to With Range("A1").CurrentRegion, it should work.
 
Upvote 0
Only if you are trying to combine what is in column A with the other columns, not if you are trying to combine column B with the other columns to the right as stated in your question.
 
Upvote 0
This rewrite of my code will avoid touching Column A's content...
VBA Code:
Sub Ezguy4u()
  Dim LastRow As Long, LastCol As Long
  LastRow = Cells(Rows.Count, "B").End(xlUp).Row
  LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
  Range("C1", Cells(LastRow, LastCol)) = Evaluate("B1:B" & LastRow & "&" & Range("C1", Cells(LastRow, LastCol)).Address)
End Sub
 
Upvote 0
Peter, the first assumption is accurate, there is no data in column A. Part of my learning process is to present problems with as many different solutions as reasonable. I have to remember that how I describe the problem, means how it affects the solution. I always find it interesting how sometimes when I read a solution, that it never accorded to me that’s what they meant. Or I get that Oh, that answer to the problem never occurred to me. So, I would say this all starts with the problem, written out. I just have to be more precise in my description. Rick, I always enjoy reading your input whether it is mine or other people’s problems. Thank you all for your time and patience.
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,977
Members
449,200
Latest member
Jamil ahmed

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