+ Reply to Thread
Results 1 to 9 of 9

Use cell as filename while saving via VBA

  1. #1
    Registered User
    Join Date
    03-03-2015
    Location
    hoogmade, nederland
    MS-Off Ver
    office 365
    Posts
    6

    Use cell as filename while saving via VBA

    Hello All,

    I am creating a new script with all kind of examples from the web, because I am a beginner in VBA, but I can't get it right.

    Case: Every day I have to create two files from my excel planning sheet, which I save as image on the network. It works now with a hardcopy name, but as I need to copy every day I want to use a cell as part of the filename. This is the script with the hardcoded filename (donderdag 1.jpg) which is working:

    Sub Selectie1()
    '
    ' Selectie1 Macro
    '

    '
    Range("B4:U27").Select
    Dim tmpChart As Chart, n As Long, shCount As Long, sht As Worksheet, sh As Shape
    Dim fileSaveName As Variant, pic As Variant
    'Create temporary chart as canvas
    Set sht = Selection.Worksheet
    Selection.Copy
    sht.Pictures.Paste.Select
    Set sh = sht.Shapes(sht.Shapes.Count)
    Set tmpChart = Charts.Add
    tmpChart.ChartArea.Clear
    tmpChart.Name = "PicChart" & (Rnd() * 10000)
    Set tmpChart = tmpChart.Location(Where:=xlLocationAsObject, Name:=sht.Name)
    tmpChart.ChartArea.Width = sh.Width
    tmpChart.ChartArea.Height = sh.Height
    tmpChart.Parent.Border.LineStyle = 0
    'Paste range as image to chart
    sh.Copy
    tmpChart.ChartArea.Select
    tmpChart.Paste
    'Save chart image to file
    tmpChart.Export FileName:="P:\Planningen\Voor scherm\Donderdag 1.jpg", FilterName:="jpg"
    'Clean up
    sht.Cells(1, 1).Activate
    sht.ChartObjects(sht.ChartObjects.Count).Delete
    sh.Delete
    End Sub

    I want to change Donderdag with the information from cell G4 from the current selected worksheet. So export should be something like:

    'Save chart image to file
    tmpChart.Export FileName:="P:\Planningen\Voor scherm\(g4)-1.jpg", FilterName:="jpg"
    'Clean up

    Any help would be appreciated very much!

    Kind regards,

    Joost

  2. #2
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Use cell as filename while saving via VBA

    if you want to use cell data, just concat it in. instead of this:
    Please Login or Register  to view this content.
    do this:
    Please Login or Register  to view this content.
    if the sheet that has the cell you are target does NOT have the focus or is not the active sheet when the code is run, preceed the RANGE() reference with the sheet object as the qualifying statement. your code already has the book and sheet declared.

  3. #3
    Registered User
    Join Date
    03-03-2015
    Location
    hoogmade, nederland
    MS-Off Ver
    office 365
    Posts
    6

    Re: Use cell as filename while saving via VBA

    Hello Adam,

    Thanks for the quick reply. I replaced my statement and tried to run it on the active sheet, but it gave me the error: Error 1004 during running: Methode Range of object_Global is failed. So probably he couldn't find it. I tried to add the sht as active worksheet but as I am a rookie, It didn't work. So I have 52 different sheets, so I want to use the same cell on each sheet when I run that statement, but the sheetname is always variable.

    So something like:
    tmpChart.Export FileName:="P:\Planningen\Voor scherm\" & RANGE(Activesheet:A1) & ".jpg", FilterName:="jpg"

    Hope u can help me again!

    Thanks

    Kind regards,

    Joost

  4. #4
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Use cell as filename while saving via VBA

    to loop the sheets, do this:
    Please Login or Register  to view this content.
    put that code in your routine where it should go.

  5. #5
    Registered User
    Join Date
    03-03-2015
    Location
    hoogmade, nederland
    MS-Off Ver
    office 365
    Posts
    6

    Re: Use cell as filename while saving via VBA

    Hello Adam,

    Sorry but with this last code it seems to copy the information from the last sheet.

    Probably I explained myself incorrectly.

    I have copied in an example of 1 sheet of my excelfile. I am on the sheet "47 19 DO" and on the right side I placed a button which is linked to the macro. When I press that button it should take the information from this active sheet cell G4 (text "donderdag") and use that as filename of the saved jpg.

    When I go to the next sheet, I have the same button and than it should take the information out of G4 from that sheet, so sheet "47 20 VR"

    Attachment 704947

    Hope this helps.

  6. #6
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Use cell as filename while saving via VBA

    I cannot view attachments the way you uploaded yours. I have no idea what question askers do to cause that. see this image. got another way?
    Attached Images Attached Images

  7. #7
    Registered User
    Join Date
    03-03-2015
    Location
    hoogmade, nederland
    MS-Off Ver
    office 365
    Posts
    6

    Re: Use cell as filename while saving via VBA

    I tried again. Can you see the attachment now?
    Attached Images Attached Images

  8. #8
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Use cell as filename while saving via VBA

    Quote Originally Posted by jesse1601 View Post
    When I go to the next sheet, I have the same button and than it should take the information out of G4 from that sheet, so sheet "47 20 VR"
    if you want to have a button on EVERY SHEET and press those manually everytime you want an export, then it gets even easier:
    Please Login or Register  to view this content.
    alternatives to try if it errors:

    activesheet.name

    activeworkbook.activeworksheet

    activeworkbook.activesheet

    put that code in a standard module and call the same routine behind every button on every sheet.

  9. #9
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: Use cell as filename while saving via VBA

    Looks like Cell G4 does not have "Donderdag"
    It does look like the Cell(s) that have "Donderdag" are formatted as Merge & Center.
    If so, to avoid future problems which are just about certain to happen, get rid of all Merge & Center and change it to Center Across Selection.
    Als dat niet zo is, vergeet wat ik net heb gezegd.

    Please Login or Register  to view this content.

+ 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. How to append sheet1 cell value to filename when saving
    By ramrez2009 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-31-2014, 03:44 AM
  2. Saving workbook as notepad using cell data for filename & retaining original workbook name
    By peterkerr_13 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-26-2012, 08:07 AM
  3. Help with formula of filename and saving filename as current time
    By hqradio in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-04-2012, 06:10 AM
  4. Saving filename with content from a cell error
    By bryanc2k in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-29-2010, 06:52 AM
  5. Saving filename with content from a cell?
    By fael097 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-28-2010, 02:14 PM
  6. Saving Filename From Cell Text
    By jeffv in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-23-2009, 03:09 PM
  7. creating a folder and saving in it with a filename taken from cell
    By dittotharappel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-21-2009, 12:52 PM

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