Professional Documents
Culture Documents
Real Hardware Engineers Code - Supplementary Tools
Real Hardware Engineers Code - Supplementary Tools
○ Problems mainly occur on Macros when the file structure changes (i.e., columns
are added, file type changes, names change, etc.).
○ Understanding Macro rules can help you set up an Excel sheet to write Macros
at a later date.
Learning to Navigate via VBA
● Excel is a large matrix (x columns, y rows).
● Learning to move in an Excel sheet is the first and most powerful tool to becoming
good at Macros.
● Why? An Excel sheet is data.
● Learning to move from one location to the next associated location helps you more
quickly associate your data or more quickly decide what you want to do next.
○ Note: If you can write instructions for someone to do your Excel manipulation,
you can write a Macro to do it!
Learning to Navigate via VBA
● Useful commands to help you navigate within your file.
Best Practice: use sheet names that NEVER have spaces. If you would like to use
sheet names with multiple words, separate them via an underscore. Ex:
“MONTHLY_BUDGET”
***In general, most applications will be hindered with the use of spaces; avoid them.
1) Range(“A1”).select This command can be used to select a specific cell (Ex. 1) or specific range of cells
2) Range (“A1: A10”).select (Ex. 2). Ex. 3 becomes powerful when you would like your range to be selected to
3) Range(“A” & j ).select become a variable and use the “Range” function within a loop (more on this later).
1) ActiveCell.Offset(1, 0).select This command can be used to navigate off your activecell. Ex. 1 moves you down
2) ActiveCell.Offset(0, 1).select one row. Ex. 2 moves you right one row. Ex. 3 allows you to use variables to make a
3) ActiveCell.Offset(y, x).select decision on where to navigate (you must clarify these values for the command to
work). Positive is down for “y” direction and to the right for “x” direction.
1) Activecell.address This command returns the full address of the current position (Format: “$A$5”).
2) Activecell.row This command returns the row the cell is currently located in (Format: #).
3) Activecell.column This command returns the column the row is currently in (Format: #) .
For i = 1 To 10 This is a loop command. The commands below the “for” and above the “next i” will
‘ code to execute goes here repeat till i=10.
Next i
Learning to Navigate via VBA
● Check your understanding.
● Follow the script below. What will the code return for when i = 4?
Learning to Navigate via VBA
● Answer.
● Follow the script below. What will the code return for when i = 4?
Teaching Yourself New Commands
● There are many excel VBA commands that you may not know how to correctly
“write” as code, but learning what they are is easy to figure out.
● Doing this and using your engineering prowess for understanding something you’ve
never seen before will allow you to figure out which items you can change and
manipulate.
● HINT: Break down what you want to do in a series of basic steps.
Recording a Macro
● This can be a very powerful tool to teach yourself VBA commands.
● This can also be useful if you are doing generic Excel file manipulation to an Excel file
that is always in the same format.
Recording a Macro
● Prompt: You have an Excel file with numerous
columns that have different audiences.
Depending on to whom you are sending this
Excel report, you want to (hide and/or delete
columns). You do this daily.
● After you press “OK,” you will be “live,” and every action you do within your Excel file will be
recorded. Know the steps you want to perform.
Recording a Macro
● To view your recorded Macro, right-click on the tab name (at bottom) and click “View
Code.” A new window will launch (short cut: alt-F11).
● In the new window, click on “Module” on the left-hand side.
● “Module#” stores your written or recorded code.
Recording a Macro
● All codes start with “Sub,” followed by the name of your Macro, and end with “End
Sub.”
● To the right is the code from your manual Excel
manipulation.
● What is not shown from our procedure?
● Why would this happen?
○ Click my “source_data” tab
○ Press ctrl-shift-a (selects all cells in the tab)
○ Copy the selected cells (ctrl-c)
○ Click “customer1_report_out” tab
○ Select Cell “A1” and paste (ctrl-v)
○ Hold ctrl and select all blue columns
○ Right click and press “delete”
Recording a Macro
● Click my “source_data” tab
● Press ctrl-shift-a (selects all cells in
the tab)
● Copy the selected cells (ctrl-c)
● Click “customer1_report_out” tab
● Select Cell “A1” and paste (ctrl-v)
● Hold ctrl and select all blue columns.
● Right click and press “delete”
Why did this happen? When the macro These steps were not in our
began recording, the user was already on procedure. The first line was
the correct tab! recorded because when the user
“right-clicked” to delete, he clicked
What if the user was on the wrong tab in the Excel window that happened
and ran this? The procedure would work, to be cell “N1” (this line is not
but the data that was copy-pasted would important).
be wrong.
Fix: Add sheets (“Source_Data”).select “ to The second line was added by Excel.
the beginning of the code. Let Excel do its own thing when you
see a command you don’t fully
understand.
Learning from a Recorded Macro
● How do I take what I’ve learned via :
“Customer1_Report_out()” and perform
this similar task for “Customer2”
Application.ScreenUpdating = False
As your code becomes more involved, add this line to the beginning
of your code. This will prevent your screen from flickering.
Writing a Macro to Work on Two Files
● Easy task: Two files; same structure; one master file with 2 columns that are
separately owned.
● One column is “CompanyA” input; the other column is “Supplier” input.
● When the file comes from the supplier, column “E” must be copied and pasted into
“CompanyA” from the updated supplier file.
● Develop Procedure: Remember, think very basic—a to b, b to c, etc.
1. Open “Myfile1”
2. Open “Other_file2”
3. Click right tab in “Other_file2”
4. Copy column “E” in workbook “Other_file2”
5. Select Workbook “Myfile1”
6. Select right tab in “Myfile1”
7. Select Column “E” and paste
8. Save “Myfile1”
9. Close “Other_File2”
10. Save “Myfile1”
● Time to record!
Writing a Macro to Work on Two Files
● Check results.
● Any suggestions?
Writing a Macro to Work on Two Files
● “Sheet1” selection not specified in the paste command.
● Add line: Application.ScreenUpdating = False
● Make this Macro a button and place it on a control panel in your workbook! (People
like buttons!)
● When working, always think top down: what “workbook”; what “sheet”; what “cell”
(row/column).
Creating a “button” for a Macro
● Add “button” icon to your toolbar
● Right click “quick access toolbar”
■ Select “customize quick
access toolbar”
■ From pop up window>
left column > top drop
down > Select “All
Commands”
■ Scroll down to “button
(From control)”
■ Click “Add” from center
■
■ “Button” will now show
up on your toolbar
Creating a “button” for a Macro
● Add button to “control panel” sheet.
● Assign it a Macro.
Creating a “button” for a Macro
Click your Macro; click OK
SCRIPTS:
SAVE AS ‘STEP’
&
EXPORT PDF
Table 1: Save as “STEP” Code
' Save Part as STEP with rev in folder .swp ---------------------------12/09/14
'
'Description: Macro to save part as STEP file in specified location with rev information in the
step file name.
'
'Precondition: Any active part with some geometry
'
'Postcondition: Part is saved as "STEP" file.=
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
'Custom Property
sRev = swModel.CustomInfo("Revision") 'Change Custom Property name here
swModel.SaveAs (FileLocation & sFileName & "_Rev_" & sRev & ".step")
End Sub
'------------------
Table 2: Export PDF Code
' Precondition: Have an open drawing document.
'
' Postcondition: Exports all Drawing sheets in a single PDF. Exports all KS in
' a single PDF. Writes a log showing the processed documents. Renames and
' moves duplicates to a backup directory.
'
'*******************************************************************************
Option Explicit
Global Const swDocDRAWING = 3 '
Global Const version = 2.55
'**************************************************************************************************
' Function: Main
'
' Description: First process to run in the macro. Initiates all variables. Ensures a drawing doc
' is currently open and active.
'
'**************************************************************************************************
Sub main()
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set swDraw = swDoc
Table 2: Export PDF Code
bRet = False
'**************************************************************************************************
' Function: ProcessDoc
'
' Description: Get all sheet names and runs the export process.
'
'**************************************************************************************************
Table 2: Export PDF Code
Sub ProcessDoc(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2)
'***********************************************************************
' Function: ExportSheets
'
' Description: Exports all Kit Sheets and Drawings as PDFs to the
' appropriate sub-directories.
'
'***********************************************************************
Table 2: Export PDF Code
Sub ExportSheets(swApp As SldWorks.SldWorks, swDoc As SldWorks.ModelDoc2)
Dim strFile As String
Dim DrawingSheets() As String
Dim lErrors As Long
Dim lWarnings As Long
Dim Count As Long
Dim boolstatus As Boolean
Dim ExpPath As String
Dim Check As Boolean
Dim ModString As String
Dim FileLocation As String
Dim sRev As String
Filename = swDoc.GetPathName
FileLocation = "C:\Users\dcagle\Desktop\PDF_RELEASE"
FileLocation = FileLocation & "\"
Table 2: Export PDF Code
Set swModelDocExt = swDoc.Extension
Set swPDFData = swApp.GetExportFileData(swExportPdfData)
'Custom Property
sRev = swDoc.CustomInfo("Revision") 'Change Custom Property name here
Check = False
Count = 1
If Filename <> "" Then
ResFileName = Left(Filename, Len(Filename) - 7)
Else
MsgBox "Please save the drawing before running this macro.", vbCritical
End If
ExpPath = FileLocation & ResFileName & "_REV_" & sRev & ".PDF"