Solved

how to reference a range in an other excel file

Posted on 2015-02-06
6
35 Views
Last Modified: 2016-02-12
I have a function I need to call from an outside excel spreadsheet
so the code I have is

 
Sub CleanAll()
    Dim rng As Range

    For Each rng In Sheets("Sheet1").Range("A1:K1500").Cells 'adjust sheetname and range accordingly
        rng.Value = AlphaNumericOnly(rng.Value)
    Next
End Sub

Open in new window


is it possible to reference sheet1 in file "c:\temp\myexcelfile.xls"

thanks in advance
0
Comment
Question by:damixa
  • 2
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
Simon earned 500 total points
ID: 40594781
This works if you want to open and update the target workbook.

Sub CleanAll()
    Dim wb as workbook
    Dim rng As Range
Set wb = Application.Workbooks.Open ("c:\temp\myexcelfile.xls")
with wb
    For Each rng In .Sheets("Sheet1").Range("A1:K1500").Cells 'adjust sheetname and range accordingly
        rng.Value = AlphaNumericOnly(rng.Value)
    Next
end with
End Sub

Open in new window

0
 

Author Comment

by:damixa
ID: 40594790
Do you know if I can do this if I am in access? so run this code from an access application?

thanks
0
 
LVL 18

Expert Comment

by:Simon
ID: 40594802
Yes, from Access you'd have to create an object reference to the Excel application. If you add a reference to Excel from the Access VBE using Tools/References you can still use intellisense. I can't test this from where I'm sitting, so the code below may need minor tweaks...

e.g.
Sub CleanAll()
dim objExcel as excel.application
   Dim wb as excel.workbook
    Dim rng As excel.Range
set objExcel= CreateObject("Excel.Application")
Set wb = objExcel.Workbooks.Open ("c:\temp\myexcelfile.xls")
with wb
    For Each rng In .Sheets("Sheet1").Range("A1:K1500").Cells 'adjust sheetname and range accordingly
        rng.Value = AlphaNumericOnly(rng.Value)
    Next
end with
End Sub

Open in new window

0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40640130
I've requested that this question be closed as follows:

Accepted answer: 500 points for SimonAdept's comment #a40594802

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
 

Author Comment

by:damixa
ID: 40640129
Thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question