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
 
compile error
invalid use of object

and it highlights nothing in this code line
Code:
 If testSheet Is Not Nothing Then
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Typo (again!). 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
Change that line to:
Code:
 If Not (testSheet Is Nothing) Then
 
Upvote 0
now no error, but still its not copying data in already present sheet,for new sheet its working like great
 
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
What do you want it to do if the sheet already exists, paste or not paste?
 
Upvote 0
code has to paste data in any case, if the sheet is present or not, the difference is, if the sheet is present(matches the cell value) then paste in it, else creates a new sheet and pastes into it and rename it
 
Upvote 0
It should be doing it. Made a tiny adjustment, 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 Not (testSheet Is Nothing) Then
         .testSheet.Range("A1").PasteSpecial Paste:=xlValues
      Else
         Worksheets.Add().Name = "NewSheet"
         With .Sheets("NewSheet")
             .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,617
Messages
6,179,914
Members
452,949
Latest member
beartooth91

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