[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!
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!
newbk.saveas thisworkbook.path &"\" & cells(rw, 4) & ".xlsx" ' other parameters if required
newbk.close
rw = rw + 1
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
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
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.
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:
msgbox cells(rw, 4)
'or
msgbox thisworkbook.path
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
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"...
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