How to compare two workbooks - vba

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

Related

Trying to copy data from a closed workbook to a current workbook

Sub Testing()
Dim Target_Workbook As Workbook
Dim Source_Workbook As Workbook
Dim Target_Path As String
Target_Path = "Sample.xlsx"
Set Target_Workbook = Workbooks.Open(Target_Path)
Set Source_Workbook = ThisWorkbook
Source_data = Source_Workbook.Sheets(1).Range("A1:Y74").Copy
Target_Workbook.Sheets(1).Range("A1").Activate
Source_Workbook.Save
Target_Workbook.Save
Target_Workbook.Close False
MsgBox "Task Completed"
End Sub
use below code, populate your source and target excel file names and call this code
Sub CopyWorkbook(Sourceworkbook, TargetWorkbook)
Dim sh As Worksheet, wb As Workbook, wbSource As Workbook
Dim SourcefileName As String
SourcefileName = Sourceworkbook
Set wbSource = Workbooks.Open(Sourceworkbook)
Set wb = Workbooks(TargetWorkbook)
For Each sh In Workbooks(SourcefileName).Worksheets
sh.Copy After:=wb.Sheets(wb.Sheets.count)
Next sh
wbSource.Close
End Sub
e.g TargetWorkbook = "TwoSheet_Compare V2.0.xlsm" and
SourceWorkbook = "sourceFile.xlsx"

Looping through all files in a folder (encountering run time error '-2147221080 (800401a8)')

I am running a code that aims to pull in data from all workbooks within a specific folder. I have written the code for the loop separate from the code for the pulling in of data.
Sub FixedIncomeLoop()
Dim file As Variant
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim folderpath As String
Dim ws As Worksheet
Dim ws2 As Worksheet
Set wb1 = ActiveWorkbook
Set ws = ActiveSheet
folderpath = "C:\Users\Wei Hong\Documents\Trade Tickets\"
file = Dir(folderpath)
While (file <> "")
Set wb2 = Workbooks.Open(file)
Set ws2 = wb2.Worksheets("Trade Ticket")
Call FixedIncome(wb1, ws, ws2)
wb2.Close SaveChanges:=False
Set wb2 = Nothing
Wend
End Sub
Private Sub FixedIncome(wb1 As Workbook, ws As Worksheet, ws2 As Worksheet)
Dim ws3 As Worksheet
Set ws3 = wb1.Worksheets("Constants") <-- facing error
ws.Activate
...
End Sub
I am facing the runtime error above. Not sure why!

Copy form External Workbook XLDOWN into Current workbook

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

VBA Copy data from one sheet to another

I'm pretty new to VBA and need some help with a project. I need to write a macro that reads the Sheet Name in Column C, and pastes the values from a source workbook to a range in a target workbook, which is specified in Column D.
So for example, it needs to copy the data in Sheet2 of Myworkbook book, and paste it into range of Theirworkbook Sheet2. The place where the range and sheet number information is stored in a separate workbook.
Edit: I've added a picture of what wbOpen looks like. This is it here.
Option Explicit
Sub PasteToTargetRange()
Dim arrVar As Variant 'stores all the sheets to get the copied
Dim arrVarTarget As Variant 'stores names of sheets in target workbook
Dim rngRange As Range 'each sheet name in the given range
Dim rngLoop As Range 'Range that rngRange is based in
Dim wsSource As Worksheet 'source worksheet where ranges are found
Dim wbSource As Workbook 'workbook with the information to paste
Dim wbTarget As Workbook 'workbook that will receive information
Dim strSourceFile As String 'location of source workbook
Dim strTargetFile As String 'location of source workbook
Dim wbOpen As Workbook 'Current open workbook(one with inputs)
Dim wsRange As Range 'get information from source workbook
Dim varRange As Range 'Range where values should be pasted
Dim i As Integer 'counter for For Loop
Dim wbkNewSheet As Worksheet 'create new worksheet if target workbook doesn't have
Dim wsTarget As Worksheet 'target workbook worksheet
Dim varNumber As String 'range to post
Set wbOpen = Workbooks.Open("WorkbookWithRanges.xlsx")
'Open source file
MsgBox ("Open the source file")
strSourceFile = Application.GetOpenFilename
If strSourceFile = "" Then Exit Sub
Set wbSource = Workbooks.Open(strSourceFile)
'Open target file
MsgBox ("Open the target file")
strTargetFile = Application.GetOpenFilename
If strTargetFile = "" Then Exit Sub
Set wbTarget = Workbooks.Open(strTargetFile)
'Activate transfer Workbook
wbOpen.Activate
Set wsRange = ActiveSheet.Range("C9:C20")
Set arrVarTarget = wbTarget.Worksheets
For Each varRange In wsRange
If varRange.Value = 'Target workbook worksheets
varNumber = varRange.Offset(0, -1).Value
Set wsTarget = X.Offset(0, 1)
wsSouce.Range(wsTarget).Value = varNumber
Else
wbkNewSheet = Worksheets.Add
wbkNewSheet.Name = varRange.Value
End If
Next
End Sub
Something like this (untested but should give you an idea)
Sub PasteToTargetRange()
'....omitted
Set wsRange = wbOpen.Sheets(1).Range("C9:C20")
For Each c In wsRange
shtName = c.Offset(0, -1).Value
Set wsTarget = GetSheet(wbTarget, shtName) 'get the target sheet
wbSource.Sheets(shtName).Range(c.Value).Copy wsTarget.Range(c.Value)
Next
End Sub
'Get a reference to a named sheet in a specific workbook
' By default will create the sheet if not found
Function GetSheet(wb As Workbook, ws As String, Optional CreateIfMissing As Boolean = True)
Dim rv As Worksheet
On Error Resume Next 'ignore eroror if no match
Set rv = wb.Worksheets(ws)
On Error GoTo 0 'stop ignoring errors
'sheet wasn't found, and should create if missing
If rv Is Nothing And CreateIfMissing Then
Set rv = wb.Worksheets.Add(after:=wb.Worksheets(wb.Worksheets.Count))
rv.Name = ws
End If
Set GetSheet = rv
End Function

dynamically selecting workbook and worksheet

I have to copy and paste specific columns from one worksheet in workbook1 to another worksheet in the same workbook1 or it may be workbook2 also. I mean I want to dynamically select the source workbook and worksheet and also the destination workbook and worksheet. I must be able to select the columns that I want to copy also dynamically.
I've tried this:
Dim thisWb As Workbook
Dim destWb As String
Dim destSheet As Worksheet, FromSheet As Worksheet
Dim FromBook As String
Set thisWb = ThisWorkbook
Set destSheet = thisWb.ActiveSheet
FromBook = Application.GetOpenFilename
If FromBook = "False" Then Exit Sub
destWb = Application.GetOpenFilename
Workbooks.Open Filename:=destWb
Set FromSheet = destWb.Worksheets("Sheet1")
Set sourcecolumn = Workbooks("FromBook").Worksheets("sheet1").Columns("A")
Set targetcolumn = Workbooks("destWb").Worksheets("sheet2").Columns("B")
sourcecolumn.Copy Destination:=targetcolumn
There is an "invalid specifier" compile time error and destwb is highlighted on this line:
Set FromSheet = destwb.Worksheets("Sheet1")
i have tried doing this with static workbooks,worksheets,column names and it works.
Dim sourcecolumn As Range, targetcolumn As Range
Set sourcecolumn = Workbooks("Book1.xlsm").Worksheets("sheet1").Columns("A")
Set targetcolumn = Workbooks("Book1.xlsm").Worksheets("sheet2").Columns("B")
sourcecolumn.Copy Destination:=targetcolumn
The problem is i want to select the workbooks,worksheets and columns dynamically...
There is some type confusion in your variable declarations. On the line on which you get the error, you're trying to assign a Worbook object reference to a String variable. This doesn't work because those are two different data types.
Here's a fix. I commented the lines that I changed:
Dim pthFromBook As String
Dim pthDestWb As String ' to store the path of the workbook file
Dim thisWb As Workbook
Dim destWb As Workbook ' to store reference to workbook object
Dim destSheet As Worksheet, FromSheet As Worksheet
Set thisWb = ThisWorkbook
Set destSheet = thisWb.ActiveSheet
pthFromBook = Application.GetOpenFilename
If pthFromBook = "False" Then Exit Sub
pthDestWb = Application.GetOpenFilename ' first get the path
Set destWb = Workbooks.Open(pthDestWb) ' then open the workbook
Set FromSheet = destWb.Worksheets("Sheet1")
Finally, I don't know if it's a typo, but on the last line above, there appears to be some confusion between From and Dest sheets/workbooks...