SOLVED

VBA - Transferring select data to new spreadsheet

%3CLINGO-SUB%20id%3D%22lingo-sub-1501941%22%20slang%3D%22en-US%22%3EVBA%20-%20Transferring%20select%20data%20to%20new%20spreadsheet%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1501941%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20found%20this%20VBA%20code%20to%20transfer%20select%20data%20(Row%2012%20only)%20from%20one%20file%20(Source%20file)%20to%20another%20(Transfer%20Data%20file).%20Obviously%2C%20this%20is%20very%20limited%20in%20its%20uses%2C%20therefore%20i%20would%20like%20to%20adapt%20so%20that%20rather%20than%20transferring%20data%20from%20only%20Row%2012%20it%20transfers%20rows%20with%20'Yes'%20in%20column%20A....and%20if%20possible%20to%20only%20transfer%20select%20data%2C%20such%20as%20columns%20B%3AD%20and%20G%3AI.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECurrently%2C%20the%20VBA%20opens%2C%20adds%20the%20data%20to%20the%20'Transfer%20Data'%20file%20on%20the%20next%20empty%20line%2C%20saves%20and%20closes%20the%20document.%20These%20features%20i'd%20like%20to%20remain.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20anyone%20can%20assist%20it%20would%20be%20a%20huge%20help.%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3ESimon%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1501941%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1503408%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20-%20Transferring%20select%20data%20to%20new%20spreadsheet%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1503408%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F639845%22%20target%3D%22_blank%22%3E%40Si_Slack%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20try%20the%20updated%20macro%20in%20the%20attached%20file.%26nbsp%3B%20I%20used%20autofilter%20to%20select%20only%20data%20with%20'Yes'%20in%20column%20A.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAs%20your%20code%20saves%20the%20workbook%20before%20closing%2C%20I'd%20suggest%20saving%20a%20copy%20of%20both%20files%20to%20a%20new%20location%20(as%20a%20backup)%20before%20running%20the%20macro.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1505428%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20-%20Transferring%20select%20data%20to%20new%20spreadsheet%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1505428%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F531239%22%20target%3D%22_blank%22%3E%40Charla74%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%20the%20response.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20just%20tried%20the%20macro%20and%20it%20returns%20the%20same%20result%20(Row%2012).%20Maybe%20i've%20missed%20something%3F%20I've%20check%20the%20code%20and%20it%20looks%20to%20be%20the%20same%20too.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1505460%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20-%20Transferring%20select%20data%20to%20new%20spreadsheet%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1505460%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F639845%22%20target%3D%22_blank%22%3E%40Si_Slack%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDid%20you%20download%20the%20file%20from%20my%20post%3F%26nbsp%3B%20Anyway%2C%20the%20revised%20code%20should%20look%20as%20follows%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPrivate%20Sub%20CommandButton1_Click()%3C%2FP%3E%3CP%3EDim%20wb%20As%20Workbook%3CBR%20%2F%3EThisWorkbook.Worksheets(%22Source%22).Rows(12).Select%3CBR%20%2F%3ESelection.Copy%3C%2FP%3E%3CP%3ESet%20wb%20%3D%20Workbooks.Open(%22C%3A%5CUsers%5CSslack%5CDesktop%5CTransfer%20Data.xlsx%22)%3CBR%20%2F%3Ewb.Worksheets(%22data%22).Activate%3C%2FP%3E%3CP%3Elastrow%20%3D%20ActiveSheet.Cells(Rows.Count%2C%201).End(xlUp).Row%3C%2FP%3E%3CP%3EActiveSheet.Cells(lastrow%20%2B%201%2C%201).Select%3C%2FP%3E%3CP%3EActiveSheet.Paste%3CBR%20%2F%3EActiveWorkbook.Save%3C%2FP%3E%3CP%3EActiveWorkbook.Close%20savechanges%20%3D%20True%3C%2FP%3E%3CP%3ESet%20wb%20%3D%20Nothing%3C%2FP%3E%3CP%3EThisWorkbook.Worksheets(%22Source%22).Activate%3CBR%20%2F%3EThisWorkbook.Worksheets(%22Source%22).Cells(1%2C%201).Select%3C%2FP%3E%3CP%3EApplication.CutCopyMode%20%3D%20False%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1510119%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20-%20Transferring%20select%20data%20to%20new%20spreadsheet%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1510119%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F531239%22%20target%3D%22_blank%22%3E%40Charla74%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYes%2C%20I've%20downloaded%20the%20workbook%20a%20couple%20of%20times%20and%20tried%20it.%20It%20still%20only%20transfers%20'Row%2012'%20as%20it%20did%20previously.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAlso%2C%20the%20code%20in%20your%20message%20is%20the%20same%20code%20i%20had%20in%20the%20original%20workbook.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMaybe%20the%20changes%20didn't%20save%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1510133%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20-%20Transferring%20select%20data%20to%20new%20spreadsheet%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1510133%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F639845%22%20target%3D%22_blank%22%3E%40Si_Slack%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOops!%26nbsp%3B%20You're%20right%2C%20it%20is%20the%20same%20as%20the%20original%2C%20not%20sure%20how%20I%20managed%20that....%3C%2FP%3E%3CP%3ESo%2C%20below%20should%20be%20the%20alternative%20code%20-%20try%20this%20and%20let%20me%20know%20how%20it%20works%20out%20for%20you.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESub%20Export_Details()%3C%2FP%3E%3CP%3EDim%20wb%20As%20Workbook%3CBR%20%2F%3EDim%20rTable%20As%20Range%3C%2FP%3E%3CP%3EThisWorkbook.Worksheets(%22Source%22).Activate%3CBR%20%2F%3EActiveSheet.UsedRange.AutoFilter%20Field%3A%3D1%2C%20Criteria1%3A%3D%22Yes%22%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20rTable%20%3D%20Sheets(%22Source%22).AutoFilter.Range%3CBR%20%2F%3ESet%20rTable%20%3D%20rTable.Resize(rTable.Rows.Count%20-%201)%3CBR%20%2F%3ESet%20rTable%20%3D%20rTable.Offset(1)%3CBR%20%2F%3ErTable.Copy%3C%2FP%3E%3CP%3ESet%20wb%20%3D%20Workbooks.Open(%22C%3A%5CUsers%5CSslack%5CDesktop%5CTransfer%20Data.xlsm%22)%3CBR%20%2F%3Ewb.Worksheets(%22data%22).Activate%3C%2FP%3E%3CP%3Elastrow%20%3D%20ActiveSheet.Cells(Rows.Count%2C%201).End(xlUp).Row%3C%2FP%3E%3CP%3EActiveSheet.Cells(lastrow%20%2B%201%2C%201).Select%3C%2FP%3E%3CP%3EActiveSheet.Paste%3CBR%20%2F%3EActiveWorkbook.Save%3C%2FP%3E%3CP%3EActiveWorkbook.Close%20savechanges%20%3D%20True%3C%2FP%3E%3CP%3ESet%20wb%20%3D%20Nothing%3C%2FP%3E%3CP%3EThisWorkbook.Worksheets(%22Source%22).Activate%3CBR%20%2F%3EApplication.CutCopyMode%20%3D%20False%3CBR%20%2F%3EActiveSheet.AutoFilterMode%20%3D%20False%3CBR%20%2F%3EThisWorkbook.Worksheets(%22Source%22).Cells(1%2C%201).Select%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1514628%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20-%20Transferring%20select%20data%20to%20new%20spreadsheet%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1514628%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F531239%22%20target%3D%22_blank%22%3E%40Charla74%3C%2FA%3E%26nbsp%3B%20It%20works%20a%20treat!!%20Thank%20you%20for%20your%20help!!%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi, 

I've found this VBA code to transfer select data (Row 12 only) from one file (Source file) to another (Transfer Data file). Obviously, this is very limited in its uses, therefore i would like to adapt so that rather than transferring data from only Row 12 it transfers rows with 'Yes' in column A.

 

Currently, the VBA opens, adds the data to the 'Transfer Data' file on the next empty line, saves and closes the document. These features i'd like to remain.

 

Private Sub CommandButton1_Click()

Dim wb As Workbook
ThisWorkbook.Worksheets("Source").Rows(12).Select
Selection.Copy

 

Set wb = Workbooks.Open("C:\Users\Sslack\Desktop\Transfer Data.xlsx")
wb.Worksheets("data").Activate

 

lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

 

ActiveSheet.Cells(lastrow + 1, 1).Select

 

ActiveSheet.Paste


ActiveWorkbook.Save

 

ActiveWorkbook.Close savechanges = True

 

Set wb = Nothing

 

ThisWorkbook.Worksheets("Source").Activate
ThisWorkbook.Worksheets("Source").Cells(1, 1).Select

 

Application.CutCopyMode = False

 

End Sub

 

If anyone can assist it would be a huge help. 

Thanks

Simon

6 Replies
Highlighted

@Si_Slack 

Hi,

 

Please try the updated macro in the attached file.  I used autofilter to select only data with 'Yes' in column A.

 

As your code saves the workbook before closing, I'd suggest saving a copy of both files to a new location (as a backup) before running the macro.

Highlighted

 

Hi @Charla74,

 

Thank you for the response. 

 

I've just tried the macro and it returns the same result (Row 12). Maybe i've missed something? I've check the code and it looks to be the same too.

 

Thanks

 

Highlighted

Hi @Si_Slack 

 

Did you download the file from my post?  Anyway, the revised code should look as follows:

 

Private Sub CommandButton1_Click()

Dim wb As Workbook
ThisWorkbook.Worksheets("Source").Rows(12).Select
Selection.Copy

Set wb = Workbooks.Open("C:\Users\Sslack\Desktop\Transfer Data.xlsx")
wb.Worksheets("data").Activate

lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

ActiveSheet.Cells(lastrow + 1, 1).Select

ActiveSheet.Paste
ActiveWorkbook.Save

ActiveWorkbook.Close savechanges = True

Set wb = Nothing

ThisWorkbook.Worksheets("Source").Activate
ThisWorkbook.Worksheets("Source").Cells(1, 1).Select

Application.CutCopyMode = False

End Sub

Highlighted

Hi @Charla74,

 

Yes, I've downloaded the workbook a couple of times and tried it. It still only transfers 'Row 12' as it did previously. 

 

Also, the code in your message is the same code i had in the original workbook.  

 

Maybe the changes didn't save?

Highlighted
Best Response confirmed by Si_Slack (Occasional Contributor)
Solution

@Si_Slack 

 

Oops!  You're right, it is the same as the original, not sure how I managed that....

So, below should be the alternative code - try this and let me know how it works out for you.

 

Sub Export_Details()

Dim wb As Workbook
Dim rTable As Range

ThisWorkbook.Worksheets("Source").Activate
ActiveSheet.UsedRange.AutoFilter Field:=1, Criteria1:="Yes"

Set rTable = Sheets("Source").AutoFilter.Range
Set rTable = rTable.Resize(rTable.Rows.Count - 1)
Set rTable = rTable.Offset(1)
rTable.Copy

Set wb = Workbooks.Open("C:\Users\Sslack\Desktop\Transfer Data.xlsm")
wb.Worksheets("data").Activate

lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

ActiveSheet.Cells(lastrow + 1, 1).Select

ActiveSheet.Paste
ActiveWorkbook.Save

ActiveWorkbook.Close savechanges = True

Set wb = Nothing

ThisWorkbook.Worksheets("Source").Activate
Application.CutCopyMode = False
ActiveSheet.AutoFilterMode = False
ThisWorkbook.Worksheets("Source").Cells(1, 1).Select

End Sub

Highlighted

@Charla74  It works a treat!! Thank you for your help!!