Copy range from one workbook to another

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,343
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this code

VBA Code:
Sub AddYP()
Application.DisplayAlerts = False
Dim newyp As String, rng As Range, wb1 As Workbook, wb2 As Workbook, ListName As String
    Set wb1 = ThisWorkbook
        newyp = Tracker.Cells(6, 4)
    Workbooks.Open Filename:=ThisWorkbook.Path & "\Young People\List.xlsm"
    Set wb2 = Workbooks("List")
    With wb2.Sheets(1).Range("A:A") 'searches all of column A
        Set rng = .Find(What:=newyp, After:=.Cells(.Cells.Count), LookIn:=xlValues, _
                        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                        MatchCase:=False)
        If Not rng Is Nothing Then
        MsgBox "This name is already in the list."
        Exit Sub
        Else
            wb2.Sheets(1).Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = newyp
        End If
        End With
        Call CreateWB(newyp, wb1)
        wb2.Sheets(1).Range("A2:A" & wb2.Sheets(1).Range("A2").End(xlUp).Row).Copy wb1.Sheets("YPNames").Range("A2")
        
        wb1.Names.Add Name:="tblYPNames", RefersTo:=wb1.Sheets("YPNames").Range("A2").End(xlUp)
        Tracker.cboYP.ListFillRange = "tblYPNames"
        Tracker.cboYP.ListFillRange = "tblYPNames"
Application.DisplayAlerts = True
End Sub

Is this line the correct code to copy the list on sheet 1 of list.xlsm to the sheet called YPNames in the workbook that contains the sub?
VBA Code:
wb2.Sheets(1).Range("A2:A" & wb2.Sheets(1).Range("A2").End(xlUp).Row).Copy wb1.Sheets("YPNames").Range("A2")
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,898
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
doesn't the code in AddYP already do that ??
VBA Code:
wb1.Names.Add Name:="tblYPNames", RefersTo:=wb1.Sheets("YPNames").Range("A2").End(xlUp)
        Tracker.cboYP.ListFillRange = "tblYPNames"
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,898
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
OR this
VBA Code:
wb1.Names.Add Name:="tblYPNames", RefersTo:=wb1.Sheets("YPNames").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
        Tracker.cboYP.ListFillRange = "tblYPNames"
 

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,343
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
How can I set the named range as the current list in YPNames, starting at A2?
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,898
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
Doesn't post #12 do that ??
 

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,343
Office Version
  1. 365
  2. 2016
Platform
  1. Windows

ADVERTISEMENT

I have the named range pointing to A2, add a name and then the named range is pointing to A1:A2, instead of A2:A3.
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,898
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
Is wb1 the activeworkbook ??
If so
VBA Code:
Sheets("YPNames").Names.Add Name:="tblYPNames", RefersTo:=Sheets("YPNames").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
        Tracker.cboYP.ListFillRange = "tblYPNames"
 

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,343
Office Version
  1. 365
  2. 2016
Platform
  1. Windows

ADVERTISEMENT

I changed your code to xldown instead of xlup and the whole list was in the named range but the named range now refers to this

Excel Formula:
=YPNames!$A$2:$A$1048576
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,898
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
That's because you used XLDOWN
 

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,343
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
That seems to be working pretty well now, thanks Michael.

Is there a way Michael to read the file names that are in the folder, Young People, without the extension into the combo box cboYPNames instead of having a separate list file for them?

So the combo will always show all of the files that are in the folder and will be up to date?
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,898
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
Have a read through this thread...
but by continuously changing the way you do things will simply cause you lots of headaches....you'll simply get confused.
AND
I don't think you will extract straight to the combo box
 

Forum statistics

Threads
1,141,315
Messages
5,705,699
Members
421,406
Latest member
kluna90

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top