Add New Sheets with Name from a Range

Jsingh96

New Member
Joined
Jun 11, 2020
Messages
9
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi,

Looking for some help on the below code. Instead of adding just a new sheet Set (ws = y.Sheets.Add) (Sheet1, Sheet2...) I need to create the new sheet with a name from a specific range. The range is the adjacent cells/column from "E2:E33"





Sub CopyData()

Application.ScreenUpdating = False


Application.DisplayAlerts = False
Dim ws As Worksheet
Dim x As Workbook
Dim y As Workbook
Dim rngURL As Range
Dim cll As Range

Set rngURL = Worksheets("data_pull").Range("E2:E33")
On Error GoTo errHandler
For Each cll In rngURL

DoEvents

Set x = Workbooks.Open(cll.Value)
Set y = ThisWorkbook

Set ws = y.Sheets.Add
x.Sheets("data_paste").Cells.Copy
ws.Cells.PasteSpecial
ws.Cells.Copy
ws.Cells.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=True, Transpose:=False

Application.CutCopyMode = False

x.Close

Next
errHandler:
If Err.Number <> 0 Then MsgBox "There was an error." & vbNewLine & "Error " & Err.Number & vbTab & Err.Description, vbOKOnly, "Error"
Application.DisplayAlerts = True
Set x = Nothing
Set y = Nothing
Set ws = Nothing
Application.ScreenUpdating = True

End Sub





Many Thanks in Advance
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I rewrote your macro to avoid the error handler, so that in case of error it can continue with the other sheets. Check if it is useful for you.


VBA Code:
Sub CopyData()
  Dim wb1 As Workbook, wb2 As Workbook
  Dim cll As Range
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  Set wb1 = ThisWorkbook
  
  For Each cll In wb1.Sheets("data_pull").Range("E2:E33")
    If cell <> "" And cll.Offset(, -1) <> "" Then
      If Dir(cll.Value) <> "" Then
        Set wb2 = Workbooks.Open(cll.Value)
        If Evaluate("ISREF('[" & wb2.Name & "]data_paste'!A1)") Then
          On Error Resume Next
            wb1.Sheets(cll.Offset(, -1).Text).Delete
          On Error GoTo 0
          wb2.Sheets("data_paste").Copy after:=wb1.Sheets(wb1.Sheets.Count)
          With wb1.Sheets(wb1.Sheets.Count)
            .Name = cll.Offset(, -1).Text
            .Cells.Copy
            .Cells.PasteSpecial Paste:=xlPasteValues
          End With
        End If
        wb2.Close False
      End If
    End If
  Next
  
  Application.CutCopyMode = False
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
I rewrote your macro to avoid the error handler, so that in case of error it can continue with the other sheets. Check if it is useful for you.


VBA Code:
Sub CopyData()
  Dim wb1 As Workbook, wb2 As Workbook
  Dim cll As Range
 
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  Set wb1 = ThisWorkbook
 
  For Each cll In wb1.Sheets("data_pull").Range("E2:E33")
    If cell <> "" And cll.Offset(, -1) <> "" Then
      If Dir(cll.Value) <> "" Then
        Set wb2 = Workbooks.Open(cll.Value)
        If Evaluate("ISREF('[" & wb2.Name & "]data_paste'!A1)") Then
          On Error Resume Next
            wb1.Sheets(cll.Offset(, -1).Text).Delete
          On Error GoTo 0
          wb2.Sheets("data_paste").Copy after:=wb1.Sheets(wb1.Sheets.Count)
          With wb1.Sheets(wb1.Sheets.Count)
            .Name = cll.Offset(, -1).Text
            .Cells.Copy
            .Cells.PasteSpecial Paste:=xlPasteValues
          End With
        End If
        wb2.Close False
      End If
    End If
  Next
 
  Application.CutCopyMode = False
  Application.ScreenUpdating = True
End Sub

Hi thanks a lot for this tip - However, the code is not working now with the above change
 
Upvote 0

Forum statistics

Threads
1,224,746
Messages
6,180,704
Members
452,994
Latest member
Janick

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
Back
Top