Being self taught I have not learned everything there is to know about Excel VBA. Hell lets be honest, I know some basics, but not as much as I probably should, so this question although probably a generic basic question, probably should be known by someone who knows VBA, but in the case of I learn what I need to move forward… so I need an explanation.
When do you need to define a Sub or Function? Why do you need to define a Sub or Function? What is the purpose of defining a Sub or Function?
I only ask, because of this:
Option Explicit
Sub OpenOrderReportExport()
Dim wsJL As Worksheet 'Jobs List
Dim wsPOT As Worksheet 'PO Tracking
Dim wsTNO As Worksheet 'Tel-Nexx OOR
Dim wbBK2 As Workbook 'New Workbook
Dim wsWS1 As Worksheet 'Sheet1
Dim wsWS2 As Worksheet 'Sheet2
Dim wsWS3 As Worksheet 'Sheet3
Set wsJL = Sheets("Jobs List") 'Jobs List
Set wsPOT = Sheets("PO Tracking") 'PO Tracking
Set wsTNO = Sheets("Tel-Nexx OOR") 'Tel-Nexx OOR
Set wbBK2 = Workbook("Book2") 'New Workbook
Set wsWS1 = Sheets("Sheet1") 'Sheet1
Set wsWS2 = Sheets("Sheet2") 'Sheet2
Set wsWS3 = Sheets("Sheet3") 'Sheet3
Application.ScreenUpdating = False ' Prevents screen refreshing.
CurrentFile = ThisWorkbook.FullName
NewFileType = "Excel Files 2007 (*.xlsx)"
NewFile = Application.GetSaveAsFilename(InitialFileName:="Open Order Log - " & Format(Date, "dd-mm-yyyy") & ".txt", fileFilter:=NewFileType)
End Sub
Why does this sub need to be defined? I am putting it as a standard module, and working on setting it up so that once a button is pushed I will be able to move a few pages over to a new workbook, delete the blank ones, and go from there with a save function that you tell it where to save.
I have a long way to go, but I wanted to test out the save function, and go from there, because to me that is going to be the hardest thing.
So without solving my entire conundrum, I would like to know why something like this needs to be defined? And how it needs to be defined… if someone could give an answer it be greatly appreciated.
You are misunderstanding the problem.
The following line of code is incorrect. It does not create a new workbook like you think it does. The function
Workbookdoes not exist – when you try to run/compile the VBA editor complains that the function does not exist.Change it to
and it will create a new workbook and save a reference in
wbBK2. Alternatively you could reference an open workbook by adding an “s” toWorkbookif you are not trying to make a new one:You also will need to dimension these variables as strings eventually:
Another note, too – when you use
You probably want to change this to reference the sheets in the other workbook
as it will be quite helpful to maintain the right references to these sheets, even if you add additional workbooks.