Solved

how to reference a range in an other excel file

Posted on 2015-02-06
6
36 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Macro to convert table into pivot tables 25 72
Excel 2007 Random Generate List Based on Defined Source Cells 3 32
MS Access vba Print  PDF First page only 16 35
Vlookup Help 3 30
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

710 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