Using a UDF to determin if 2 lines are parallel, perpendicular, or neither

extrahotfudge

New Member
Joined
Apr 9, 2021
Messages
10
Office Version
  1. 365
  2. 2019
Platform
  1. MacOS
ve four points (x1,y1), (x2,y2), (x3,y3), and (x4,y4). The first two points define line 1 and the last two points define line 2. I'm trying to use a user defined function to determine the status of the two lines, so either Parallel, Perpendiculr, or Neither. This is what I have for the code, but I am getting a #VALUE! Error. Anyone have any ideas on what to use?

VBA Code:
Option Explicit
Function Line_Status(x1, y2, x2, y2, x3, y3, x4, y4)
    Dim s1 As Double
    Dim s2 As Double
    
    s1 = (y2 - y1) / (x2 - x1)
    s2 = (y4 - y3) / (x4 - x3)
    
    If s1 = s2 Then
    Line_Status = "parallel"
    
    If s1 / s2 = -1 Then
    Line_Status = "Perpendicular"
    
    Else
    Line_Status = "Neither"
    
    End If
End Funtion
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Correction : ElseIf s1 / s2 = -1 Then …​
 
Upvote 0
As you did not declare the type of each variable passed to the function so post at least some accurate samples with the expected results …​
 
Upvote 0
As you did not declare the type of each variable passed to the function so post at least some accurate samples with the expected results …​
Here is what is in my sheet, here K2 should return "Neither"

lab7.xlsm
ABCDEFGHIJK
1x1y1x2y2x3y3x4y4m1m2Status
2133426780.50.4#VALUE!
3
Lines_2b (FINISH)
Cell Formulas
RangeFormula
I2I2=IFERROR((D2-B2)/(C2-A2),"Undefined")
J2J2=IFERROR((H2-F2)/(G2-E2),"Undefined")
K2K2=Line_Status(A2,B2,C2,D2,E2,F2,G2,H2)
 
Upvote 0
Typo to be corrected : End Function …​
 
Upvote 0

Yes 'cause of another error in the Function codeline, don't you see the VBE alert ?! As there are two 'y2' but no 'y1' …​
 
Upvote 0
Solution
Also instead of If s1 / s2 = -1 Then use If s1 * s2 = -1 Then.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top