drawing a line with VBA

plost33

Well-known Member
Joined
Oct 2, 2008
Messages
866
How would i have VBA draw a line between a two ranges? say i have a named range of "start" and another name range "stop". i want to draw a red line between them. how can i do that with code. this is beggining my education for a much larger project I want to do later in the future.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try

Rich (BB code):
Sub AddLine()
Dim l1 As Long, l2 As Long, r1 As Long, r2 As Long
l1 = Range("Start").Left + Range("Start").ColumnWidth * 6
l2 = Range("Start").Top + Range("Start").RowHeight
r1 = Range("Stop").Left
r2 = Range("Stop").Top
With ActiveSheet.Shapes.AddLine(l1, l2, r1, r2).Line
    .ForeColor.RGB = RGB(255, 0, 0)
End With
End Sub

you may need to adjust the value 6 to suit.
 
Upvote 0
Try putting this in your loop

Code:
Application.Wait Now + TimeSerial(0, 0, 1)
 
Upvote 0
Yes - hours, minutes, seconds - but here's a hint. Click in the word TimeSerial in the Visual Basic Editor and press F1 to display the help :)
 
Upvote 0
when i run the code now, it is now delaying. it does seem to wait a second before any lines are displayed, but they both appear at the same time. I do not have it in a loop yet, but i will as i continue to build this code. will having it in the code as i do below not make it pause 1 second before moving on? also, will excel always wait to display objects, such as lines, all at once, once the code and finished running? I am trying to make a kind of animation which will draw several lines, but only draw one per second.

Code:
Sub AddLine()
Dim l1 As Long, l2 As Long, r1 As Long, r2 As Long, x1 As Long, x2 As Long
l1 = Range("GI40F").Left 'left side of range
l2 = Range("GI40F").Top + Range("Start").RowHeight ' bottom left corner of range
r1 = Range("GI47C").Left
r2 = Range("GI47C").Top
x1 = Range("WD69C").Left + Range("Start").ColumnWidth * 4 'right side middle
x2 = Range("WD69C").Top + Range("Start").RowHeight ' bottom left corner of range
With ActiveSheet.Shapes.AddLine(l1, l2, r1, r2).Line
    .ForeColor.RGB = RGB(255, 0, 0) 'red
End With
Application.Wait Now + TimeSerial(0, 0, 1) ' waits 1 second before proceeding
With ActiveSheet.Shapes.AddLine(r1, r2, x1, x2).Line
    .ForeColor.RGB = RGB(0, 0, 255) 'blue
End With

End Sub
[\code]
 
Upvote 0
It should delay by one second before adding the second line. Try increasing the 1 to say 3 and see if there is a discernible delay.
 
Upvote 0
it just waits longer then adds all lines at once rather than one at a time seperated by the 1 or 3 second interval.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

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