I need macro to open a file, copy and paste everything under specific column into wor

kbenjamin827

New member
Joined
Jul 5, 2018
Messages
2
Reaction score
0
Points
0
Excel Version(s)
2007
Hi all,

I need a macro that opens a file, then copy and pastes everything under a specific column (named Total) and place those values under the Total column in the workbook. What I have so far is a macro that opens a file, and copies and pastes values from a fixed cell range, into a fixed destination.

Code:
Sub Foo()
Dim vFile As Variant
Dim wbCopyTo As Workbook
Dim wsCopyTo As Worksheet
Dim wbCopyFrom As Workbook
Dim wsCopyFrom As Worksheet

Set wbCopyTo = ActiveWorkbook
Set wsCopyTo = ActiveSheet


    '-------------------------------------------------------------
    'Open file with data to be copied
    
    vFile = Application.GetOpenFilename("Excel Files (*.xl*)," & _
    "*.xl*", 1, "Select Excel File", "Open", False)
    
    'If Cancel then Exit
    If TypeName(vFile) = "Boolean" Then
        Exit Sub
    Else
    Set wbCopyFrom = Workbooks.Open(vFile)
    Set wsCopyFrom = wbCopyFrom.Worksheets(1)
    End If
    
    '--------------------------------------------------------------
    'Copy Range
    wsCopyFrom.Range("D6:R26").Copy
    wsCopyTo.Range("D7").PasteSpecial Paste:=xlPasteValues, _
            Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
    wsCopyFrom.Range("S6:S26").Copy
    wsCopyTo.Range("X7").PasteSpecial Paste:=xlPasteValues, _
            Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            
    'Close file that was opened
    wbCopyFrom.Close SaveChanges:=False

End Sub

Any suggestions?
 
.
Are the source Sheet/Column/Cell range always the same ?

Are the destination Sheet/Column/Cell range always the same ?
 
source sheet/column/cell range usually differ. usually it is 1-15, but at times they can be 3-12, 5-10, etc.. however the destination sheet/column/cell range are always the same, 1-15.
 
.
Try this :

Code:
Option Explicit


Sub Foo()
Dim vFile As Variant
Dim wbCopyTo As Workbook
Dim wsCopyTo As Worksheet
Dim wbCopyFrom As Workbook
Dim wsCopyFrom As Worksheet
Dim x As String


Set wbCopyTo = ActiveWorkbook
Set wsCopyTo = ActiveSheet




    '-------------------------------------------------------------
    'Open file with data to be copied
    
    vFile = Application.GetOpenFilename("Excel Files (*.xl*)," & _
    "*.xl*", 1, "Select Excel File", "Open", False)
    
    'If Cancel then Exit
    If TypeName(vFile) = "Boolean" Then
        Exit Sub
    Else
    Set wbCopyFrom = Workbooks.Open(vFile)
    Set wsCopyFrom = wbCopyFrom.Worksheets(1)
    End If
    
    '--------------------------------------------------------------
    'Copy Range
    
     Dim UserRange As Range
     Dim OutPut As String   ' output = 565
     Dim Prompt As String
     Dim Title As String
     
        Prompt = "Select a cell for the output."
        Title = "Select a cell"
    
    '   Display the Input Box
        On Error Resume Next
        Set UserRange = Application.InputBox( _
            Prompt:=Prompt, _
            Title:=Title, _
            Default:=ActiveCell.Address, _
            Type:=8) 'Range selection
            Application.ScreenUpdating = False
    
    '   Was the Input Box canceled?
        If UserRange Is Nothing Then
            MsgBox "Canceled."
        Else


            'UserRange = OutPut
            UserRange.Select
            Selection.Copy
            
            wsCopyTo.Range("A1").PasteSpecial Paste:=xlPasteValues, _
                Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                
            Application.ScreenUpdating = True


        End If
        
    'Close file that was opened
    wbCopyFrom.Close SaveChanges:=False
    wsCopyTo.Range("A1").Select


End Sub
 
Back
Top