VBA code to create sheet in new workbook and rename it

fari1

Active Member
Joined
May 29, 2011
Messages
362
Hi,
i'm seeking for a vba code for my following issue
.
the code does match this cell value from oldworkbook with all the sheetnames in the newworkbook, if it finds the sheetname that matches value, it copies a range F1 to H1 in its cell A1, else it create a new sheet and rename it with the cell value and paste the range.
any help on it is greatly appreciated

my code is

Code:
Option Explicit
Sub copyDataToClosedWorkbook()
    Dim wbTo   As Workbook
    Dim wbFrom As Workbook
    Set wbFrom = ActiveWorkbook
    Set wbTo = Workbooks.Open("C:\Project\Final.xlsm", _
                              False, True)
    With wbFrom
    wbFrom.Sheets("ratios").Range("k1:Aj6").Copy
    End With
    
    Application.ScreenUpdating = False
    With wbTo
    wbTo.Sheets("Sheet1").Name = wbFrom.Sheets("webquery").Range("A1").Value.Activate
    ActiveSheet.Range("A1").PasteSpecial Paste:=xlValues, Transpose:=True
    Worksheets.Add().Name = ("sheet1")
End With
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    wbTo.ChangeFileAccess Mode:=xlReadWrite
    wbTo.Close SaveChanges:=True
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Can see some mistakes in your code. Try:
Code:
Option Explicit
Sub copyDataToClosedWorkbook()

    Dim wbTo   As Workbook
    Dim wbFrom As Workbook

    Set wbFrom = ActiveWorkbook
    Set wbTo = Workbooks.Open("C:\Project\Final.xlsm", False, True)

    With wbFrom
      .Sheets("ratios").Range("K1:AJ6").Copy
    End With
    
    With Application
      .ScreenUpdating = False
      .DisplayAlerts = False
    End With

    With wbTo
      With .Sheets("Sheet1")
           .Range("A1").PasteSpecial Paste:=xlValues
           .Name = wbFrom.Sheets("webquery").Range("A1").Value.Activate
      End With
      Worksheets.Add().Name = ("Sheet1")
    End With

    wbTo.ChangeFileAccess Mode:=xlReadWrite
    wbTo.Close SaveChanges:=True

    With Application
      .ScreenUpdating = True
      .DisplayAlerts = True
    End With

End Sub
 
Upvote 0
hi jack thanks for the quicki reply
the code is giving me the runtime error424 object required
 
Upvote 0
Missed that. Change it to:
Code:
.Name = wbFrom.Sheets("webquery").Range("A1").Value
 
Upvote 0
the code is working like a charm, BUT
i want the code to work like this, to match the cell value with the sheet names in newworkbook, if the sheet exists paste the range else create new sheet and rename it with cell value and paste the range. hope i didn't confuse you
 
Upvote 0
See if this works:
Code:
Option Explicit
Sub copyDataToClosedWorkbook()

    Dim wbTo   As Workbook
    Dim wbFrom As Workbook
    Dim testSheet As WorkSheet

    Set wbFrom = ActiveWorkbook
    Set wbTo = Workbooks.Open("C:\Project\Final.xlsm", False, True)

    With wbFrom
      .Sheets("ratios").Range("K1:AJ6").Copy
    End With
    
    With Application
      .ScreenUpdating = False
      .DisplayAlerts = False
    End With

    With wbTo
      On Error Resume Next
      Set testSheet = Sheets(wbFrom.Sheets("webquery").Range("A1").Value)
      If testSheet Is Nothing Then
         Worksheets.Add().Name = ("Sheet1")         
         With .Sheets("Sheet1")
             .Range("A1").PasteSpecial Paste:=xlValues
             .Name = wbFrom.Sheets("webquery").Range("A1").Value
        End With
      Else
        .Sheets(wbFrom.Sheets("webquery").Range("A1")).Range("A1").PasteSpecial Paste:=xlValues
      End If
    End With

    wbTo.ChangeFileAccess Mode:=xlReadWrite
    wbTo.Close SaveChanges:=True

    With Application
      .ScreenUpdating = True
      .DisplayAlerts = True
    End With

End Sub
 
Upvote 0
the code is great i guess, but the only problem if a sheet already exists that matches cell value, it doesn't copy data into that sheet and also dun give me any error, i mean it completes the process without copying, else it works great
 
Upvote 0
Try:
Code:
Option Explicit
Sub copyDataToClosedWorkbook()

    Dim wbTo   As Workbook
    Dim wbFrom As Workbook
    Dim testSheet As WorkSheet

    Set wbFrom = ActiveWorkbook
    Set wbTo = Workbooks.Open("C:\Project\Final.xlsm", False, True)

    With wbFrom
      .Sheets("ratios").Range("K1:AJ6").Copy
    End With
    
    With Application
      .ScreenUpdating = False
      .DisplayAlerts = False
    End With

    With wbTo
      On Error Resume Next
      Set testSheet = Sheets(wbFrom.Sheets("webquery").Range("A1").Value)
      If testSheet Is Not Nothing Then
         .testSheet.Range("A1").PasteSpecial Paste:=xlValues
      Else
         Worksheets.Add().Name = ("Sheet1")         
         With .Sheets("Sheet1")
             .Range("A1").PasteSpecial Paste:=xlValues
             .Name = wbFrom.Sheets("webquery").Range("A1").Value
        End With
        On Error Goto 0
      Else
      End If

    wbTo.ChangeFileAccess Mode:=xlReadWrite
    wbTo.Close SaveChanges:=True

    With Application
      .ScreenUpdating = True
      .DisplayAlerts = True
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,903
Members
452,948
Latest member
Dupuhini

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