Need help with vba code

Hellcheat

New Member
Joined
Nov 10, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hi, I'm hoping you could give me some help. I have zero experience in vba coding but I've managed to kind of mish mash some code together.
After the latest code add it started giving issues. I hope someone on here would be able to help.

I apologise for the nightmares the code might give you.

VBA Code:
Private Sub CommandButton1_Click()

Dim FName           As String
Dim FPath           As String
Dim NewBook         As Workbook
Dim WSheet          As Worksheet
Dim WsNames         As Range, CR As Range

Application.ScreenUpdating = False

' Network drive
FPath = "Z:\Excel"
' Name and Year based on cell value
FName = "Daily Capacity" & Range("A13").Value


' Define the Workbook to copy
Set NewBook = Workbooks.Add(Template:="Z:\Excel\Daily Capacity Master.xlsx")
    If Dir(FPath & "\" & FName) <> "" Then
        MsgBox "File " & FPath & FName & " already exists"
        ' If error happens it moves on?
        On Error Resume Next
        On Error GoTo 0
    Else
' Define Worksheet to copy
Set WSheet = Sheets("Master Week")
Set WsNames = Sheets("data").Range("J2:J" & Rows.Count).SpecialCells(2)
    For Each CR In WsNames
        If Not Evaluate("ISREF('" & CR.Value & " '!J2)") Then
            WSheet.Copy After:=Sheets(Sheets.Count)
            Active.Sheet.Name = CR.Value
        End If
    Next CR
    
WSheet.Select
'Define Save As
 With NewBook
' .Title = FName
' add additional code here
 NewBook.SaveAs Filename:=FPath & "\" & FName & ".xlsx"
 NewBook.Close
 'If it doesn't close
 'NewBook.Close SaveChanges:=True
 End With
 
Application.ScreenUpdating = True

End Sub

The purpose of the code is create a new workbook named Daily Capacity YYYY, based on another workbook.
Copy the template named "Master Week" and rename it based on a list on the worksheet named "data", this list can change plus minus a few cells (could potentially hardcode)
Then save the new workbook and close it. I've attached a picture of what it sort should look like the worksheets.
Any help is very appreciated
 

Attachments

  • final result.png
    final result.png
    2.3 KB · Views: 17

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi there,
welcome to the forum. Because it's a bit hard to check your code without all your files, try the following to debug your code:
  • in the VBE (VBA-editor), switch on the "Direct" window and the "local variables" (both to be found in the View menu)
  • The Direct window is really helpful to see what is happening in your code: by adding a line to your code Debug.Print MyVariable , it will be printed there, so you can see where your code is and what is happening
  • The local variables are helpful to see what variables contain when your code is running. However, this only works if your is stopped (so not when you press play/F5).
  • Therefore: use F8 to run your code, basically going step by step through your code to see where it crashes.
  • An alternative: add break points (F9 or click in front of a line so a small circle appears and the line gets highlighted). If you run the code with F5, it'll stop there. That would be a good time to e.g. check the variables in the Local Variables window.
Those are the main debugging tools VBE can offer and it helps massively to understand where/why your code fails.

Hope it helps, if not: don't hesitate to answer this post,
Cheers,
Koen
 
Upvote 0

Forum statistics

Threads
1,214,921
Messages
6,122,280
Members
449,075
Latest member
staticfluids

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