Skip to content

Instantly share code, notes, and snippets.

@dfinke
Created December 6, 2015 20:14
Show Gist options
  • Select an option

  • Save dfinke/acd93a68faaf4b01bf8c to your computer and use it in GitHub Desktop.

Select an option

Save dfinke/acd93a68faaf4b01bf8c to your computer and use it in GitHub Desktop.
Calculate Excel Column Name from a Column Number
function Get-ExcelColumnName {
param(
[Parameter(ValueFromPipeline=$true)]
$columnNumber=1
)
Process {
$dividend = $columnNumber
$columnName = @()
while($dividend -gt 0) {
$modulo = ($dividend - 1) % 26
$columnName += [char](65 + $modulo)
$dividend = [int](($dividend -$modulo)/26)
}
[PSCustomObject] @{
ColumnNumber = $columnNumber
ColumnName = $columnName -join ''
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment