Skip to content

Instantly share code, notes, and snippets.

@Aviator-Coding
Forked from f-steff/excel formulas.md
Created January 4, 2024 16:42
Show Gist options
  • Save Aviator-Coding/1cbdc9f515f64ac6d7924e3eaccfd7c9 to your computer and use it in GitHub Desktop.
Save Aviator-Coding/1cbdc9f515f64ac6d7924e3eaccfd7c9 to your computer and use it in GitHub Desktop.

Revisions

  1. @f-steff f-steff revised this gist Dec 11, 2023. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions excel formulas.md
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,16 @@
    Excel formulas to calculate IP values - Works in Excel and Google Sheet.
    ============
    Updated March 16. 2023
    Updated 2023-12-11: Hosts calculation updated to support CIDR or 31 and 32.

    Prerequisites:
    * A1 contains an IP address, such as 10.0.0.2
    * B1 contains the number of bits in the netmask (CIDR) such as 24

    The below formulas then go into C1, D1 etc. to perform the various calculations. Some calculations depends on other calculations.

    A Google Sheet with all the formulas can be accessed and copied here: https://docs.google.com/spreadsheets/d/1G-vStX0DRB7tq-wvbALnL4HeXYXXOkjh_GriTN-b6-o (Also updated march 16. 2023)
    A Google Sheet with all the formulas can be accessed and copied here: https://docs.google.com/spreadsheets/d/1G-vStX0DRB7tq-wvbALnL4HeXYXXOkjh_GriTN-b6-o (Also updated 2023-12-11)

    Note, the formulas below are written with the comma spreadsheet notation used in many countries globally. You may need to change the notation to semicolon notation, which is primearly used in the U.S. E.g. **=ROUND(PI(),3)** and **=ROUND(PI();3)** are identical but uses different formula notation.
    Note, the formulas below are written with the comma spreadsheet notation used in most countries globally. You may need to change the notation to semicolon notation, which is primearly used in the U.S. E.g. **=ROUND(PI(),3)** and **=ROUND(PI();3)** are identical but uses different formula notation.

    Huge thanks to @6d6163 and @Baribf for pointing out a mistake when used in Excel (The instance_num in Substitute() must not be zero!) and for spotting that the StartIP in a range must be one higher than the range number. They also spotted a copy/paste error mixing up A1 and C1. Their work is what triggred the march 16. 2023 update of these formulas.

    @@ -100,8 +100,9 @@ BITOR(

    G1: Calculate number of hosts
    -------------------------
    Updated the method of calculating the number of hosts, to peoperly calculate the number of hosts when CIDR is set to 31 (peer-to-peer network) and 32 which is a single IP.
    ```excel
    =2^(32-B1)-2
    =switch(B1, 31,2, 32,1, (2^(32-B1)-2))
    ```

    Example
  2. @f-steff f-steff revised this gist Mar 16, 2023. 1 changed file with 46 additions and 52 deletions.
    98 changes: 46 additions & 52 deletions excel formulas.md
    Original file line number Diff line number Diff line change
    @@ -1,121 +1,115 @@
    Excel formulas to calculate IP values - Works in Google Sheet, too! :-)
    Excel formulas to calculate IP values - Works in Excel and Google Sheet.
    ============
    Updated March 16. 2023

    Prerequisites:
    * A1 contains an IP address, such as 10.0.0.2
    * B1 contains the number of bits in the netmask (CIDR) such as 24

    The below formulas then go into C1, D1 etc. to perform the various calculations. Some calculations depends on other calculations.

    A Google Sheet with all the formulas can be accessed and copied here: https://docs.google.com/spreadsheets/d/1G-vStX0DRB7tq-wvbALnL4HeXYXXOkjh_GriTN-b6-o/edit?usp=sharing
    A Google Sheet with all the formulas can be accessed and copied here: https://docs.google.com/spreadsheets/d/1G-vStX0DRB7tq-wvbALnL4HeXYXXOkjh_GriTN-b6-o (Also updated march 16. 2023)

    Note, the formulas below are written with the comma spreadsheet notation used in many countries globally. You may need to change the notation to semicolon notation, which is primearly used in the U.S. E.g. **=round(pi(),3)** and **=round(pi();3)** are identical but uses different formula notation.
    Note, the formulas below are written with the comma spreadsheet notation used in many countries globally. You may need to change the notation to semicolon notation, which is primearly used in the U.S. E.g. **=ROUND(PI(),3)** and **=ROUND(PI();3)** are identical but uses different formula notation.

    Huge thanks to @6d6163 and @Baribf for pointing out a mistake when used in Excel (The instance_num in Substitute() must not be zero!) and for spotting that the StartIP in a range must be one higher than the range number. They also spotted a copy/paste error mixing up A1 and C1. Their work is what triggred the march 16. 2023 update of these formulas.

    C1: Calculate netmask
    -----------------
    ```excel
    =bin2dec(mid(REPT("1",B1)&rept("0",32-B1),1,8))&"."&
    bin2dec(mid(REPT("1",B1)&rept("0",32-B1),9,8))&"."&
    bin2dec(mid(REPT("1",B1)&rept("0",32-B1),17,8))&"."&
    bin2dec(mid(REPT("1",B1)&rept("0",32-B1),25,8))
    =BIN2DEC(MID(REPT("1",B1)&REPT("0",32-B1),1,8))
    &"."&
    BIN2DEC(MID(REPT("1",B1)&REPT("0",32-B1),9,8))
    &"."&
    BIN2DEC(MID(REPT("1",B1)&REPT("0",32-B1),17,8))
    &"."&
    BIN2DEC(MID(REPT("1",B1)&REPT("0",32-B1),25,8))
    ```

    D1: Calculate start of IP range
    ---------------------------
    ```excel
    =BITAND(
    (LEFT(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),0))-1))
    ,
    (bin2dec(mid(REPT("1",B1)&rept("0",32-B1),1,8)))
    (LEFT(A1, FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),1))-1)),
    (BIN2DEC(MID(REPT("1",B1)&REPT("0",32-B1),1,8)))
    )
    &"."&
    BITAND(
    (MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),1))+1,find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))-find(char(160),SUBSTITUTE(C1,".",CHAR(160),1))))
    ,
    (bin2dec(mid(REPT("1",B1)&rept("0",32-B1),9,8)) )
    BITAND(
    (MID(A1, FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),1))+1,FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),2))-FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),1))-1)),
    (BIN2DEC(MID(REPT("1",B1)&REPT("0",32-B1),9,8)))
    )
    &"."&
    BITAND(
    (MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))+1,find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))-find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))))
    ,
    (bin2dec(mid(REPT("1",B1)&rept("0",32-B1),17,8)))
    (MID(A1, FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),2))+1,FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),3))-FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),2))-1)),
    (BIN2DEC(MID(REPT("1",B1)&REPT("0",32-B1),17,8)))
    )
    &"."&
    BITAND(
    (MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))+1,find(char(160),SUBSTITUTE(A1&".",".",CHAR(160),4))-find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))-1))
    ,
    bin2dec(mid(REPT("1",B1)&rept("0",32-B1),25,8))
    )
    (MID(A1, FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),3))+1,FIND(CHAR(160),SUBSTITUTE(A1&".",".",CHAR(160),4))-FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),3))-1)),
    (BIN2DEC(MID(REPT("1",B1)&REPT("0",32-B1),25,8)))
    )+1
    ```

    E1: Calculate end of IP range
    -------------------------
    ```excel
    =BITOR(
    (LEFT(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),0))-1))
    ,
    (255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),1,8)))
    =BITOR(
    (LEFT(A1, FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),1))-1)),
    (255-BIN2DEC(MID(REPT("1",B1)&REPT("0",32-B1),1,8)))
    )
    &"."&
    BITOR(
    (MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),1))+1,find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))-find(char(160),SUBSTITUTE(C1,".",CHAR(160),1))))
    ,
    (255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),9,8)) )
    BITOR(
    (MID(A1, FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),1))+1,FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),2))-FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),1))-1)),
    (255-BIN2DEC(MID(REPT("1",B1)&REPT("0",32-B1),9,8)) )
    )
    &"."&
    BITOR(
    (MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))+1,find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))-find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))))
    ,
    (255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),17,8)))
    (MID(A1, FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),2))+1,FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),3))-FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),2))-1)),
    (255-BIN2DEC(MID(REPT("1",B1)&REPT("0",32-B1),17,8)))
    )
    &"."&
    BITOR(
    (MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))+1,find(char(160),SUBSTITUTE(A1&".",".",CHAR(160),4))-find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))))
    ,
    (255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),25,8)))
    (MID(A1, FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),3))+1,FIND(CHAR(160),SUBSTITUTE(A1&".",".",CHAR(160),4))-FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),3))-1)),
    (255-BIN2DEC(MID(REPT("1",B1)&REPT("0",32-B1),25,8)))
    )-1
    ```

    F1: Calculate Broadcast IP
    ----------------------
    ```excel
    =BITOR(
    (LEFT(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),0))-1))
    ,
    (255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),1,8)))
    =BITOR(
    (LEFT(A1, FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),1))-1)),
    (255-BIN2DEC(MID(REPT("1",B1)&REPT("0",32-B1),1,8)))
    )
    &"."&
    BITOR(
    (MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),1))+1,find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))-find(char(160),SUBSTITUTE(C1,".",CHAR(160),1))))
    ,
    (255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),9,8)) )
    BITOR(
    (MID(A1, FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),1))+1,FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),2))-FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),1))-1)),
    (255-BIN2DEC(MID(REPT("1",B1)&REPT("0",32-B1),9,8)))
    )
    &"."&
    BITOR(
    (MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))+1,find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))-find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))))
    ,
    (255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),17,8)))
    (MID(A1, FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),2))+1,FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),3))-FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),2))-1)),
    (255-BIN2DEC(MID(REPT("1",B1)&REPT("0",32-B1),17,8)))
    )
    &"."&
    BITOR(
    (MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))+1,find(char(160),SUBSTITUTE(A1&".",".",CHAR(160),4))-find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))))
    ,
    (255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),25,8)))
    (MID(A1, FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),3))+1,FIND(CHAR(160),SUBSTITUTE(A1&".",".",CHAR(160),4))-FIND(CHAR(160),SUBSTITUTE(A1,".",CHAR(160),3))-1)),
    (255-BIN2DEC(MID(REPT("1",B1)&REPT("0",32-B1),25,8)))
    )
    ```

    G1: Calculate number of hosts
    -------------------------
    ```excel
    =2^(32-B1)-1
    =2^(32-B1)-2
    ```

    Example
    =======
    For an IP setup such as 10.100.10.20/20 the following values will be calculated:

    * Netmask: 255.255.240.0
    * Start IP: 10.100.0.0
    * Start IP: 10.100.0.1
    * End IP: 10.100.15.254
    * Broadcast IP: 10.100.15.255
    * Number of hosts: 4095
    * Number of hosts: 4094
  3. @f-steff f-steff revised this gist Sep 22, 2022. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions excel formulas.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,13 @@ Prerequisites:
    * A1 contains an IP address, such as 10.0.0.2
    * B1 contains the number of bits in the netmask (CIDR) such as 24

    Calculate netmask
    The below formulas then go into C1, D1 etc. to perform the various calculations. Some calculations depends on other calculations.

    A Google Sheet with all the formulas can be accessed and copied here: https://docs.google.com/spreadsheets/d/1G-vStX0DRB7tq-wvbALnL4HeXYXXOkjh_GriTN-b6-o/edit?usp=sharing

    Note, the formulas below are written with the comma spreadsheet notation used in many countries globally. You may need to change the notation to semicolon notation, which is primearly used in the U.S. E.g. **=round(pi(),3)** and **=round(pi();3)** are identical but uses different formula notation.

    C1: Calculate netmask
    -----------------
    ```excel
    =bin2dec(mid(REPT("1",B1)&rept("0",32-B1),1,8))&"."&
    @@ -14,7 +20,7 @@ bin2dec(mid(REPT("1",B1)&rept("0",32-B1),17,8))&"."&
    bin2dec(mid(REPT("1",B1)&rept("0",32-B1),25,8))
    ```

    Calculate start of IP range
    D1: Calculate start of IP range
    ---------------------------
    ```excel
    =BITAND(
    @@ -42,7 +48,7 @@ BITAND(
    )
    ```

    Calculate end of IP range
    E1: Calculate end of IP range
    -------------------------
    ```excel
    =BITOR(
    @@ -70,7 +76,7 @@ BITOR(
    )-1
    ```

    Calculate Broadcast IP
    F1: Calculate Broadcast IP
    ----------------------
    ```excel
    =BITOR(
    @@ -98,7 +104,7 @@ BITOR(
    )
    ```

    Calculate number of hosts
    G1: Calculate number of hosts
    -------------------------
    ```excel
    =2^(32-B1)-1
  4. @f-steff f-steff revised this gist Mar 5, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion excel formulas.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    Excel formulas to calculate IP values
    Excel formulas to calculate IP values - Works in Google Sheet, too! :-)
    ============

    Prerequisites:
  5. @f-steff f-steff revised this gist Mar 5, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion excel formulas.md
    Original file line number Diff line number Diff line change
    @@ -106,7 +106,7 @@ Calculate number of hosts

    Example
    =======
    For an IP setup such as 10.100.10.20 the following values will be calculated:
    For an IP setup such as 10.100.10.20/20 the following values will be calculated:

    * Netmask: 255.255.240.0
    * Start IP: 10.100.0.0
  6. @f-steff f-steff revised this gist Mar 5, 2021. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions excel formulas.md
    Original file line number Diff line number Diff line change
    @@ -108,8 +108,8 @@ Example
    =======
    For an IP setup such as 10.100.10.20 the following values will be calculated:

    Netmask: 255.255.240.0
    Start IP: 10.100.0.0
    End IP: 10.100.15.254
    Broadcast IP: 10.100.15.255
    Number of hosts: 4095
    * Netmask: 255.255.240.0
    * Start IP: 10.100.0.0
    * End IP: 10.100.15.254
    * Broadcast IP: 10.100.15.255
    * Number of hosts: 4095
  7. @f-steff f-steff created this gist Mar 5, 2021.
    115 changes: 115 additions & 0 deletions excel formulas.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,115 @@
    Excel formulas to calculate IP values
    ============

    Prerequisites:
    * A1 contains an IP address, such as 10.0.0.2
    * B1 contains the number of bits in the netmask (CIDR) such as 24

    Calculate netmask
    -----------------
    ```excel
    =bin2dec(mid(REPT("1",B1)&rept("0",32-B1),1,8))&"."&
    bin2dec(mid(REPT("1",B1)&rept("0",32-B1),9,8))&"."&
    bin2dec(mid(REPT("1",B1)&rept("0",32-B1),17,8))&"."&
    bin2dec(mid(REPT("1",B1)&rept("0",32-B1),25,8))
    ```

    Calculate start of IP range
    ---------------------------
    ```excel
    =BITAND(
    (LEFT(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),0))-1))
    ,
    (bin2dec(mid(REPT("1",B1)&rept("0",32-B1),1,8)))
    )
    &"."&
    BITAND(
    (MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),1))+1,find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))-find(char(160),SUBSTITUTE(C1,".",CHAR(160),1))))
    ,
    (bin2dec(mid(REPT("1",B1)&rept("0",32-B1),9,8)) )
    )
    &"."&
    BITAND(
    (MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))+1,find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))-find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))))
    ,
    (bin2dec(mid(REPT("1",B1)&rept("0",32-B1),17,8)))
    )
    &"."&
    BITAND(
    (MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))+1,find(char(160),SUBSTITUTE(A1&".",".",CHAR(160),4))-find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))-1))
    ,
    bin2dec(mid(REPT("1",B1)&rept("0",32-B1),25,8))
    )
    ```

    Calculate end of IP range
    -------------------------
    ```excel
    =BITOR(
    (LEFT(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),0))-1))
    ,
    (255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),1,8)))
    )
    &"."&
    BITOR(
    (MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),1))+1,find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))-find(char(160),SUBSTITUTE(C1,".",CHAR(160),1))))
    ,
    (255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),9,8)) )
    )
    &"."&
    BITOR(
    (MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))+1,find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))-find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))))
    ,
    (255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),17,8)))
    )
    &"."&
    BITOR(
    (MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))+1,find(char(160),SUBSTITUTE(A1&".",".",CHAR(160),4))-find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))))
    ,
    (255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),25,8)))
    )-1
    ```

    Calculate Broadcast IP
    ----------------------
    ```excel
    =BITOR(
    (LEFT(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),0))-1))
    ,
    (255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),1,8)))
    )
    &"."&
    BITOR(
    (MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),1))+1,find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))-find(char(160),SUBSTITUTE(C1,".",CHAR(160),1))))
    ,
    (255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),9,8)) )
    )
    &"."&
    BITOR(
    (MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))+1,find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))-find(char(160),SUBSTITUTE(A1,".",CHAR(160),2))))
    ,
    (255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),17,8)))
    )
    &"."&
    BITOR(
    (MID(A1, find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))+1,find(char(160),SUBSTITUTE(A1&".",".",CHAR(160),4))-find(char(160),SUBSTITUTE(A1,".",CHAR(160),3))))
    ,
    (255-bin2dec(mid(REPT("1",B1)&rept("0",32-B1),25,8)))
    )
    ```

    Calculate number of hosts
    -------------------------
    ```excel
    =2^(32-B1)-1
    ```

    Example
    =======
    For an IP setup such as 10.100.10.20 the following values will be calculated:

    Netmask: 255.255.240.0
    Start IP: 10.100.0.0
    End IP: 10.100.15.254
    Broadcast IP: 10.100.15.255
    Number of hosts: 4095