+ Reply to Thread
Results 1 to 18 of 18

How to concatenate a variable with a Range

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-13-2007
    Location
    Petersfield, Hampshire, UK
    MS-Off Ver
    MS Office for Mac ver 16
    Posts
    135

    How to concatenate a variable with a Range

    Following advice from this forum I have developed the following code to save a workbook with a filename made up from Ranges within a worksheet. What I had also hoped to do was include the first 2 letters of a Range.

    The code that works is as follows
    Private Sub SaveFile_Click()
    '
    ' SaveFile Macro
    '
    Dim Dt As Date
    Dim Ex As String
    
    Dt = Range("StartDate").Value
    Ex = Left(ExIncOp, 2)
    '
            If IsEmpty(Worksheets("Operation").Range("ExIncOp")) Then
        Msg = MsgBox("Please enter TYPE in cell C6 to continue ", vbOKOnly)
            End If
            
            If IsEmpty(Worksheets("Operation").Range("Name")) Then
        Msg = MsgBox("Please enter NAME in cell H6 to continue ", vbOKOnly)
            End If
        
            If IsEmpty(Worksheets("Operation").Range("StartDate")) Then
        Msg = MsgBox("Please enter DATE FROM in cell E10 to continue", vbOKOnly)
            
    Exit Sub
            End If
    
        ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" &  _
    Range(Debrief_Folder) & "\" & Range("Name").Value & " " & Format _
    (Dt, "dd-mmm-yy") & ".xls", FileFormat:=xlNormal
    
    End Sub
    This code includes the line
    Ex = Left(ExIncOp, 2)
    which I believe creates a variable Ex made up of the first 2 characters of the Range ExIncOp.
    How do I then introduce this variable into the ActiveWorkbook line?
    I have tried
    Range("Ex").Value
    but I realise Ex is not a Range so this fails.
    Geoff Culbertson
    Petersfield, UK

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Geoff,

    If ExIncOp is the name of Range then you need to change your code to this...
      Ex = Left("ExIncOp", 2)
    Left requires 2 arguments: a literal string or string variable, and the number of characters to return.

    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    02-28-2008
    Posts
    6
    Try to explain again what you're trying to achieve; perhaps with example input/output.

  4. #4
    Forum Contributor
    Join Date
    11-13-2007
    Location
    Petersfield, Hampshire, UK
    MS-Off Ver
    MS Office for Mac ver 16
    Posts
    135
    Hi Leith / cremate,

    I had already found the error in lack of " " and the code as follows works
    Private Sub SaveFile_Click()
    '
    ' SaveFile Macro
    '
    Dim Dt As Date
    Dim Ex As String
    
    Dt = Range("StartDate").Value
    Ex = Left("ExIncOp", 2)
    
            If IsEmpty(Worksheets("Operation").Range("ExIncOp")) Then
        Msg = MsgBox("Please enter TYPE in cell C6 to continue ", vbOKOnly)
            End If
            
            If IsEmpty(Worksheets("Operation").Range("Name")) Then
        Msg = MsgBox("Please enter NAME in cell H6 to continue ", vbOKOnly)
            End If
        
            If IsEmpty(Worksheets("Operation").Range("StartDate")) Then
        Msg = MsgBox("Please enter DATE FROM in cell E10 to continue", vbOKOnly)
            
    Exit Sub
            End If
    
         ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & Range _
         ("Debrief_Folder").Value & "\" & Range("Name"). _
         Value & " " & Format(Dt, "dd-mm-yy") & ".xls", FileFormat:=xlNormal
    '     & Range("Ex").Value
        
    End Sub
    But if I include the highlighted red code as follows
         ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & Range _
         ("Debrief_Folder").Value & "\" & Range("Ex").Value & Range("Name"). _
         Value & " " & Format(Dt, "dd-mm-yy") & ".xls", FileFormat:=xlNormal
    it fails even if I take the " " from around Ex

    Regards

  5. #5
    Forum Contributor
    Join Date
    11-13-2007
    Location
    Petersfield, Hampshire, UK
    MS-Off Ver
    MS Office for Mac ver 16
    Posts
    135

    Save a file with filename from Ranges in worksheet

    I have a template worksheet where the user enters data in three cells which are also named Ranges.

    I have a button on the sheet which should save the file with the first two letters of the first Range, a space, the second Range, a space, and the third Range.

    The file name should look something like In Test 29-Feb-08.xls.

    The code I have works without the first two letters of the first Range.
    Private Sub SaveFile_Click()
    '
    ' SaveFile Macro
    '
    Dim Dt As Date
    Dim Ex As String
    
    Dt = Range("StartDate").Value
    Ex = Left("ExIncOp", 2)
    
            If IsEmpty(Worksheets("Operation").Range("ExIncOp")) Then
        Msg = MsgBox("Please enter TYPE in cell C6 to continue ", vbOKOnly)
            End If
            
            If IsEmpty(Worksheets("Operation").Range("Name")) Then
        Msg = MsgBox("Please enter NAME in cell H6 to continue ", vbOKOnly)
            End If
        
            If IsEmpty(Worksheets("Operation").Range("StartDate")) Then
        Msg = MsgBox("Please enter DATE FROM in cell E10 to continue", vbOKOnly)
            
    Exit Sub
            End If
      
         ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & Range _
         ("Debrief_Folder").Value & "\" & Range("Name"). _
         Value & " " & Format(Dt, "dd-mm-yy") & ".xls", FileFormat:=xlNormal
    
        
    
    End Sub
    I would like to add those two letters to the file name by replacing the red code with something like
    ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & Range _
         ("Debrief_Folder").Value & "\" & Range("Ex").Value & " " & Range("Name"). _
         Value & " " & Format(Dt, "dd-mm-yy") & ".xls", FileFormat:=xlNormal
    The magenta code is my addition, which does not work
    Can anyone see the error?

    Thanks in advance

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Geoff,

    In your code the variable EX is a string. If I understand what your are trying to do, you want the first 2 letters of the value stored in the Named Range "ExIncOp" to the file title. The changes are in blue.
    Private Sub SaveFile_Click()
    '
    ' SaveFile Macro
    '
    Dim Dt As Date
    Dim Ex As String
    
    Dt = Range("StartDate").Value
    Ex = Left(Range("ExIncOp").Text, 2)
    
            If IsEmpty(Worksheets("Operation").Range("ExIncOp")) Then
        Msg = MsgBox("Please enter TYPE in cell C6 to continue ", vbOKOnly)
            End If
            
            If IsEmpty(Worksheets("Operation").Range("Name")) Then
        Msg = MsgBox("Please enter NAME in cell H6 to continue ", vbOKOnly)
            End If
        
            If IsEmpty(Worksheets("Operation").Range("StartDate")) Then
        Msg = MsgBox("Please enter DATE FROM in cell E10 to continue", vbOKOnly)
            
    Exit Sub
            End If
      
    ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & Range _
         ("Debrief_Folder").Value & "\" & EX & " " & Range("Name"). _
         Value & " " & Format(Dt, "dd-mm-yy") & ".xls", FileFormat:=xlNormal
        
    End Sub
    Sincerely,
    Leith Ross

  7. #7
    Forum Contributor
    Join Date
    11-13-2007
    Location
    Petersfield, Hampshire, UK
    MS-Off Ver
    MS Office for Mac ver 16
    Posts
    135

    Error message Range of object failed

    Leith,

    Thanks for your response, which made complete sense to me and you understood exactly what I required, however I still get the following error

    Run-time eror '1004'
    Method 'Range' of object '_Worksheet' failed

    I attach the .xlt file and hope you can see what is going wrong. If you cannot, I will be happy without the first 2 letters of Range 'ExIncOp'!

    Regards
    Attached Files Attached Files

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Geoff,

    Your file has only one worksheet named "Operation". The other two worksheets "Chronology" and "Post Event" are missing. The error occurs because CmdBtn3 is supposed to be located on the worksheet "Chronology" which is missing. You need the other sheets before the command button initialization will work correctly.

    Sincerely,
    Leith Ross

  9. #9
    Forum Contributor
    Join Date
    11-13-2007
    Location
    Petersfield, Hampshire, UK
    MS-Off Ver
    MS Office for Mac ver 16
    Posts
    135
    Hello Leith,

    I was trying to keep the file size small and forgot about the other links!

    Here is the Workbook with all the sheets.

    Thanks for your help, again
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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