Copy form External Workbook XLDOWN into Current workbook - vba

I am trying to copy all the results from cell "A9" tell the end of the data using XLDOWN from the the file named "DDR" & tab named "everett" and paste it back into the workbook im currently using.
Any help will be greatly appreciated
Sub XLDOWN1()
'
' XLDOWN1 Macro
'
'
Dim wbSource As Workbook, wbDest As Workbook
Dim wsSource As Worksheet, wsDest As Worksheet
Dim rngSource As Range, rngDest As Range
Set wbSource = Workbooks.Open("G:\GAGC\Accounting\Payroll\Payroll\Analysis Macro Upload\DDR.xlsx", , True)
Set wsSource = wbSource.Worksheets("Everett")
ws.Range("A9", Range("A9").End(xlDown)).Select
Selection.Copy
Set rngSource = wsSource.Range("A9").Range(Selection, Selection.End(xlDown))
Set wbDest = ThisWorkbook
Set wsDest = wbDest.Worksheets("2016")
Set rngDest = wsDest.Range("A4") 'Destination Cell
rngDest.Value = rngSource.Value 'Copies values over only
wbSource.Close (False) 'Close without saving changes
End Sub

Give this a shot. There were a few issues in the code you provided that should be clear after seeing the refactored code below.
Dim wbSource As Workbook, wbDest As Workbook
Dim wsSource As Worksheet, wsDest As Worksheet
Dim rngSource As Range, rngDest As Range
'set up source workbook, sheet, range
Set wbSource = Workbooks.Open("G:\GAGC\Accounting\Payroll\Payroll\Analysis Macro Upload\DDR.xlsx", , True)
Set wsSource = wbSource.Worksheets("Everett")
Set rngSource = wsSource.Range(wsSource.Range("A9"), wsSource.Range("A9").End(xlDown))
'set up destination workbook, sheet, range
Set wbDest = ThisWorkbook
Set wsDest = wbDest.Worksheets("2016")
Set rngDest = wsDest.Range("A4") 'Destination Cell
rngSource.Copy Destination:=rngDest
wbSource.Close False

Related

Copy Paste between Different Workbook

Hi I want to copy and paste the range to mainworkbook, however, the range is pasted at babyworkook8 instead. Please help
Option Explicit
Dim mainworkbook As Workbook
Dim babyworkbook8 As Workbook
Sub Copypaste()
Set babyworkbook8 = Workbooks.Open("\\C:\IT Charges\IT Charges August.xlsx")
babyworkbook8.Sheets("Sheet1").Range("D4:F27").Copy
Set mainworkbook = Workbooks.Open("\\C:\Users\ivan\Desktop\IT Charges\IT
Summary Charges.xlsm")
mainworkbook.Sheets("Sheet1").Range("R1").PasteSpecial
End Sub
This is a bit long winded, but it clearly defines the source and destination ranges:
Sub CopyPaste()
Dim SourceBook As Workbook
Dim DestBook As Workbook
Dim SourceSheet As Worksheet
Dim DestSheet As Worksheet
Dim SourceRange As Range
Dim DestRange As Range
Set SourceBook = Workbooks.Open("\\C:\IT Charges\IT Charges August.xlsx")
Set DestBook = Workbooks.Open("\\C:\Users\ivan\Desktop\IT Charges\IT Summary Charges.xlsm")
Set SourceSheet = SourceBook.Sheets("Sheet1")
Set DestSheet = DestBook.Sheets("Sheet1")
Set SourceRange = SourceSheet.Range("D4:F27")
Set DestRange = DestSheet.Range("R1")
SourceRange.Copy DestRange
End Sub

copying range end xl down pasting different Wb

I've got a macro recorder code (with select and activate) that I'm trying to simplify. It currently looks like this:
Windows("Stambestand.xlsm").Activate
Range("AA2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("Ijking document.xlsm").Activate
Range("B3").Select
ActiveSheet.Paste
As of recently I've started using variables and want to trim down the code. I'm thinking along these lines:
WbStambestand.WsStam.Range("AA2", Selection, Selection.End(xlDown)).Copy WbIjk.WsIjk.range("A1").paste
Workbooks("WbStambestand").Worksheets("WsStam").Range("AA2", Selection, Selection.End(xlDown)).Copy
However, these don't function. I'm hoping you guys can help me along. Much appreciated.
FYI, these are my variables (they are declared). The Ijk ones are the paste destination.
Dim WbStambestand, WbIjk As Workbook
Dim WsIjk, WsStam As Worksheet
Set WsIjk = ActiveSheet
Set WbIjk = ActiveWorkbook
Set WsIjk = ActiveSheet
Set WbIjk = ActiveWorkbook
Set WbStambestand = Workbooks.Open(stam)
Set WsStam = WbStambestand.Worksheets("stambestand")
Try this
Sub abc()
Dim WbStambestand As Workbook
Dim WbIjk As Workbook
Dim WsIjk As Worksheet
Dim WsStam As Worksheet
Dim LastRow As Long
Dim stam As String
stam = "C:\Users\Admin\Desktop\Stambestand.xlsm" ' path with complete file name with extension.
Set WsIjk = ActiveSheet
Set WbIjk = ThisWorkbook ' workbook which has current code
Set WbStambestand = Workbooks.Open(stam)
Set WsStam = WbStambestand.Worksheets("stambestand")
LastRow = WsStam.Range("AA2").End(xlDown).Row
WsStam.Range("AA2:AA" & LastRow).Copy WsIjk.Range("B3")
End Sub

Copy specific entire column from file 1 to 2

Hello I'm trying to copy columns C, R, W,X from file 1 to file 2 with below code but keep getting an error. My VBA knowledge isn't that good yet but probably has to do with the range setting? I've tried multiple ways but can't get it to work.
Am I using the right setting or should I use another action to get the specific columns?
Sub PFS()
Dim wbCopy As Workbook
Dim wsCopy As Worksheet
Dim rngCopy As Range
Dim wbPaste As Workbook
Dim wsPaste As Worksheet
Dim rngPaste As Range
Set wbPaste = ActiveWorkbook
Set wbCopy = Workbooks.Open("path to copy")
Set wsCopy = wbCopy.Worksheets("Blad1")
Set rngCopy = wsCopy.Range("d, e").EntireColumn
Set wsPaste = wbPaste.Worksheets("PFS")
Set rngPaste = wsPaste.Range("a1")
rngCopy.Copy
rngPaste.PasteSpecial
Workbooks.Application.CutCopyMode = False
Application.DisplayAlerts = False
wbCopy.Save
wbCopy.Close
End Sub
Solutions to copy entire column.
Sub copy()
Dim wb As Workbook
Dim wbNew As Workbook
Dim ws As Worksheet
Dim wsNew As Worksheet
Set wb = ActiveWorkbook
Set ws = wb.Sheets("old")
Set wbNew = Workbooks("Book.xlsx")
Set wsNew = wbNew.Sheets("new")
ws.Columns(3).copy
wsNew.Columns(3).Insert Shift:=xlToRight
ws.Columns(18).copy
wsNew.Columns(18).Insert Shift:=xlToRight
ws.Columns(23).copy
wsNew.Columns(23).Insert Shift:=xlToRight
ws.Columns(24).copy
wsNew.Columns(24).Insert Shift:=xlToRight
Set wsNew = Nothing
Set wbNew = Nothing
Set ws = Nothing
Set wb = Nothing
End Sub

How to compare two workbooks

I like to compare two workbooks with different sheet, How can I set as an object for workbook1-->Sheet1 , workbook2--->(sheet1)
I can able to compare the worksheet inside the same workbook, Butwhere as if I want to select the sheet the "getopenfilename". how can i assign the name as an object.
code:
Dim tabWb As Workbook 'Workbook2
Dim tabWS As Worksheet 'analysing worksheet
Filename = Application.GetOpenFilename("Excel files (*.xls*),*.xl*", Title:="Open data")
Set wb = ActiveWorkbook
Set tabWS = Sheets("Tabelle1")
Dim bsmWS As Worksheet ' workbook1
Set bsmWS = Sheets("Sheet1") ' currentworksheet
Workbook1(sheet1) is my current workbook and work sheet, I like to get some data from the another workbook2(sheet1). How can i make an object for the both worksheets.I am getting compileing failure in "set bsmws"
Sub test()
Dim strFileName as String
Dim wbTarget As Workbook
Dim wbSource As Workbook
Dim wsTarget As Worksheet
Dim wsSource As Worksheet
strFileName = Application.GetOpenFilename("Excel files (*.xls*),*.xl*", Title:="Open data")
Set wbSource = ThisWorkbook
Set wbTarget = Workbooks.Open(strFileName)
Set wsSource = wbSource.Worksheets("Sheet1")
Set wsTarget = wbTarget.Worksheets("Sheet1")
'to copy from Target - > Source
wsTarget.Range("B2").Resize(5, 5).Copy wsSource.Range("B2")
'etc.
End Sub

Copy-paste selection from one workbook to another

I am trying to automate a process that involves copy pasting data from one workbook to a new workbook. I've been able to put together the code shows below from snippets found in the forums here, and other places. However, I am receiving a "Runtime error 1004" when attempting to run the procedure. Any suggestions?
Option Explicit
Dim wbI As Workbook, wbO As Workbook, wsI As Worksheet, wsO As Worksheet
Dim wbName As String
Sub transferit()
wbName = InputBox("Enter name", "name")
'~~> Source/Input Workbook
Set wbI = ThisWorkbook
'~~> Set the relevant sheet from where you want to copy
Set wsI = wbI.Sheets("Sheet1")
'~~> Destination/Output Workbook
Set wbO = Workbooks.Add
'~~> Set the relevant sheet to where you want to paste
Set wsO = wbO.Sheets("Sheet1")
With wbO
'~~>. Save the file
.SaveAs Filename:="D:\Documents\Output\wbName
End With
With wsI
Call RangeSelectionPrompt
Selection.Copy
End With
With wsO
'~~> Paste it in say Cell A1. Change as applicable
.Range("A1").PasteSpecial xlPasteValues
End With
End Sub
Sub RangeSelectionPrompt()
Dim rng As Range
Set rng = Application.InputBox("Select a range", "Obtain Range Object", Type:=8)
End Sub
Do you need global variables? It is unlikely, move them inside the Sub. You didn't concatenate the filename rightly for the .SaveAs, and you did not copy what you was expecting to...
Here is my code, ther is still some control of errors missing.
Sub transferit()
Dim wbI As Workbook, wbO As Workbook, wsI As Worksheet, wsO As Worksheet
Dim wbName As String
wbName = InputBox("Enter name", "name")
'~~> Source/Input Workbook
Set wbI = ThisWorkbook
'~~> Set the relevant sheet from where you want to copy
Set wsI = wbI.Sheets("Sheet1")
'~~> Destination/Output Workbook
Set wbO = Workbooks.Add
'~~> Set the relevant sheet to where you want to paste
Set wsO = wbO.Sheets("Sheet1")
With wbO
'~~>. Save the file
.SaveAs Filename:="D:\Documents\Output\" & wbName
End With
RangeSelectionPrompt.Copy
With wsO
'~~> Paste it in say Cell A1. Change as applicable
.Range("A1").PasteSpecial xlPasteValues
End With
End Sub
Function RangeSelectionPrompt() As Range
Set RangeSelectionPrompt = Application.InputBox("Select a range", "Obtain Range Object", Type:=8)
End Function
RangeSelectionPrompt.Copy
With wsO
'~~> Paste it in say Cell A1. Change as applicable
.Range("A1").PasteSpecial xlPasteValues
End With
End Sub
Function RangeSelectionPrompt() As Range
Set RangeSelectionPrompt = Application.InputBox("Select a range", "Obtain Range Object", Type:=8)
End Function