VBA combining cell values into text string

winstela

New Member
Joined
Feb 24, 2019
Messages
28
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I am trying to add a formula in VBA that works in an excel sheet but not when I use the same formula in VBA

I am trying to combine cell value AC2 and AC1 separated with a dash and copy this to the last column

But I keep getting Runtime error 13 Type mismatch


VBA Code:
Range("AC3:BB3").Formula = "=AC2&" - "&AC1"

Excel formula I am using

Excel Formula:
=AC2&"-"&AC1

Screenshot 2022-09-29 081446.png


Any ideas where I am going wrong?

Thank you
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try this.

Excel Formula:
Range("AC3:BB3").Formula = "=AC2&" & """ - """ & "&AC1"
 
Upvote 0
Thanks for your suggestion

When I add it to my codeI am receiving Runtime Error

Screenshot 1 2022-09-29 102204.png


my full code is

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:BB3")) Is Nothing Then ' watch all the cells in this range
    'For Each cel In Target ' do the next steps for each cell that was changed
Range("A1:BB3").UnMerge


End If
Range("AC3:BB3").Formula = "=AC2&" & """ - """ & "&AC1"
End Sub


If I debug below code line is highlighted

VBA Code:
Range("AC3:BB3").Formula = "=AC2&" & """ - """ & "&AC1"

If I end the code and review the excel sheet the formula is there, any ideas?
Thanks
 
Upvote 0
You need to double any quotes that are actually part of the formula, so:

VBA Code:
Range("AC3:BB3").Formula = "=AC2&"" - ""&AC1"
 
Upvote 0
I am still getting the error message but formula is being added to the sheet correctly.

Any ideas why?

Thanks
 
Upvote 0
Which error message, and what is your full code?
 
Upvote 0
Here is my code

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)


If Not Intersect(Target, Range("A1:BB3")) Is Nothing Then ' watch all the cells in this range
    'For Each cel In Target ' do the next steps for each cell that was changed
Range("A1:BB3").UnMerge

End If


If Not Intersect(Target, Range("A1:BB3")) Is Nothing Then ' watch all the cells in this range
    'For Each cel In Target ' do the next steps for each cell that was changed
Range("AC3:BB3").Formula = "=AC2&""-""&AC1"

End If
End Sub

The error message is

Error 2022-09-29 130809.png


which when I select Debug the following line is highlighted
VBA Code:
Range("AC3:BB3").Formula = "=AC2&""-""&AC1"

I am copying a full sheet from another excel file and pasting this into the excel file .

Thank you
 
Upvote 0
If you change cells in a change event, you should disable events while you do:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


If Not Intersect(Target, Range("A1:BB3")) Is Nothing Then ' watch all the cells in this range
application.enableevents = false
    'For Each cel In Target ' do the next steps for each cell that was changed
Range("A1:BB3").UnMerge
    'For Each cel In Target ' do the next steps for each cell that was changed
Range("AC3:BB3").Formula = "=AC2&""-""&AC1"
application.enableevents = true
End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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