Locate 2d lines

SCSabresfan

New Member
Joined
Sep 30, 2023
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Is there a code to line up an existing line to the bottom center or top center of a specific cell? I have a bunch of lines that I change the visibility based of of cell values. I need an easier way to align the lines. My lines are named something similar like LineATop, LineAMiddle and LineABottom
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
1696428955100.png

With the setup above, this code will place LineATop to the top of G4 cell, LineAMiddle to the center of J6 cell and LineABottom to the bottom of N8 cell.
VBA Code:
Sub test()
  Dim r As Long, c As Long, rowsHeights As Double, columnsWidth As Double
  Dim myDocument As Worksheet
  Dim objLine As Line
  Set myDocument = Worksheets("Sheet1")
 
  With myDocument 'Change here
  For Each objLine In .Lines
    rowsHeights = 0
    columnsWidth = 0
    Select Case objLine.Name
    Case "LineATop"
      For r = 1 To .Range(.Range("A2").Value).Row - 1
        rowsHeights = rowsHeights + .Cells(r, 1).Height
      Next
      For c = 1 To .Range(.Range("A2").Value).Column - 1
        columnsWidth = columnsWidth + .Cells(1, c).Width
      Next
      objLine.Top = rowsHeights
      objLine.Left = columnsWidth
      Case "LineAMiddle"
      For r = 1 To .Range(.Range("B2").Value).Row - 1
        rowsHeights = rowsHeights + .Cells(r, 1).Height
      Next
      For c = 1 To .Range(.Range("B2").Value).Column - 1
        columnsWidth = columnsWidth + .Cells(1, c).Width
      Next
      rowsHeights = rowsHeights + (.Cells(r, 1).Height / 2)
      objLine.Top = rowsHeights
      objLine.Left = columnsWidth
      Case "LineABottom"
      For r = 1 To .Range(.Range("C2").Value).Row
        rowsHeights = rowsHeights + .Cells(r, 1).Height
      Next
      For c = 1 To .Range(.Range("C2").Value).Column - 1
        columnsWidth = columnsWidth + .Cells(1, c).Width
      Next
      objLine.Top = rowsHeights
      objLine.Left = columnsWidth
    End Select
  Next
  End With
End Sub
Please find the sample file below:
 
Upvote 0
One small adjustment for different hicknesses:
VBA Code:
Sub test()
  Dim r As Long, c As Long, rowsHeights As Double, columnsWidth As Double
  Dim myDocument As Worksheet
  Dim objLine As Line
  Set myDocument = Worksheets("Sheet1")
  
  With myDocument
  For Each objLine In .Lines
    rowsHeights = 0
    columnsWidth = 0
    Select Case objLine.Name
    Case "LineATop"
      For r = 1 To .Range(.Range("A2").Value).Row - 1
        rowsHeights = rowsHeights + .Cells(r, 1).Height
      Next
      For c = 1 To .Range(.Range("A2").Value).Column - 1
        columnsWidth = columnsWidth + .Cells(1, c).Width
      Next
      objLine.Top = rowsHeights + (objLine.Border.Weight / 2)
      objLine.Left = columnsWidth
      Case "LineAMiddle"
      For r = 1 To .Range(.Range("B2").Value).Row - 1
        rowsHeights = rowsHeights + .Cells(r, 1).Height
      Next
      For c = 1 To .Range(.Range("B2").Value).Column - 1
        columnsWidth = columnsWidth + .Cells(1, c).Width
      Next
      rowsHeights = rowsHeights + (.Cells(r, 1).Height / 2)
      objLine.Top = rowsHeights
      objLine.Left = columnsWidth
      Case "LineABottom"
      For r = 1 To .Range(.Range("C2").Value).Row
        rowsHeights = rowsHeights + .Cells(r, 1).Height
      Next
      For c = 1 To .Range(.Range("C2").Value).Column - 1
        columnsWidth = columnsWidth + .Cells(1, c).Width
      Next
      objLine.Top = rowsHeights - (objLine.Border.Weight / 2)
      objLine.Left = columnsWidth
    End Select
  Next
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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