You are here:

Excel/Excel button macro- transfer data

Advertisement


Question
QUESTION: Hi there!

Your post was very helpful! http://en.allexperts.com/q/Excel-1059/2012/6/creating-submit-button-macro.htm

I was wondering if you could have a macro transfer the data to a different workbook instead of a different sheet in the same workbook?

ANSWER: Gatlin McConnell,

I assume you mean this code:

Private Sub CommandButton1_Click()
dim sh1 as worksheet, sh2 as worksheet
Dim r1 as Range, rw as Long


Set sh1 = Worksheets("Sheet1")
Set sh2 = Worksheets("Sheet2")

set r1 = Sh1.Range("A2:H2")  '<== change to the range you want to copy

' find the next open row in sheet2 using column A
rw = sh2.Cells(rows.count,"A").End(xlup).offset(1,0).Row
' Now copy and clear the data
r1.copy sh2.Cells(rw,"A")
' clears the value after it is transferred
r1.clearContents

End Sub

you would just change the reference to point to a sheet in another open workbook


Private Sub CommandButton1_Click()
dim sh1 as worksheet, sh2 as worksheet
Dim r1 as Range, rw as Long


Set sh1 = Worksheets("Sheet1")
Set sh2 = Workbooks("Otherworkbook.xls").Worksheets("Sheetnameofsheet") '<= changed line

set r1 = Sh1.Range("A2:H2")  '<== change to the range you want to copy

' find the next open row in sheet2 using column A
rw = sh2.Cells(rows.count,"A").End(xlup).offset(1,0).Row
' Now copy and clear the data
r1.copy sh2.Cells(rw,"A")
' clears the value after it is transferred
r1.clearContents

End Sub

does that answer the question?

--
Regards,
Tom Ogilvy



---------- FOLLOW-UP ----------

QUESTION: That works great!!! Thank you!

One more question, is there any way the information can be transferred without having the 2nd workbook open?

To give you an idea of what I'm trying to accomplish... I have employees that complete a daily task log and each day is its own workbook and worksheet, saved as the Julian day (i.e. 13-001, 13-002). On these task logs, the employee indicates how many audits they performed that day... I want to be able to obtain the number of audits performed that day into a master workbook that reports the date and how many audits were performed... The above code you provided works great, but they would have to have my master workbook open...

Hope that makes sense... Thank you for your time!

ANSWER: Gatlin McConnell,

the code could be modified to open the workbook, write the data, save and close the workbook - but unless you want to get into database programming and treat your master workbook as a database, it would need to be opened.  Also, in either case, the person running the code would need to be able to access the file being written to.

--
Regards,
Tom Ogilvy




---------- FOLLOW-UP ----------

QUESTION: The user would/could have access to the file- I'm just trying to make one less step for them to have to manually open it each and every time... like you mentioned, if the code could be modified to open, write, save and then close, that would be perfect!

Answer

Private Sub CommandButton1_Click()
dim sh1 as worksheet, sh2 as worksheet
Dim r1 as Range, rw as Long, bk as workbook
Dim s as String, bk as workbook

Set sh1 = Worksheets("Sheet1")

s = "M:\Folder1\SubFolder1\Master.xls"  '<= change
s1 = "master.xls"  '<== change
on error resume next
 workbooks.Opens s
on Error goto 0
set bk = ActiveWorkbook

if Instr(1, bk.name, s1, vbTextcompare) = 0 then
 msgbox s1 & " was not opened - quitting"
 exit sub
end if

Set sh2 = bk.Worksheets("Sheetnameofsheet") '<= changed line

set r1 = Sh1.Range("A2:H2")  '<== change to the range you want to copy

' find the next open row in sheet2 using column A
rw = sh2.Cells(rows.count,"A").End(xlup).offset(1,0).Row
' Now copy and clear the data
r1.copy sh2.Cells(rw,"A")
' clears the value after it is transferred
r1.clearContents
bk.Save
bk.Close SaveChanges:=False

End Sub

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.