Solved

how to reference a range in an other excel file

Posted on 2015-02-06
6
30 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:
SimonAdept earned 500 total points
Comment Utility
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
Comment Utility
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:SimonAdept
Comment Utility
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 45

Expert Comment

by:Martin Liss
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now