Copy cell range between workbooks from same folder - vba

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.

Related

Combine Worksheets Into New Workbook Based on Criteria and Save

I have a workbook made up with 100+ worksheets. These worksheets have account number/names/days in the name of the worksheet.
The naming convention for the worksheets follows this pattern of AccountNumber/AccountName/Description:
11-Greg-Monday
11-Greg-Tuesday
11-Greg-Friday
38-Rachel-Sunday
38-Rachel-Tuesday
38-Rachel-Saturday
I would like Excel to loop through all the worksheets, and extract all of the 11-Greg worksheets and save into a new workbook named 11-Greg, and then do the same for 38-Rachel, etc. I have a list of the account numbers/names on a worksheet named "Accounts" in the workbook.
Would it be possible to maintain the formulas after the extract of the worksheets, and formatting like column widths?
I found this code that might work to start, but I don't know how to reference the list on the "Accounts" tab to loop through for the account names?
Dim wb as Workbook, sht as WorkSheet
Dim strFileName As String
'Copy sheet as a new workbook
ActiveWorkbook.Sheets("Sheet1").Copy
Set wb = ActiveWorkbook
Set sht = wb.Sheets(1)
'SaveAs
strFileName = Application.GetSaveAsFilename(wb.Name) & "xlsx"
If strFileName = "False" Then Exit Sub 'User Canceled
wb.SaveAs Filename:=strFileName
The easiest way would be to create a list of the names you want to stack on a separate list. set that list as a range and then loop through each cell checking to see if the x letters of the sheet name match. something like this
Sub stacksheets()
Dim rng As Range, cCell As Range
Dim ws As Worksheet
Dim wb As Workbook, wb2 As Workbook
Dim shName As String
Set rng = ActiveWorkbook.Sheets("list").Range("a1:a2") ''this would be the list of names
Set wb2 = ActiveWorkbook ''remembering activeworkbook so can return
For Each cCell In rng
shName = Left(cCell.Value, 5) ''this needs to be the minimum letters from each name that is unique
Set wb = Workbooks.Add
For Each ws In wb2.Sheets
If InStr(ws.Name, shName) > 0 Then ''checks for name in sheet name
ws.Copy after:=wb.Sheets(1)
wb2.Activate
End If
Next ws
wb.SaveAs (wb2.Path & "\" & cCell.Value) '' saves workbook as list name
Next cCell
End Sub

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 Data from one open Workbook into another open workbook

Following code:
Sub CopyData()
Dim Wb1 As Workbook, wb2 As Workbook
For Each wB In Application.Workbooks
If Left(wB.Name, 21) = "Open Order Monitoring" Then
Set Wb1 = wB
Exit For
End If
Next
Set wb2 = ThisWorkbook
Wb1.Sheets(1).Range("A2").Range(.Cells(1, 1), .End(xlDown).Cells(1, 39)).Copy wb2.Sheets(2).Range("B5")
End Sub
The macro should copy data from a open workbook with variable name (open order monitoring[...]) and paste into the second sheet of the workbook I run the macro from.
But the line:
Wb1.Sheets(1).Range("A2").Range(.Cells(1, 1), .End(xlDown).Cells(1, 39)).Copy wb2.Sheets(2).Range("B5")
gives me an error. can someone solve this problem?
since:
it's always safer to use fully qualified range references (down to workbook and worksheet ones). especially when you're dealing with multiple workbooks and/or worksheets
should you only be interested in pasting values, it's faster (and safer, too) use Range1.value = Range2.Value instead of .Copy() method of Range object.
then, here follows a possible code:
Option Explicit
Sub CopyData()
Dim Wb1 As Workbook, wb2 As Workbook, wB As Workbook
Dim rngToCopy As Range
For Each wB In Application.Workbooks
If Left(wB.Name, 21) = "Open Order Monitoring" Then
Set Wb1 = wB
Exit For
End If
Next
If Not Wb1 Is Nothing Then '<~~ check if you actually found the needed workbook
Set wb2 = ThisWorkbook
With Wb1.Sheets(1)
Set rngToCopy = .Range("A2:AM2", .Cells(.Rows.Count, "A").End(xlUp))
End With
wb2.Sheets(2).Range("B5:AN5").Resize(rngToCopy.Rows.Count).Value = rngToCopy.Value
End If
End Sub
Pls try with below code
Sub CopyData()
Dim Wb1 As Workbook, wb2 As Workbook, wb As Workbook
Set wb2 = ThisWorkbook
For Each wb In Workbooks
If Left(wb.Name, 21) = "Open Order Monitoring" Then
Set Wb1 = wb
Exit For
End If
Next
Wb1.Sheets(1).Range("A2:AM2").Copy wb2.Sheets(2).Range("B5") 'Edited here
End Sub

Transferring data into an open workbook from a closed workbook without having to specify the file path, considering that both are in the same folder

How can I modify the below code to do so? I tried my luck with ThisWorkbook.Path. New to VB, any help would be much appreciated.
Private Sub CommandButton1_Click()
Dim WB1 As Workbook
Dim WB2 As Workbook
Set WB1 = ActiveWorkbook
Set WB2 = Workbooks.Open("C:\Users\joseph\Desktop\Required Files\Almost final\ RawData.xlsm")
WB1.Sheets("CR Details").Columns("A:AW").Value = WB2.Sheets("sheet1").Columns("A:AW").Value
WB2.Close
End Sub
Try:
Set WB2 = Workbooks.Open(WB1.Path & "\RawData.xlsm")
if WB2 is in the same folder as WB1.
If the macro is running from a third (separate) workbook in the same location as WB2 then:
Set WB2 = Workbooks.Open(ThisWorkbook.Path & "\RawData.xlsm")

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...