1

I want to open an existing excel work book and add a sheet with given name.. All this has to be done from a different excel work book

I am able to open the existing work book from a blank work book by executing code. But when I tired to add a new sheet to the opened work book, my code is adding the new sheet in the execution work book (Blank work book)

Sub Valuesets()
Dim ws As Worksheets
Dim PTable1 As PivotTable
Dim sheet As Sheets
Set wb = ActiveWorkbook

Workbooks.Open("C:\Users\user\Desktop\workbook1.xlsb").Worksheets("Data").Activate
On Error Resume Next
Application.DisplayAlerts = False
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "Test-1"
Application.DisplayAlerts = True
Set CSheet = Worksheets("Data")
Set SSheet = Worksheets("Test-1")
End Sub
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73

1 Answers1

0

You should set your opened workbook into a variable OpenWb so you can access it to add a sheet in this specific workbook OpenWb.Sheets.Add. The same with the data sheet and the new sheet. Always avoid using .Activate, ActiveSheet and .Select like described in How to avoid using Select in Excel VBA.

Also make sure you don't use On Error Resume Next as you did. This line hides all error messages until End Sub or On Error Goto 0 but the errors still occur. You just hide their messages. If you don't see the errors you cannot fix them. If you don't tell the user that something went wrong, he will continue with that wrong status!

Always implement a proper error handling: VBA Error Handling – A Complete Guide.
Using On Error Resume Next (unless you know in which rare cases this can be used) is a very bad practice and will produce more issues instead of preventing issues.

Sub Valuesets()
    Dim OpenWb As Workbook
    Set OpenWb = Workbooks.Open("C:\Users\user\Desktop\workbook1.xlsb")

    Dim wsData As Worksheet
    Set wsData = OpenWb.Worksheets("Data")

    Application.DisplayAlerts = False

    Dim NewSheet As Worksheet
    Set NewSheet = OpenWb.Sheets.Add(Before:=wsData)
    NewSheet.Name = "Test-1"

    Application.DisplayAlerts = True
End Sub
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • One small doubt... How can I access the different work book with a selected work sheet in it for multiple times? – srawan kallakuri Sep 05 '19 at 10:06
  • @srawankallakuri sorry I don't get your question. Can you please be more precise? If your question is longer than you can explain in a comment use your original question. – Pᴇʜ Sep 05 '19 at 10:07
  • In the above code we are opening a work book from different work book.. That work book will be active untill we open another work book -2 Here What I have done was, I opend work book-1 and after that work book 2 did some actions in book-2 Now, I want to go back to book-1 and do some more actions over there Is it possible? I tried with This sheets-But code is focusing on the executing sheet Please help me – srawan kallakuri Sep 05 '19 at 10:18
  • @srawankallakuri That is a new question now. Please open up a completely new question for that (it is to broad to answer in comments). – Pᴇʜ Sep 05 '19 at 10:23
  • I am sorry...Posted the question Sir :) – srawan kallakuri Sep 05 '19 at 10:30
  • Can you please see this question https://stackoverflow.com/questions/57828256/comparison-of-one-cell-value-from-one-sheet-with-another-cell-values-of-a-range – srawan kallakuri Sep 06 '19 at 20:46