Copy named ranges from one workbook to another? - excel

I have a Workbook A which has three named ranges Alpha, Beta, Charlie.
I would like to copy values of those name ranges to Workbook B which is stored in the SharePoint location. Workbook B also has three named ranges Alpha, Beta, Charlie.
Workbook A is not stored in the SharePoint and it comes in named differently each time so I cannot predefine it's name whereas Workbook B always has the same name.
I tried below example.
Sub CopyPaste()
Dim sourceWb As Workbook
Dim openWb As Workbook
Set openWb = ThisWorkbook
Set sourceWb = Workbooks.Open(Filename:="https://SharePointLocationToWorkbookB.xlsm ", ReadOnly:=False, UpdateLinks:=False)
sourceWb.Range("Alpha").Value = openWb("sheet1").Range("Alpha").Value
sourceWb.Range("Beta").Value = openWb("sheet1").Range("Beta").Value
sourceWb.Range("Charlie").Value = openWb("sheet1").Range("Charlie").Value
End Sub

Your sourceWb is confusingly-named... Maybe try something like this:
Sub CopyPaste()
Dim sourceWs As Worksheet, destWs As Worksheet, rn
Set sourceWs = ThisWorkbook.Worksheets("Sheet1")
Set destWs = Workbooks.Open(Filename:="https://SharePointLocationToWorkbookB.xlsm", _
ReadOnly:=False, UpdateLinks:=False).Worksheets("Sheet1") 'for example
For Each rn In Array("Alpha", "Beta", "Charlie")
destWs.Range(rn).Value = sourceWs.Range(rn).Value
Next rn
destWs.Parent.Close True 'save changes
End Sub

Related

Sum values from one Worksheet in another Worksheet

I am trying to sum values from my original worksheet in specific cells in my newly created worksheet, which has a template to fill out.
When I used macro recorder, it references the worksheet name, which would not be useful as the worksheet name changes depending on which worksheet I am working in when I run the code.
So I tried changing the worksheet name to a variable "XCXX".
The first argument works so I thought everything was okay, however, on the second argument, it keeps trying to open a file, when it should simply go back to XCXX and pull the values.
Is it a problem with my activesheet changing?
Sub AddWorkbooks()
Dim ChangeOrder As Range
Dim XCXX As Worksheet
Dim CoForm As Worksheet
Set XCXX = ActiveSheet
Set CoForm = Worksheets("+CO Form+")
'Set wbNew = Workbooks.Add
CoForm.Copy After:=Sheets(ActiveSheet.Index)
With CoForm
Range("A6:D6").Select
ActiveCell.FormulaR1C1 = XCXX.Range("D2").Value
Range("AD81").Select
ActiveCell.FormulaR1C1 = "='XCXX'!R[-64]C[-24]+'XCXX'!R[-64]C[-23]"
End With
End Sub
This should be close:
Sub AddWorkbooks()
Dim ChangeOrder As Range
Dim XCXX As Worksheet, wb As Workbook
Dim CoForm As Worksheet, CoFormCopy As Worksheet
Set wb = ActiveWorkbook
Set XCXX = ActiveSheet
Set CoForm = wb.Worksheets("+CO Form+")
CoForm.Copy After:=XCXX
Set CoFormCopy = XCXX.Next 'the copy of "+CO Form+"
With CoFormCopy 'assuming you want to work with the copy?
.Range("A6:D6").Value = XCXX.Range("D2").Value
.Range("AD81").FormulaR1C1 = _
Replace("='<nm>'!R[-64]C[-24]+'<nm>'!R[-64]C[-23]", "<nm>", XCXX.Name)
End With
End Sub
Note when using With you need to use a period to link (eg) Range() with the object used in the With statement, otherwise it defaults to the active sheet.
Also generally there's no need to select a range to do something with it.

Set Worksheet Name Equal To A Variable

I want to be able to open up a second data source (workbook) and select the sheet within the second workbook which is equal to the name in a cell in my first workbook.
So far I have:
Dim src As Workbook
Dim src2 As Workbook
Dim shtno As String
Dim ws As Worksheet
Set src = ActiveWorkbook
shtno = Sheets("CONTROL").Range("G3").Value
Set ws = Sheets(shtno)
Workbooks.Open Filename:= 'file link
Set src2 = ActiveWorkbook
ws.Select
Where 'ws.Select' should select my sheet required. Where am I going wrong?
Thanks.
BigBen is correct.
You need to move the Set ws statement down below the Workbooks.Open statement then:
Set ws = src2.Sheets(shtno)
ws.Select
NOTE:
once you Open the new workbook, it becomes the Active workbook
you can only Select a worksheet on the Active workbook

How to set a workbook to a variable name?

How do I set a workbook to a variable name to be used later on in the sub?
I'm trying to open a workbook (PriceFile) and set values in this workbook to values in the original workbook (TestFile). I can open PriceFile but can't name the workbook.
Public Sub Get_Sum_Assured()
Dim TestFile As Workbook
Dim PriceFile As Workbook
Dim PriceFileName As String
Dim Test_Cases As Integer
Dim FirstTest As Integer
Dim CommDate As Date
Dim DOB As Date
Dim MonthPrem As Long
Dim SumAssured As Long
Dim TestCount As Integer
Set TestFile = ThisWorkbook
Call Open_Pricing_File
TestFile.Activate
PriceFileName = Range("Pricing_File").Value
Set PriceFile = Workbooks(PriceFileName)
Open_Pricing_File opens the file named in "Pricing_File" and when I've stepped through this works. When I try and set PriceFile to this workbook, the code falls over on this last line.
The following macro opens two workbooks and gives them the name "wb1" and "wb2".
At the end the value from cell A1 is copied from "wb1" to "wb2".
Sub copyValue_wb1wb2()
Dim wb1 As Workbook
Dim wb2 As Workbook
'wb1 (workbook1)
Workbooks.Open Filename:="C:\Data\ExcelFile1.xlsm", Local:=True, ReadOnly:=False
Set wb1 = ActiveWorkbook
'wb2 (workbook2)
Workbooks.Open Filename:="C:\Data\ExcelFile2.xlsm", Local:=True, ReadOnly:=False
Set wb2 = ActiveWorkbook
'Now you can jump between workbooks like:
wb1.Activate
wb2.Activate
'You can insert a value from wb1 to wb2 like:
wb2.Sheets(1).Range("A1").Value = wb1.Sheets(1).Range("A1").Value
End Sub

Copying data from multiple worksheets to multiple workbooks

I am attempting to copy data from multiple worksheets in an excel file to multiple files that have a template in them. So one excel file has 1500 worksheets with unique names and there exist 1500 excel files with the same name as the worksheets. I am trying to copy data (typically A1:A50) from each worksheet to another file of the same name. The target excel file has two worksheets in it and this data needs to go into each one: cells B5:B55 in "Inside Page", and cells C5:C55 in "Back Page."
Any help would be much appreciated!
Lalitha
This should get you started. The only issue may be performance if you have 1500 (!) worksheets.
Option Explicit
Public Sub splitsheets()
Dim srcwb As Workbook, trgwb As Workbook
Dim ws As Worksheet, t1ws As Worksheet, t2ws As Worksheet
Dim rng1 As Range, rng2 As Range
Dim trgnm As String
Dim fpath As String
Application.ScreenUpdating = False
'--> Set this to the location of the target workbooks
fpath = "H:/copytest/"
Set srcwb = ThisWorkbook
For Each ws In srcwb.Worksheets
trgnm = ws.Name
'--> Change A1:B3 to the range to be copied to inside page
Set rng1 = srcwb.Sheets(trgnm).Range("A1:B3")
'--> Change C4:D5 to the range to be copied to outside page
Set rng2 = srcwb.Sheets(trgnm).Range("C4:D5")
Set trgwb = Workbooks.Open(fpath & trgnm & ".xls")
With trgwb
Set t1ws = .Sheets("Inside Page")
Set t2ws = .Sheets("Outside Page")
End With
'--> Change A1:B3 to the range where you want to paste
rng1.Copy t1ws.Range("A1:B3")
'--> Change C4:D5 to the range where you want to paste
rng2.Copy t2ws.Range("C4:D5")
trgwb.Close True
Next
Application.ScreenUpdating = True
End Sub

Excel macro for copying cell contents to a specific sheet in another workbook

What I need is a way to send the contents of some cells in "ThisWorkbook" (where the macro is) to a specific sheet in another workbook (the location of which will not change, unlike "ThisWorkbook")
for some reason, this below dosen't work:
Sub Transplant()
Dim thispath As String
Dim targetpath As String
'Set filepaths
thispath = ThisWorkbook.FullName
targetpath = ThisWorkbook.Path & "/subdir/Targetbook.xlsm"
Dim Srcwb As Workbook
Dim Trgwb As Workbook
'Set workbooks
Set Srcwb = Workbooks.Open(thispath)
Set Trgwb = Workbooks.Open(targetpath)
Srcwb.Worksheets("Sheet1").Range(Srcwb .Worksheets("Sheet1").Range("A1"), _
Srcwb.Worksheets("Sheet1").Range("A1").End(xlToRight)).Copy _
Destination:=Trgwb.Sheets("Sheet1").Cells(1, 1)
End Sub
Please help!
//Leo
This is pretty much the same as what you've got, although I didnt re-open the active workbook.
Can you describe the range you're trying to copy? You might find that UsedRange is easier.
Sub Transplant()
Dim DWB As Workbook
Dim S As Worksheet
Set S = ThisWorkbook.WorksheetS("Sheet1") ' forgot to rename Source to S
Set DWB = Application.Workbooks.Open(Thisworkbook.Path & "/subdir/Targetbook.xlsm")
Set D = DWB.Worksheets("Sheet1")
S.Range(S.Range("A1"), S.Range("A1").End(xlToRight)).Copy Destination:=D.Cells(1,1)
' S.UsedRange.Copy Destination:=D.Cells(1,1) - this might be easier
End Sub

Resources