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

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")

Related

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.

Browse excel workbook and open, then use workbook in code

I need a code to browse for a workbook on a server, the workbook needs to be opened and I need to make references to this workbook in my already existing code.
The code below works until I want to set Wb2 as the the workbook I opened using the code.
Sub openfile()
Dim strFilePath As String
Dim Wb1 As Workbook
Dim Wb2 As Workbook
Set Wb1 = ActiveWorkbook
strFilePath = Application.GetOpenFilename
If strFilePath = "False" Then Exit Sub 'Pressed cancel
Workbooks.Open (strFilePath)
Set Wb2 = strFilePath
Wb2.Sheet3.Activate
End Sub
strFilePath is just a string, not a workbook. You can get the workbook object from the .Open method:
Set Wb2 = Workbooks.Open(strFilePath)
Your next line will cause an error too since Sheet3 is (I assume) an object in your workbook but not a property of the workbook class. Use
Wb2.Worksheets("Sheet3").Activate 'or whatever it's named
instead

Using Personal.xlsb - referencing active workbook in VBA

I have a number of scripts that are in a module in my Personal.xlsb file. It's kept hidden, but in this script, the idea is that you run it from within a different workbook each time. It opens a separate workbook (source.xlsx), copies a range from it, pastes into the original workbook, and then closes source.xlsx.
When it comes to the "ThisWorkbook.ActiveSheet.Paste" part, it's pasting it into the Personal.xlsb workbook instead of the target workbook that is actually open and visible. How can I make sure it's being pasted in the right workbook? The workbook's filename will always be different, so I can't specify a path or anything like that.
Sub CopyData()
Application.DisplayAlerts = False
Dim wbSource As Workbook
Set wbSource = Workbooks.Open(Filename:="source.xlsx", UpdateLinks:=3)
wbSource.Sheets(1).Range("A1:X105").Copy
ThisWorkbook.ActiveSheet.Paste
wbSource.Close
Application.DisplayAlerts = True
Call CopyCFormat
End Sub
Don't use ThisWorkbook in most cases, as it references the workbook that the macro is stored in (in this case, personal.xlsb).
Instead, you can use ActiveWorkbook to refer to whichever workbook has focus at the time the macro is run. You can also assign ActiveWorkbook to a variable for easier reference.
Sub CopyData()
Application.DisplayAlerts = False
Dim wbSource As Workbook
Dim wbTarget as Workbook
Set wbTarget = ActiveWorkbook
Set wbSource = Workbooks.Open(Filename:="source.xlsx", UpdateLinks:=3)
wbSource.Sheets(1).Range("A1:X105").Copy
wbTarget.ActiveSheet.Paste
wbSource.Close
Application.DisplayAlerts = True
Call CopyCFormat
End Sub
You could also reference the active sheet without specifying which workbook it's in, as:
Dim wbSource As Workbook
Dim shtTarget as Worksheet
Set shtTarget = ActiveSheet
Set wbSource = Workbooks.Open(Filename:="source.xlsx", UpdateLinks:=3)
wbSource.Sheets(1).Range("A1:X105").Copy
shtTarget.ActiveSheet.Paste
Luck!
If I understand it, you should just add another workbook variable.
Sub CopyData()
Dim mainWB As Workbook
Dim mainWS As Worksheet
Set mainWB = ActiveWorkbook
Set mainWS = mainWB.Sheets(1) ' Change this to whatever you need it to be
Application.DisplayAlerts = False
Dim wbSource As Workbook
Set wbSource = Workbooks.Open(Filename:="source.xlsx", UpdateLinks:=3)
wbSource.Sheets(1).Range("A1:X105").Copy
mainWS.Paste
wbSource.Close
Application.DisplayAlerts = True
Call CopyCFormat
End Sub

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

Runtime Error 9 - Subscript Out Of Range

I'm trying to create a form based interface using the VBA. I ran into a problem which I cannot figure it out. So in my code, I try to create a new workbook then save it. After that it will scan through the checkbox and see which one is getting selected. If it get selected, it will create a new sheet and copy the pre-made template. The user will open another workbook that they want and it will copy the information from that workbook to the workbook that just made. Here is the code for retrieving the data:
Sub mil10_data()
Dim NewWB As Workbook
Dim thisWB As Workbook
Dim wb As Workbook
Dim Ret
Set thisWB = ThisWorkbook
Set NewWB = ActiveWorkbook
With NewWB
'Copy the pre-made template to new workbook
thisWB.Sheets("Data 10").Range("A1:AZ3000").Copy NewWB.Sheets(ActiveSheet.Name).Range("A1:AZ3000")
'Retriving the data
Ret = Application.GetOpenFilename("Excel Files (*.CSV), *.CSV", Title:="Select File To Be Opened")
If Ret = False Then Exit Sub
Set wb = Workbooks.Open(Ret)
**'This is where the error is show up
wb.Sheets(ActiveSheet.Name).Range("E21:E2136").Copy NewWB.Sheets(ActiveSheet.Name).Range("C2:C2117")**
wb.Close SaveChanges:=False
Set wb = Nothing
Set NewWB = Nothing
End With
End Sub
I figure that maybe because there are three workbooks open, it didn't know which one is the active workbook, but that is not the case. I tested by using the MsgBox and it display the right workbook and worksheeet name that I want. If I change the ActiveSheet.Name to the actual sheet name, it works, but I don't want to use that method. I have different worksheets that need to be created, so I prefer using the ActiveSheet.Name. Anyone know why it didn't work? I would really appreciate the help. Thank you!
ActiveWorkbook and ActiveSheet are really clumsy ways of getting user input. You should save in variables the properties that are you interested in (as soon as possible), and then stop referring them directly.
In your case, the code may look like
Sub mil10_data()
Dim NewWB As Workbook
Dim thisWB As Workbook
Dim wb As Workbook
Dim Ret
Dim active_sheet_name As String
Set thisWB = ThisWorkbook
Set NewWB = ActiveWorkbook
Let active_sheet_name = Application.ActiveSheet.Name
With NewWB
'Copy the pre-made template to new workbook
thisWB.Sheets("Data 10").Range("A1:AZ3000").Copy NewWB.Sheets(active_sheet_name).Range("A1:AZ3000")
'Retrieving the data
Ret = Application.GetOpenFilename("Excel Files (*.CSV), *.CSV", Title:="Select File To Be Opened")
If Ret = False Then Exit Sub
Set wb = Workbooks.Open(Ret)
wb.Sheets(active_sheet_name).Range("E21:E2136").Copy NewWB.Sheets(active_sheet_name).Range("C2:C2117")
wb.Close SaveChanges:=False
Set wb = Nothing
Set NewWB = Nothing
End With
End Sub
If you still get "Subscript out of range" error, that means that you either don't select correctly the ActiveSheet before running the script, or that sheet does not exist in the workbook wb.