VBA: Set Workbook, and Activate Workbook Error - vba

There is a simple task that I cannot figure out. I would like to Copy Value of wb1.ws1 and paste into wb2.ws2
This is what I have tried.
Dim wb1 as Workbook, wb2 as Workbook
Dim ws1 as Worksheet, ws2 as Worksheet
'Open wb1 and ws1 and set
Set wb1 = Workbooks.Open("R:\Workbook1.xlsx")
Set ws1 = Sheet("Sheet1")
'Open wb2 and ws2 and set
Set wb2 = Workbooks.Open("R:\Workbook2.xlsx")
Set ws2 = Sheet("Sheet2")
'Copy Value of wb1.ws1 and paste into wb2.ws2
wb1.ws1.Range("A1").Copy wb2.ws2.Range("A1")
Why does this not work? It seems like I cannot select a range by specifying wb1.ws1.Range(XYZ)
It forces me to separate the lines into
wb1.Activate
ws1.Select
Range("A1").Copy
wb2.Activate
ws2.Select
Range("A1").Paste
Seems terribly inefficient but I don't know how to eliminate the Activate-Select nightmare

Once you set a worksheet the workbook parent is already defined. so declare the parent at the Set
Dim wb1 as Workbook, wb2 as Workbook
Dim ws1 as Worksheet, ws2 as Worksheet
'Open wb1 and ws1 and set
Set wb1 = Workbooks.Open("R:\Workbook1.xlsx")
Set ws1 = wb1.WorkSheets("Sheet1")
'Open wb2 and ws2 and set
Set wb2 = Workbooks.Open("R:\Workbook2.xlsx")
Set ws2 = wb2.Worksheets("Sheet2")
'Copy Value of wb1.ws1 and paste into wb2.ws2
ws1.Range("A1").Copy ws2.Range("A1")

Related

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!

Excel VBA To Set Variable To Worksheet

I am trying to set a Worksheet variable to the Activesheet but I am getting an error of
Object variable or with block variable not set
This is my syntax - what is the appropriate way of doing this?
Sub TestIt()
Dim ws2 As Worksheet, ws1 As Worksheet
ws2 = ActiveWorkbook.ActiveSheet
ws1 = "Belgium"
Debug.Print ws2
Debug.Print ws1
End Sub
1- You need to use the Set keyword to assign object variables.
2- You cannot assign a sheet variable to a string directly, but you need to index through the Worksheets collection
3- You cannot Debug.Print a worksheet, but only its name or some cell inside
Sub TestIt()
Dim ws2 As Worksheet, ws1 As Worksheet
Set ws2 = ActiveWorkbook.ActiveSheet
Set ws1 = ThisWorkbook.Worksheets("Belgium")
Debug.Print ws2.Name
Debug.Print ws1.Cells(1,1).Value
End Sub

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

Copy cell range between workbooks from same folder

I currently have:
Sub Ranger()
Dim rng As Range
Dim WB1 As Workbook, WB2 As Workbook, ActiveWB As Workbook
Dim WS1 As Worksheet, WS2 As Worksheet
Dim FName As String
FName = "General Text"
Set WB1 = ThisWorkbook
Set WS1 = WB1.Sheets("Sheet1")
Set WB2 = Workbooks.Open(FileName:=FName)
Set WS2 = WB2.Sheets(1)
With WS2
Set rng = .Range(Range("A1"), Range("A5"))
End With
With WS1
rng.Copy .Cells(1, 1)
End With
WB2.Close
End Sub
Which aims to copy the range A1:A5 in the newly opened workbook into the original workbook (ThisWorkbook). It currently opens the second workbook but does not copy anything into the first workbook. There are also no errors and I would like to avoid using specific names in setting WB1/WB2 as WB2 could be either .xls or .xlsx
Try this, it works for me:
Sub Ranger()
Dim rng As Range
Dim WB2 As Workbook
Dim FName As String
FName = "D:\Tuchanka\Temp\pelda.xlsx"
Set WB2 = Workbooks.Open(Filename:=FName)
ThisWorkbook.Worksheets(1).Range("A1:A5").Value = WB2.Worksheets(1).Range("A1:A5").Value
WB2.Close
End Sub
Using variables and with statements is pointless in your situation, as instead of making your code simpler and easier to read, they just add a lot of gibberish and make your code seem way too complex. Only use these if you need them.

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