VBA Help

BarneyLTD

New Member
Joined
Sep 27, 2017
Messages
27
Hi all, i'm hoping someone can help me write a little code to perform the below action?

So, I have 2 workbooks. I need to copy cells A-E from the active sheet to a closed workbook and paste the values in to the first sheet (named Original Order) from cell A1.
The destination workbook resides in different locations so I need to be prompted to open the file. I also need this file to remain open so a copy can be saved in a shared folder.

Is this something that can be done?

I'm overhauling some processes for work so I might be back with a few more questions :)

Thanks in advance

Tom
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Ensure to change the filter and the default folder to suit your needs.

Code:
Sub CopyCells()
    Dim wbTo As Workbook
    Dim wsTo As Worksheet, wsFrom As Worksheet
    Dim sDefaultFolder As String
    Dim sPath As String
    Dim sfilter As String
    
    'Set the default folder for the prmpt to open to
    sDefaultFolder = "C:\Test" 'change as necessary
    ChDir sDefaultFolder
    
    'set filter, change as required
    sfilter = "Excel Files (*.xlsx; *.xlsm),*.xlsx;*.xlsm"
    
    'get the file's full path
    sPath = Application.GetOpenFilename(FileFilter:=sfilter)
    
    If sPath = "False" Then Exit Sub 'user clicked cancel
    
    'get the active sheet as this will change when workbook is open
    Set wsFrom = ActiveSheet
    
    'open the file. Ensure it isn't open
    On Error Resume Next
    Set wbTo = Workbooks.Open(Filename:=sPath)
    If Err.Number > 0 Then MsgBox "Error opening File": Exit Sub
    On Error GoTo 0
    
    'get the worksheet
    Set wsTo = wbTo.Worksheets(1)
    
    'copy A:E to A1
    wsFrom.Range("A:E").Copy wsTo.Range("A1")
    
    ActiveSheet.Cells(1, 1).Select 'select first cell
    
End Sub
 
Upvote 0
Gallen, you're a genius! This works perfectly :)
I would like to paste in the values only if this is possible? The sheet it pastes to has formatting I wish to keep.



Ensure to change the filter and the default folder to suit your needs.

Code:
Sub CopyCells()
    Dim wbTo As Workbook
    Dim wsTo As Worksheet, wsFrom As Worksheet
    Dim sDefaultFolder As String
    Dim sPath As String
    Dim sfilter As String
    
    'Set the default folder for the prmpt to open to
    sDefaultFolder = "C:\Test" 'change as necessary
    ChDir sDefaultFolder
    
    'set filter, change as required
    sfilter = "Excel Files (*.xlsx; *.xlsm),*.xlsx;*.xlsm"
    
    'get the file's full path
    sPath = Application.GetOpenFilename(FileFilter:=sfilter)
    
    If sPath = "False" Then Exit Sub 'user clicked cancel
    
    'get the active sheet as this will change when workbook is open
    Set wsFrom = ActiveSheet
    
    'open the file. Ensure it isn't open
    On Error Resume Next
    Set wbTo = Workbooks.Open(Filename:=sPath)
    If Err.Number > 0 Then MsgBox "Error opening File": Exit Sub
    On Error GoTo 0
    
    'get the worksheet
    Set wsTo = wbTo.Worksheets(1)
    
    'copy A:E to A1
    wsFrom.Range("A:E").Copy wsTo.Range("A1")
    
    ActiveSheet.Cells(1, 1).Select 'select first cell
    
End Sub
 
Upvote 0
Change the copy line to:

Code:
    'copy A:E to A1
    wsFrom.Range("A:E").Copy
    wsTo.Range("A1").PasteSpecial xlPasteValues
 
Last edited:
Upvote 0
Gallen, another favour to ask of you :)

is it possible to alter this so it will only copy rows where the value in column C is greater than 0?
i tried to add in a filter but it isn't working for me.
 
Upvote 0
Gallen, another favour to ask of you :)

is it possible to alter this so it will only copy rows where the value in column C is greater than 0?
i tried to add in a filter but it isn't working for me.

If you add in a filter, this is perfectly acceptable, but you will need to change the copy to "only visible cells"

To learn how to do this, I suggest creating a dummy sheet, turning macro recorder on, filter down to a set of values, then highlight what you want and in the home tab press "Find and Select > Go To Special > Visible Cells Only"

Then copy that, and press stop recording.

Look in the code in the VB Editor window and alter to suit your needs, but this will give you the correct syntax to copy only visible cells when you filter.

As an example, if you filter a list of 100 results and only show 4, if you tell excel to copy that range, it will filter those cells plus any in between, whereas if you do it manually you do only copy what's visible to you.
 
Upvote 0
To filter in code use this (amendments in red)

Code:
Sub CopyCells()
    Dim wbTo As Workbook
    Dim wsTo As Worksheet, wsFrom As Worksheet
    Dim sDefaultFolder As String
    Dim sPath As String
    Dim sfilter As String
    
    'Set the default folder for the prmpt to open to
    sDefaultFolder = "C:\Test" 'change as necessary
    ChDir sDefaultFolder
    
    'set filter, change as required
    sfilter = "Excel Files (*.xlsx; *.xlsm),*.xlsx;*.xlsm"
    
    'get the file's full path
    sPath = Application.GetOpenFilename(FileFilter:=sfilter)
    
    If sPath = "False" Then Exit Sub 'user clicked cancel
    
    'get the active sheet as this will change when workbook is open
    Set wsFrom = ActiveSheet
    
    'open the file. Ensure it isn't open
    On Error Resume Next
    Set wbTo = Workbooks.Open(Filename:=sPath)
    If Err.Number > 0 Then MsgBox "Error opening File": Exit Sub
    On Error GoTo 0
    
    'get the worksheet
    Set wsTo = wbTo.Worksheets(1)
    
    [COLOR=#ff0000]'Apply filter
    wsFrom.Range("A:E").AutoFilter Field:=3, Criteria1:=">0"[/COLOR]   
    'copy visible A:E to A1
    wsFrom.Range("A:E")[COLOR=#ff0000].SpecialCells(xlCellTypeVisible)[/COLOR].Copy
    wsTo.Range("A1").PasteSpecial xlPasteValues
    
[COLOR=#ff0000]    'Clear Filter
    wsFrom.Range("A:E").AutoFilter[/COLOR]
    
    'select first cell
    ActiveSheet.Cells(1, 1).Select
    
End Sub
 
Upvote 0
Thanks RockandGrohl, Gallen.

apologies but i should have said the data to filter is in a table.

i get a Runtime error 1004 with that code Gallen, autofilter method of range class failed.

I have data in cells A:E
the table starts in A9 (the headers, data from A10)
Column C is called Order and it is this cell i need to filter before copying.

i tried recording a macro for the filter of a table and pasted that code in to what you provided Gallen but i had no luck.

Cheers
 
Upvote 0

Forum statistics

Threads
1,214,431
Messages
6,119,457
Members
448,898
Latest member
drewmorgan128

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top