Ampersand issue in VBA

nyconfidential

New Member
Joined
Jul 22, 2015
Messages
49
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!
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
you could try setting the company name into a variable like this:
VBA Code:
filn = "M" & Chr(38) & "M Box Company.xlsm"
MsgBox filn
 
Upvote 0
@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)
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,208
Members
448,554
Latest member
Gleisner2

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