Ampersand issue in VBA

nyconfidential

New Member
Joined
Jul 22, 2015
Messages
46
Office Version
  1. 365
  2. 2016
Hi all - I've been encountering an issue with workbooks that contain an ampersand. If i do something like try to copy data from one open workbook to another in VBA and the workbook is named something like "M&M Box Company.xlsm", I will get a "Subscript out of range" error, because the name is not getting read correctly and it thinks the workbook im referencing does not exist. If I change the workbook name to "MM Box Company.xlsm", code works fine. Any idea what I should do to correct this problem? (other than having to remove the ampersand from the file name each time, obviously not an ideal solution). Thanks!
 

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

offthelip

Well-known Member
Joined
Dec 23, 2017
Messages
1,732
Office Version
  1. 2010
Platform
  1. Windows
you could try setting the company name into a variable like this:
VBA Code:
filn = "M" & Chr(38) & "M Box Company.xlsm"
MsgBox filn
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,977
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
@nyconfidential are you on a Mac or Windows machine? Asking because the below works fine for me on a Windows machine...

VBA Code:
ThisWorkbook.Sheets("Sheet1").Range("A1:C20").Copy Workbooks("M&M Box Company.xlsb").Sheets("Sheet5").Cells(1, 1)
 

nyconfidential

New Member
Joined
Jul 22, 2015
Messages
46
Office Version
  1. 365
  2. 2016
Thanks Mark (and offthelip) - I'm actually on Microsoft Surface - the line of code actually works on some Windows machines and not on others, see below...:confused:

c.value = Workbooks(wrkbkLastYear).Sheets(wrkSheet.Name).Range(c.Address).Value
 

Watch MrExcel Video

Forum statistics

Threads
1,127,570
Messages
5,625,579
Members
416,119
Latest member
JCLLE

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
Top