Consulting

Results 1 to 3 of 3

Thread: Solved: Cell Value As String

  1. #1
    VBAX Regular
    Joined
    Mar 2011
    Posts
    52
    Location

    Solved: Cell Value As String

    I have got a script for getting Data from a closed workbook, however, this code gets the data from a "static" filename and I want to have it as a variable. I.e. I want to us a cell value as filename.

    Can somebody give the solution?

    ************************************************
    Option Explicit

    Sub GetDataDemo()

    Dim FilePath$, Row&, Column&, Address$

    'change constants & FilePath below to suit
    '**************************************

    Const FileName$ = "TEST1.xls"
    Const SheetName$ = "Blad1"
    'Const NumRows& = 10
    'Const NumColumns& = 10
    FilePath = ActiveWorkbook.path & "\"
    '***************************************

    DoEvents
    Application.ScreenUpdating = False
    If Dir(FilePath & FileName) = Empty Then
    MsgBox "The file " & FileName & " was not found", , "File Doesn't Exist"
    Exit Sub
    End If
    ' For Row = 1 To NumRows
    ' For Column = 1 To NumColumns
    Address = Cells(2, 1).Address
    Cells(2, 2) = GetData(FilePath, FileName, SheetName, Address)
    Address = Cells(2, 2).Address
    Cells(2, 3) = GetData(FilePath, FileName, SheetName, Address)
    ' Columns.AutoFit
    ' Next Column
    ' Next Row
    ActiveWindow.DisplayZeros = False
    End Sub


    Private Function GetData(path, file, sheet, Address)
    Dim Data$
    Data = "'" & path & "[" & file & "]" & sheet & "'!" & Range(Address).Range("A1").Address(, , xlR1C1)
    GetData = ExecuteExcel4Macro(Data)
    End Function

    *************************************

    The file name "Test1.xls" has got to be the following cell Range("A1").select

  2. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    change :
    [vba]
    Dim FileName$
    FileName$ = Range("A1")

    [/vba]

  3. #3
    VBAX Regular
    Joined
    Mar 2011
    Posts
    52
    Location
    Thanks for the quick reply!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •