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
 

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

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,701
Office Version
  1. 365
Platform
  1. Windows
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")
 

Watch MrExcel Video

Forum statistics

Threads
1,123,337
Messages
5,601,026
Members
414,422
Latest member
acegreen

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