Skip to content

Instantly share code, notes, and snippets.

@HerbFargus
HerbFargus / Concatenate.txt
Last active March 8, 2017 22:09
Arcmap Field Calculator (Python): Concatenate values from fields that are not NULL
#Pre-Logic Script Code:
def concat_fields(field1, field2, field3, field4):
if field1.strip() == "" or field2.strip() == "" or field3.strip() == "" or field4.strip() == "":
return ""
else:
return field1 + "/" + field2 + "/" + field3 + " " + field4
#FIELD=
concat_fields( !Field1!, !Field2!, !Field3!, !Field4!)
@HerbFargus
HerbFargus / lstrip.txt
Last active March 8, 2017 22:08
Arcmap Field Calculator (Python): Strip preceding Zeroes
#Strip preceding Zeroes
!FIELD!.lstrip("0")
@HerbFargus
HerbFargus / replace.txt
Created March 8, 2017 21:59
Arcmap Field Calculator (Python): Find and Replace
#Find and Replace Values in a Field
!FIELD!.replace("OriginalText", "NewText")
@HerbFargus
HerbFargus / domain_convert.txt
Last active July 29, 2018 10:39
ArcGIS Field Calculator (Python): Convert Domains from coded values to descriptions
#Pre-Logic Script Code:
def checkValue(c, d):
if c == 1:
return "A"
if c == 2:
return "B"
if c == 3:
return "C"
if c == 4:
return "D"
@HerbFargus
HerbFargus / IF_NULL.txt
Created March 13, 2017 15:09
Arcmap Field Calculator (Python): Conditional If Then statement if a field is NULL
#Pre-Logic Script Code:
def calculate(f1, f2):
if f2 == None:
return f1
else:
return f1 + f2
#Field =
calculate( !FIELD1!, !FIELD2! )
@HerbFargus
HerbFargus / addzero.txt
Created March 14, 2017 22:23
Arcmap Field Calculator (Python): Add leading zeroes to single character values (1-9)
Say I have a list of sections, out of the 36 numbers I want 1-9 to have the format 01,02, etc.
They are currently ints but my final field will be a text value so I need to convert the int to a text value to add a preceding 0.
Add a new field
use field calculator to fill the column based on the int, this will now be a text value.
Then run the following script in the field calculator:
Pre-Logic Script Code:
def addzero(f1, f2):
if len(f1) == 1:
return "0" + f1
@HerbFargus
HerbFargus / Delete_Rows_if_Columns_Match.vb
Last active May 1, 2017 17:50
VBA (Excel): Compare two columns and if they match, delete the whole row
' Excel VBA Macro: This compares two columns, if they match the the row is deleted.
Sub Main()
With Application ' Optimise so deleting rows doesnt hang
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
' Set up prompt input boxes for user to choose the columns
@HerbFargus
HerbFargus / Milepost_Generator.vb
Last active May 1, 2017 15:57
VBA (Excel): Filling in the gaps between hundredth mileposts
' Excel VBA Macro: This takes a list of endpoints and automatically generates rows for each hundredth point between the two columns: beginning milepost and the ending milepost
Sub Main()
With Application ' Optimise so inserting rows doesnt hang
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
' Set up prompt input boxes for user to choose the columns
@HerbFargus
HerbFargus / Delete_Row_With_Empty_Cell.vb
Last active May 1, 2017 17:44
VBA (Excel): Check if a cell is empty, if so, delete the entire row
' Excel VBA Macro: This checks if a cell is empty or blank, if so then it will delete the row.
Sub Main()
With Application ' Optimise so deleting rows doesnt hang
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
' Set up prompt input boxes for user to choose the columns
@HerbFargus
HerbFargus / copy_if_blank_or_empty.vb
Last active May 1, 2017 17:47
VBA (Excel): If the cell is blank, copy a value from another cell. Has options if it is blank or if its a null string (which apparently are different things)
' Excel VBA Macro: This compares two columns and if a field is empty, it will copy a value from a different field.
Sub Main()
With Application ' Optimise so deleting rows doesnt hang
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
' Set up prompt input boxes for user to choose the columns