+ Reply to Thread
Results 1 to 2 of 2

Assigning a cell value to a variable - between different workbooks

Hybrid View

  1. #1
    Registered User
    Join Date
    01-18-2016
    Location
    Portugal
    MS-Off Ver
    MS Office 2013
    Posts
    1

    Post Assigning a cell value to a variable - between different workbooks

    How, without opening the source workbook, copy the contents of a cell and put it in a variable in the destination workbook?

    Example:

    valueX = "='c:\code\[example.xlsx]Sheet1'!R2C1"
    MsgBox (valueX)
    The result is ='c:\code\[example.xlsx]Sheet1'!R2C1
    and not the value XPTO

    If I assign the reference to a cell directly, it work:

    Range("A1").Value="='c:\code\[example.xlsx]Sheet1'!R2C1"
    MsgBox(Range("A1").Value)
    The result is XPTO

  2. #2
    Valued Forum Contributor Gatti's Avatar
    Join Date
    06-08-2015
    Location
    Brasil, São Paulo, Ribeirão Preto
    MS-Off Ver
    365
    Posts
    346

    Re: Assigning a cell value to a variable - between different workbooks

    Hello...

    if you do:

    valueX = "='c:\code\[example.xlsx]Sheet1'!R2C1"
    The valueX will be a string with the whole path assigned, not the reference.

    Try this way:

    Sub ReferCellClosedWbk()
        
      Dim txt As String
      Dim wb As Workbook
      Dim ws As Worksheet
      
      Application.ScreenUpdating = False
      
      txt = ThisWorkbook.Path & "\example.xlsx"
      
      If Dir(txt) = "" Then
          MsgBox txt & " does not exist"
      Else
          Set wb = Workbooks.Open(txt)
          On Error Resume Next
          Set ws = wb.Sheets("Sheet1")
          If Not ws Is Nothing Then
              MsgBox ws.Range("a1")
              wb.Close
          Else
              MsgBox ws.Name & " does not exist"
          End If
          Err.Clear
      End If
      
      Application.ScreenUpdating = True
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 12
    Last Post: 02-15-2015, 05:41 PM
  2. [SOLVED] Assigning A Cell Address to A Variable, Using A Variable - Error '1003'
    By ch_abs in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-28-2014, 01:36 PM
  3. Assigning a cell to a variable
    By penfold1992 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-16-2013, 11:12 AM
  4. Assigning Value to Variable, and Show Value of that Variable
    By torofish in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-19-2010, 05:42 AM
  5. Assigning a variable and pasting variable to last unused column.
    By Ageia in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 11-20-2008, 05:06 PM
  6. Assigning a min to a variable
    By Ed in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-03-2006, 11:30 AM
  7. Assigning a variable a value from a Cell in VBA
    By jefflck@comcast.net in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-15-2006, 02:30 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1