VBA code that references a worksheet [given URL] , then copies and pastes desired range

macrolearning

New Member
Joined
Oct 13, 2014
Messages
1
Hi everybody,

First of all, thank you all for your previous collaboration. I am currently engaging more with Excel [2010] and VBA, and with the stumbles along the way, your answers to other people's questions have been great answers to my questions as well. Usually with the Record Macro feature and all of your advice, I manage to learn some new VBA and get some nice projects done. I wouldn't have created this account if it wasn't because I could not find this topic discussed anywhere:


I am working with a lot of worksheets that have a heavy amount of formulas, and the intention is to create a macro that finds a workbook, and copies and pastes a range into another.

Reading the file location URL as an input in a cell is my greatest struggle. Is this possible with VBA? Or does the explicit name of the workbook have to be placed into VBA each time and not into a cell?



TLDR; VBA copy and paste from a new workbook mentioned in a cell from range defined on cells.

Here is an example image in case this explains my problem better:

TemplateExample
File LocationFile Location
[URL OF FILE WOULD GO HERE]C:\Users\Public\Pictures\Sample Pictures
Range DesiredRange Desired
From ColumnFrom RowTo ColumnTo RowFrom ColumnFrom RowTo ColumnTo Row
[Identifying numbers/letters go here]B5M10

<tbody>
</tbody>


Thanks ahead of time, guys!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
The first thing is to use terminology that applies to Excel. URL is an internet term which stands for "Uniform Resource Locator" and applies to such things a site addresses or email addresses. I think you might be using it here in reference to "Path" which is a Parent child sequence from Drive, Directory and subdirectories hierarchical order that defines where a file resides. If you are dealing with only one path, then you can simply define it once in your code as:
example:
Code:
myPath = ThisWorkbook.Path 'If all workbooks in the same directory as the one which hosts the code.
Or
Code:
fPath = "C:\DeskTop\xlFiles\myProjects" 'If different than the host workbook.
The 'myPath' and 'fPath' variable names bear no significance. You can use any name you want in either place, since it is simply a device to hold the string value of the actual path.
If you have more than one directory to use, then you can either list them with their corresponding file names in worksheet columns and then refer to them in code. Assume file names in Column A and paths in Column B for five files and five directories.
Code:
For i = 1 To 5
 With ActiveSheet
  myFile = .Cells(i, 1).Value 'get file name from col A
  myPath = .Cells(i, 2).Value 'get path from col B
  Workbooks.Open(myPath & "\" & myFile) 'concatenate variables with separator inserted to open files.
  'Code to do stuff here
  ActiveWorkbook.Close True
 End With
Next
For more discussion on the subject, you can switch over to the discussions forum or browse the Q&A page, which also has numerous references to this subject.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,269
Members
449,075
Latest member
staticfluids

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