Results 1 to 10 of 10

Thread: [Excel 2011]:Writing from one Excel file to enother- Dynamic way

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    3

    [Excel 2011]:Writing from one Excel file to enother- Dynamic way

    Hi all,
    I have one file that contains three rows of information (Plz see the attached file) and I want to copy each row to a different file. Each row should be copy to a certain file that his name is located in the first file in row D.
    In the end I expect to find the original file with addition of 3 files with data: File1.xlsx, File2.xlsx, File3.xlsx.

    Is there an option in VB to do it like that?
    Thanks in ahead!
    Attached Images Attached Images  

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [Excel 2011]:Writing from one Excel file to enother- Dynamic way

    Welcome to VBForums

    Yes, this can be done using VBA.
    You can try to use the macro-recoder.
    Using the macro-recorder, perform all steps needed, stop the recorder and look at the code!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Excel 2011]:Writing from one Excel file to enother- Dynamic way

    if the names in column D are existing files then open the files, else add a workbook, then saveas
    example for the latter
    vb Code:
    1. rw = 1
    2. do until isempty(range("a1"))
    3.   set newbk = workbooks.add
    4.   newbk.sheets(1).range("a1:e1") = range(cells(rw, 1),cells(rw, 5))
    5.   newbk.saveas thisworkbook.path &"\" & cells(rw, 4) & ".xlsx"  ' other parameters if required
    6.   newbk.close
    7.   rw = rw + 1
    8. loop
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [Excel 2011]:Writing from one Excel file to enother- Dynamic way

    I'd change to start of the loop to.
    Code:
    do until isempty(cells(rw,1))
    otherwise to loop will run till...........................
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Excel 2011]:Writing from one Excel file to enother- Dynamic way

    I'd change to start of the loop to.
    good idea
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    3

    Re: [Excel 2011]:Writing from one Excel file to enother- Dynamic way

    Thank you guys for your very quick response.
    Opus, the kind of solution that you suggested will work only in the private case like I attached. I need a general solution that will even work tomorrow in the same way if I would have a table that the orders of the rows will not be the same.
    westconn1, great loop, but is it run in your computer?
    Are you sure that ThisWorkbook.Path & "\" & Cells(rw, 4) & ".xlsx" is the right way to write it?
    I found that &Cells(rw, 4) doesn't put the data in the address.

    Thanks again.

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Excel 2011]:Writing from one Excel file to enother- Dynamic way

    Are you sure that ThisWorkbook.Path & "\" & Cells(rw, 4) & ".xlsx" is the right way to write it?
    yes, unless this is apple version or some such, then god knows

    I found that &Cells(rw, 4) doesn't put the data in the address.
    what address?
    to test the results
    vb Code:
    1. msgbox cells(rw, 4)
    2. 'or
    3. msgbox thisworkbook.path
    4. msgbox thisworkbook.path & "\" & cells(rw, 4)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    3

    Re: [Excel 2011]:Writing from one Excel file to enother- Dynamic way

    Do you know what could be the reason that it doesn't work?
    For some reason I get a message "Run time error '1004': application defined or object defined error"...
    Attached Images Attached Images  

  9. #9
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: [Excel 2011]:Writing from one Excel file to enother- Dynamic way

    If I am not mistaken it is because the way you setup your calculation for your 'rw' variable. rw will be 1 more than the last row you put data in.

    If I am right, try:
    vb Code:
    1. MsgBox ThisWorkbook.Path & "\" & Cells(rw, 4).Offset(-1, 0)
    2.  
    3. Or
    4.  
    5.     rw2 = rw - 1
    6.     MsgBox ThisWorkbook.Path & "\" & Cells(rw2, 4)

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Excel 2011]:Writing from one Excel file to enother- Dynamic way

    where you tried that in button4 rw had no value assigned (therefore 0), row(0) does not exist and will give 1004 error, you must assign value to rw

    if you have declared rw in the general section an assign value elsewhere, please disregard the above
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width