Insert value from cell into macro

FearofExcel

New Member
Joined
Jan 31, 2014
Messages
1
Hi All,

I'm currently creating a report where I have a macro that pulls info from a workbook and pastes it into another when prompted by the user. This code looks like this:

Code:
Sub Button()



Dim x As Workbook
Dim y As Workbook

Set x = Workbooks.Open("C:\Users\myusername\documents\targetfilename.xls")
Set y = Workbooks.Open("C:\Users\myusername\documents\destinationfilename.xls")

x.Worksheets("Sheet1").Range("A13:I28").Copy

y.Sheets("Sheet1").Range("A13:I28").PasteSpecial Paste:=xlPasteValues

Application.CutCopyMode = False

x.Close


End Sub

It works fine for me, the problem arises when another user tries to run it on their system as obviously the username changes rendering the pathways above invalid.

A solution I have thought of involves having the user enter their username as a value in a cell and having this value inserted into pathway in the VBA code.

For example if the user entered their username in cell A1, the pathway in the macro would update to "C:\Users\CellValue\documents\destinationfilename.xls".

Is there any way I can do this?

This is the first taste of programming I've had and I have to say the buzz you get when something works is awesome, but this problem is causing me a headache ha ha.

Thanks
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Welcome to the board. You can break up and down strings in VBA, so you could use something like:
Code:
Const sRoot as String = "C:\Users\"
Dim sUser as String
Dim x as Workbook

sUser = Range("A1").Value
Set x = Workbooks.Open(sRoot & sUser & "documents\destinationfilename.xls")
 
Upvote 0

Forum statistics

Threads
1,214,807
Messages
6,121,679
Members
449,047
Latest member
notmrdurden

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