Consulting

Results 1 to 4 of 4

Thread: Solved: file generation + naming from a variable

  1. #1

    Solved: file generation + naming from a variable

    Hey all,

    what would be command to create a file using a variable from an array?

    example:

    range (c1:c5) holds information like : data.xls. datz.xls, etc...

    need to know how to open\create a file (including location)...
    any ideas?

    Thanks!

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub test()
    Dim cel as range
    For Each cel In Range("C1:C5")
    Workbooks.Add
    ActiveWorkbook.SaveAs "C:\AAA\" & cel & ".xls"
    Next
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Hi mdmackillop,

    Thanks, I should have added that I dont want to see the screens flying by (there is alot of cells to go through in the actual script) heres what I got so far:

    Dim Filename As String
    Dim aduserid As String
    Dim secgroup As String
    Dim userid As String
    Dim Resname As String
    Dim WS As Worksheet


    destinationpath = "c:\test\"

    Set WS = Worksheets("Lookup")

    For Each cell In WS.Range("L2:L50")

    aduserid = cell.Value
    userid = cell.Offset(0, -9).Value
    secgroup = cell.Offset(0, -2).Value
    Resname = cell.Offset(0, 7).Value


    Filename = destinationpath & Resname

    Dim OutputFile As String

    OutputFile = Filename

    If Len(Dir(OutputFile)) Then
    Kill OutputFile
    End If

    ' Do While cell <> ""

    Open Filename For Output As #1

    Write #1, "blah blah blah"

    Close #1

    Next cell

    ' Loop

    End Sub

    I am having an issue with when it hits a blank cell , it gives me file not found and crashes out. I was trying to see about a do hile statement to bypass the blank cell issue... any ideas?

  4. #4
    Hi ,

    Try using an if condition like:

    For Each cell In WS.Range("L2:L50")
    if cell.value<>"" then
    aduserid = cell.Value
    userid = cell.Offset(0, -9).Value
    secgroup = cell.Offset(0, -2).Value
    Resname = cell.Offset(0, 7).Value


    Filename = destinationpath & Resname

    Dim OutputFile As String

    OutputFile = Filename

    If Len(Dir(OutputFile)) Then
    Kill OutputFile
    End If

    ' Do While cell <> ""

    Open Filename For Output As #1

    Write #1, "blah blah blah"

    Close #1
    else
    endif
    Next cell

Posting Permissions

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