+ Reply to Thread
Results 1 to 35 of 35

Run Time Errors - Code not working

  1. #1
    Registered User
    Join Date
    04-08-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    38

    Run Time Errors - Code not working

    Hi

    Can someone advise me on how to fix the code below

    I have hundreds of different rows to export into different individual tabs. When I run this code I get
    Run-time Error ‘1004’
    Cannot rename a sheet to the same name as another sheet, a referenced object library or a workbook referenced by visual basic

    Additionally, the code is always generating tabs inside the two workbooks – even though this there are 2 options to not to this – it always happens!!! It is also not picking up the corresponding cells.

    Any help is most grateful

    -------------

    Sub FillOutTemplate()

    Dim LastRw As Long, Rw As Long, Cnt As Long
    Dim dSht As Worksheet, tSht As Worksheet
    Dim MakeBooks As Boolean, SavePath As String

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Set dSht = Sheets("Sales")
    Set tSht = Sheets("Template")

    'Option to create separate workbooks
    MakeBooks = MsgBox("Create separate workbooks?" & vbLf & vbLf & _
    "YES = Template will be copied to separate workbooks" & vbLf & _
    "NO = Template will be copied to sheets within this workbook", _
    vbYesNo + vbQuestion) = vbYes


    If MakeBooks Then
    MsgBox "Please select a destination for the new workbooks"
    Do
    With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    .Show
    If .SelectedItems.Count > 0 Then
    SavePath = .SelectedItems(1) & "\"
    Exit Do
    Else
    If MsgBox("Do you wish to abort?", _
    vbYesNo + vbQuestion) = vbYes Then Exit Sub
    End If
    End With
    Loop
    End If

    LastRw = dSht.Range("A" & Rows.Count).End(xlUp).Row

    For Rw = 3 To LastRw
    tSht.Copy After:=Worksheets(Worksheets.Count)
    With ActiveSheet

    .Name = dSht.Range("F3" & Rw)
    .Range("A3").Value = dSht.Range("D5" & Rw).Value
    .Range("B3").Value = dSht.Range("D7 & Rw").Value
    .Range("C3").Value = dSht.Range("D9 & Rw").Value
    .Range("D3").Value = dSht.Range("D11 & Rw").Value
    .Range("E3").Value = dSht.Range("D13 & Rw").Value
    .Range("F3").Value = dSht.Range("D15 & Rw").Value
    .Range("G3").Value = dSht.Range("D19 & Rw").Value
    .Range("H3").Value = dSht.Range("C19 & Rw").Value
    .Range("I3").Value = dSht.Range("C20 & Rw").Value
    .Range("J3").Value = dSht.Range("C21 & Rw").Value
    .Range("K3").Value = dSht.Range("C22 & Rw").Value
    .Range("L3").Value = dSht.Range("C23 & Rw").Value
    .Range("M3").Value = dSht.Range("D18 & Rw").Value
    .Range("N3").Value = dSht.Range("D19 & Rw").Value
    .Range("O3").Value = dSht.Range("D20 & Rw").Value
    .Range("P3").Value = dSht.Range("D21 & Rw").Value
    .Range("Q3").Value = dSht.Range("D22 & Rw").Value
    .Range("R3").Value = dSht.Range("D23 & Rw").Value
    .Range("S3").Value = dSht.Range("D25 & Rw").Value
    .Range("T3").Value = dSht.Range("D27 & Rw").Value
    .Range("U3").Value = dSht.Range("D29 & Rw").Value
    .Range("V3").Value = dSht.Range("D31 & Rw").Value
    .Range("W3").Value = dSht.Range("D33 & Rw").Value
    .Range("X3").Value = dSht.Range("D35 & Rw").Value
    .Range("Y3").Value = dSht.Range("D37 & Rw ").Value
    .Range("Z3").Value = dSht.Range("D39").Value
    .Range("AA3").Value = dSht.Range("D41").Value
    .Range("AB3").Value = dSht.Range("D43").Value
    .Range("AC3").Value = dSht.Range("D45").Value
    .Range("AD3").Value = dSht.Range("D47").Value
    .Range("AE3").Value = dSht.Range("D49").Value
    .Range("AF3").Value = dSht.Range("D51").Value
    .Range("AG3").Value = dSht.Range("D53").Value
    .Range("AH3").Value = dSht.Range("D55").Value
    .Range("AI3").Value = dSht.Range("D58").Value
    .Range("AJ3").Value = dSht.Range("D60").Value
    .Range("AK3").Value = dSht.Range("D62").Value
    End With


    If MakeBooks Then
    ActiveSheet.Move
    ActiveWorkbook.SaveAs SavePath & Range("F3").Value, xlNormal
    ActiveWorkbook.Close False
    End If
    Cnt = Cnt + 1
    Next Rw

    dSht.Activate
    If MakeBooks Then
    MsgBox "Workbooks created: " & Cnt
    Else
    MsgBox "Worksheets created: " & Cnt
    End If

    Application.ScreenUpdating = True
    End Sub

  2. #2
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Run Time Errors - Code not working

    Just a thought...I really does make the code easier to read....

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    Ernest

    Please consider adding a * if I helped

    Nothing drives me crazy - I'm always close enough to walk....

  3. #3
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Run Time Errors - Code not working

    this is the code that is probably causing the problem...basically it means you are trying to name a worksheet (tab) that already has that name...so you have to either delete the worksheet, change the name of the old worksheet or change what you are going to call the new worksheet....

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    04-08-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Run Time Errors - Code not working

    Sorry - see below

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    04-08-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Run Time Errors - Code not working

    The error is highlighting

    Please Login or Register  to view this content.

  6. #6
    Forum Contributor pareshj's Avatar
    Join Date
    05-20-2014
    Location
    MUMBAI
    MS-Off Ver
    2007 & 2010
    Posts
    447

    Re: Run Time Errors - Code not working

    Hi sophia,

    Please check the code posted in your post #5,

    Please Login or Register  to view this content.
    You have already specified range as "D5" then why are using & Rw with it.

    Pls check carefully whether should be like this :

    Please Login or Register  to view this content.
    or

    Please Login or Register  to view this content.

    Please correct it and then check.

    Regards,
    Paresh J
    Click on "* Add Reputation" as a way to say thanks

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Run Time Errors - Code not working

    As Judge indicated, the error is most likely to be on this line

    Please Login or Register  to view this content.
    You have already a sheet name called dSht.Range("F3" & Rw). You can not use the same sheet name again.
    You can try
    Please Login or Register  to view this content.
    But the error will not go away until you use unique sheet names
    Last edited by AB33; 09-19-2014 at 07:00 AM.

  8. #8
    Registered User
    Join Date
    04-08-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Run Time Errors - Code not working

    Hi

    Basically what I was trying to do with
    Please Login or Register  to view this content.
    Was to createthe output sheet name as the cell value on the master sheet. Is this wrong?

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Run Time Errors - Code not working

    No, as long as you do not have a sheet name with the cell value of dSht.Range("F3" & Rw). Even if you already have, you should get an error on that line, which says along the line "You can not create a sheet".

    Does the new sheet created have a name which is dSht.Range("F3" & Rw)?
    What is dSht.Range("F3" & Rw)?
    If all these assumptions are correct, you should not get an error on this line
    Please Login or Register  to view this content.
    .

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Run Time Errors - Code not working

    The easiest way to pin down the error is: To step over the code using F8 and see where the code errors.

  11. #11
    Registered User
    Join Date
    04-08-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Run Time Errors - Code not working

    Hi The code errors

    Please Login or Register  to view this content.

    What is
    Please Login or Register  to view this content.
    this is what I want to call my new workbook. I'm really stumped here!!!

  12. #12
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Run Time Errors - Code not working

    Sorry!
    I did not spot this line was wrong

    Please Login or Register  to view this content.
    It should be

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    04-08-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Run Time Errors - Code not working

    The code is now error at
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Where am I going wrong?!

  14. #14
    Forum Contributor pareshj's Avatar
    Join Date
    05-20-2014
    Location
    MUMBAI
    MS-Off Ver
    2007 & 2010
    Posts
    447

    Re: Run Time Errors - Code not working

    Hi,

    Try this:

    ActiveSheet..Name = dSht.Range("F3").value

    And remove With Activesheet from above line.


    Regads,
    Paresh J

  15. #15
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Run Time Errors - Code not working

    Sophia,
    Did you also try
    .Name = dSht.Range("F3" & Rw)
    ?
    If so, did you get an error?

    It would be easier to see the error if you could attach the sample please.

  16. #16
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Run Time Errors - Code not working

    When you say the code errors, what does the error say?

  17. #17
    Registered User
    Join Date
    04-08-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Run Time Errors - Code not working

    Hi - that didnt work

    Quote Originally Posted by pareshj View Post
    Hi,

    Try this:

    ActiveSheet..Name = dSht.Range("F3").value

    And remove With Activesheet from above line.


    Regads,
    Paresh J

  18. #18
    Registered User
    Join Date
    04-08-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Run Time Errors - Code not working

    Hi it says

    Run-time error '1004':

    Application- defined or object -defined error

  19. #19
    Forum Contributor pareshj's Avatar
    Join Date
    05-20-2014
    Location
    MUMBAI
    MS-Off Ver
    2007 & 2010
    Posts
    447

    Re: Run Time Errors - Code not working

    Sophia,

    Please remove the double dot.

    ActiveSheet..Name replace with ActiveSheet.Name

  20. #20
    Registered User
    Join Date
    04-08-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Run Time Errors - Code not working

    Hi I removed the double dot

    Quote Originally Posted by pareshj View Post
    Sophia,

    Please remove the double dot.

    ActiveSheet..Name replace with ActiveSheet.Name

  21. #21
    Forum Contributor pareshj's Avatar
    Join Date
    05-20-2014
    Location
    MUMBAI
    MS-Off Ver
    2007 & 2010
    Posts
    447

    Re: Run Time Errors - Code not working

    Hi,

    Is it working?

  22. #22
    Registered User
    Join Date
    04-08-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Run Time Errors - Code not working

    No

    Should I replace End With with something?

  23. #23
    Forum Contributor pareshj's Avatar
    Join Date
    05-20-2014
    Location
    MUMBAI
    MS-Off Ver
    2007 & 2010
    Posts
    447

    Re: Run Time Errors - Code not working

    Hi,

    Remove End With too..

  24. #24
    Registered User
    Join Date
    04-08-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Run Time Errors - Code not working

    Quote Originally Posted by pareshj View Post
    Hi,

    Remove End With too..
    Still the error is highlighting for

    ActiveSheet.Range("A3").Value = dSht.Range("D5").Value

  25. #25
    Forum Contributor pareshj's Avatar
    Join Date
    05-20-2014
    Location
    MUMBAI
    MS-Off Ver
    2007 & 2010
    Posts
    447

    Re: Run Time Errors - Code not working

    Sophia,

    Can you attach your excel file?

  26. #26
    Registered User
    Join Date
    04-08-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Run Time Errors - Code not working

    Input_ Output.xlsx

    HI

    Its very classified - but I have made a mock one which follwos the same lines
    Attached Files Attached Files

  27. #27
    Forum Contributor pareshj's Avatar
    Join Date
    05-20-2014
    Location
    MUMBAI
    MS-Off Ver
    2007 & 2010
    Posts
    447

    Re: Run Time Errors - Code not working

    Hi,

    Your attached file does not contain the code.

    Regards,
    Paresh J

  28. #28
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Run Time Errors - Code not working

    I have now tested the code and see why the code errors.
    This line is wrong

    Please Login or Register  to view this content.
    Should be

    Please Login or Register  to view this content.
    Saying row 3 column F.
    You will get another error.
    If the column F and what ever the row number is blank, that if F3, F4 cells are blanks.
    You will also get another error called " You can not use the same sheet name again if you try to use the same cell value to create a sheet name.

  29. #29
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Run Time Errors - Code not working

    You might not get an error on these lines, but these lines will not be copied. You need to use either the A1 range D7 or column D and row no Rw
    that

    Please Login or Register  to view this content.
    Last edited by AB33; 09-24-2014 at 06:42 AM.

  30. #30
    Registered User
    Join Date
    04-08-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Run Time Errors - Code not working

    HI

    Many thanks

    However - its now saying "Invalid use of property as the error".
    This is my new code


    Please Login or Register  to view this content.

  31. #31
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Run Time Errors - Code not working

    Deleted-Wrong
    Last edited by AB33; 09-24-2014 at 06:38 AM. Reason: Edited

  32. #32
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Run Time Errors - Code not working

    Sorry!
    It was my mistake!
    I think you copied the line from post#29
    Meant to be
    Please Login or Register  to view this content.

  33. #33
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Run Time Errors - Code not working

    This site stinks . It has now over 24 hours and the edit button is not working.

    I have removed most of the lines as they are referring to the same range. Please try 1 or 2 lines at a time and will add more lines
    Please Login or Register  to view this content.
    Last edited by AB33; 09-24-2014 at 06:41 AM. Reason: Edited

  34. #34
    Registered User
    Join Date
    04-08-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Run Time Errors - Code not working

    Thanks AB33 - the error is still coming up for

    Please Login or Register  to view this content.
    Even with the above.

    Does anyone have a simpler code?.

  35. #35
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Run Time Errors - Code not working

    Please try the attached.
    I did make the sample myself.
    A simple code? It is your own code.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Code not working the 2nd time in For loop
    By rbs123 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-17-2013, 06:19 AM
  2. [SOLVED] Copy & Paste Code not working second time around?
    By matrixpom in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-10-2013, 05:08 PM
  3. [SOLVED] Disable Run-Time Errors "Application.DisplayAlerts = False" Not Working
    By Punx in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-12-2012, 03:03 PM
  4. First time coding in VB, no errors but my very simple script isn't working. Help?
    By metajellyfish in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-17-2012, 05:01 PM
  5. VBA Code only working 60% of time???
    By sweedey in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-07-2011, 09:08 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1