+ Reply to Thread
Results 1 to 7 of 7

Setting a workbook name as a variable...

  1. #1
    Registered User
    Join Date
    01-25-2005
    Posts
    4

    Question Setting a workbook name as a variable...

    OK--

    I have two workbooks. The first workbook contains information that is copied to the second workbook. I then wish to go back to the first workbook and close it (and then repeat this process with additional workbooks' contents being copied to the second workbook). Should be simple...


    Windows("Workbook1").Activate
    ActiveWorkbook.Close



    HOWEVER-- there are the following restrictions:

    *The name of the first workbook changes daily at random.

    *The first workbook is a nonExcel document that opens via Import Wizard & is never saved while it's open.

    *This process should run without user interference - i.e., no forms that require the name of workbook being typed in by the user.

    Any ideas? Many thanks in advance.


    *
    Last edited by apeface76; 01-25-2005 at 04:44 PM. Reason: clarification

  2. #2
    Forum Contributor
    Join Date
    11-09-2004
    Posts
    451
    Are all the workbooks in same directory.

    so you want to copy all the workbook into single workbook.

    It is not clear, can you explain in detail.

  3. #3
    Registered User
    Join Date
    01-25-2005
    Posts
    4
    Quote Originally Posted by anilsolipuram
    Are all the workbooks in same directory.

    so you want to copy all the workbook into single workbook.

    It is not clear, can you explain in detail.


    I have a random number of text files (between 3-7). All of these text files are in the same directory.

    I want to open the first text file in Excel and, after formatting it, copy its contents into a pre-existing Excel file (which is already open & located in a different directory).

    I then wish to close the text file without saving it, open the second text file in Excel, copy its contents into the pre-existing Excel file, and so forth.

    The names of the text files change daily.

    I hope this clarifies things.

  4. #4
    Forum Contributor
    Join Date
    11-09-2004
    Posts
    451
    Read through the comments

    Sub Macro1()
    Dim ar As Variant
    Dim file_path As Variant
    file_path = "c:\test\" 'this is the path where the text files exist
    ar = Array("Diag000.txt", "Diag001.txt") ' this are the files from which we need to copy
    Dim file_name As Variant
    file_name = ActiveWorkbook.Name
    Cells.Select
    Cells.Delete
    For i = 0 To UBound(ar)
    'below code will open the text demlimit by space
    Workbooks.OpenText Filename:=file_path & ar(i), Origin:=xlWindows, StartRow _
    :=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
    ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False, Comma:=False _
    , Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array _
    (3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1))
    Range(ActiveSheet.UsedRange.Address).Select
    Selection.Copy 'copy the text file format
    While ActiveWorkbook.Name <> file_name
    ActiveWindow.ActivateNext
    Wend
    Dim r As Range
    Set r = ActiveSheet.UsedRange
    temp = Split(r.Address, ":")
    If UBound(temp) > 0 Then
    Range("a" & Range(temp(1)).Offset(1, 0).Row).Select
    Else
    Range("a" & Range(r.Address).Row).Select
    End If
    ActiveSheet.Paste 'paste into the workbook
    Application.CutCopyMode = False
    While ActiveWorkbook.Name <> ar(i)
    ActiveWindow.ActivateNext
    Wend
    ActiveWindow.Close 'close the text file
    Next
    End Sub

  5. #5
    Registered User
    Join Date
    01-25-2005
    Posts
    4
    First off, thanks!

    A couple questions:

    1) Are the items in the array required to have fixed names ("Diag000.txt", "Diag001.txt)? Or can they be as follows: ("Diag" & TDate & "000.txt", "Diag" & TDate & "001.txt").


    2) Since we don't know how many text files there will be each day, is it possible to include all possible text file names within the array & then skip over the ones not found in the directory?


    Please pardon my ignorance-- I've only been working in VBA for about a month.



    Quote Originally Posted by anilsolipuram
    Read through the comments

    Sub Macro1()
    Dim ar As Variant
    Dim file_path As Variant
    file_path = "c:\test\" 'this is the path where the text files exist
    ar = Array("Diag000.txt", "Diag001.txt") ' this are the files from which we need to copy
    Dim file_name As Variant
    file_name = ActiveWorkbook.Name
    Cells.Select
    Cells.Delete
    For i = 0 To UBound(ar)
    'below code will open the text demlimit by space
    Workbooks.OpenText Filename:=file_path & ar(i), Origin:=xlWindows, StartRow _
    :=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
    ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False, Comma:=False _
    , Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array _
    (3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1))
    Range(ActiveSheet.UsedRange.Address).Select
    Selection.Copy 'copy the text file format
    While ActiveWorkbook.Name <> file_name
    ActiveWindow.ActivateNext
    Wend
    Dim r As Range
    Set r = ActiveSheet.UsedRange

    temp = Split(r.Address, ":")
    If UBound(temp) > 0 Then
    Range("a" & Range(temp(1)).Offset(1, 0).Row).Select
    Else
    Range("a" & Range(r.Address).Row).Select
    End If
    ActiveSheet.Paste 'paste into the workbook
    Application.CutCopyMode = False
    While ActiveWorkbook.Name <> ar(i)
    ActiveWindow.ActivateNext
    Wend
    ActiveWindow.Close 'close the text file
    Next
    End Sub

  6. #6
    Forum Contributor
    Join Date
    11-09-2004
    Posts
    451
    1) Are the items in the array required to have fixed names ("Diag000.txt", "Diag001.txt)? Or can they be as follows: ("Diag" & TDate & "000.txt", "Diag" & TDate & "001.txt").

    You can use variable in array value, no array values are not fixed.



    I used error trapping mechanism to skip files which donot exist.









    Sub Macro1()
    Dim ar As Variant
    Dim file_path As Variant
    file_path = "c:\test\" 'this is the path where the text files exist
    ar = Array("Diag000.txt", "Diag001.txt") ' this are the files from which we need to copy
    Dim file_name As Variant
    file_name = ActiveWorkbook.Name
    Cells.Select
    Cells.Delete
    For i = 0 To UBound(ar)
    'below code will open the text demlimit by space
    on error resume next
    Workbooks.OpenText Filename:=file_path & ar(i), Origin:=xlWindows, StartRow _
    :=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
    ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False, Comma:=False _
    , Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array _
    (3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1))
    if err.description<>"" then
    err.clear
    go to a:
    else
    Range(ActiveSheet.UsedRange.Address).Select
    Selection.Copy 'copy the text file format
    While ActiveWorkbook.Name <> file_name
    ActiveWindow.ActivateNext
    Wend
    Dim r As Range
    Set r = ActiveSheet.UsedRange

    temp = Split(r.Address, ":")
    If UBound(temp) > 0 Then
    Range("a" & Range(temp(1)).Offset(1, 0).Row).Select
    Else
    Range("a" & Range(r.Address).Row).Select
    End If
    ActiveSheet.Paste 'paste into the workbook
    Application.CutCopyMode = False
    While ActiveWorkbook.Name <> ar(i)
    ActiveWindow.ActivateNext
    Wend
    ActiveWindow.Close 'close the text file
    end if
    a:
    Next
    End Sub

  7. #7
    Registered User
    Join Date
    01-25-2005
    Posts
    4
    now we're cooking with gas! many thanks for your help!

+ 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