Selecting diagonal range

Yamasaki450

Board Regular
Joined
Oct 22, 2021
Messages
71
Office Version
  1. 2021
Platform
  1. Windows
Hi guys.

Does anyone know how to select cells diagonally without holding down ctrl and selecting cells individually?

I found this two codes on net they both work but cells are selected in right and down direction...
I want to select cells in up and right direction (see screenshot)
Does anyone know how to adjust any of this codes so the cells are selected in up and right direction?
Or maybe there is another option?

First one

Sub selectDiag()
'UpdatebyExtendoffice20171128
Dim I As Long
Dim xCount As Long
Dim xRg As Range
On Error Resume Next
Set xRg = ActiveCell
If xRg Is Nothing Then Exit Sub
xCount = Val(InputBox("How many cells do you want to select diagonally?", "KuTools For Excel"))
If xCount = 0 Then Exit Sub
For I = 1 To (xCount - 1)
Set xRg = Union(xRg, ActiveCell.Offset(I, I))
Next I
xRg.Select
End Sub

Second one (post 5)

Sub selectDiag()
Dim i As Integer, j As Integer
Dim myRng As Range

Set myRng = ActiveCell
j = Val(InputBox("How many cells do you want to select diagonally?", "Diagonal Cell Selection"))

For i = 1 To j
Set myRng = Union(myRng, ActiveCell.Offset(i, i))
myRng.Select
Next i
End Sub


Thanks.
 

Attachments

  • Clipboard01.jpg
    Clipboard01.jpg
    91.4 KB · Views: 11

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I imagine all you need to change is the first Offset variable to a negative.

Offset(Row,Col) = Offset(-I,I)
 
Upvote 1
Consider that the selected cell must have enough cells above the captured number.

Try this

VBA Code:
Sub selectDiag_3()
  Dim i As Integer, j As Integer
  Dim myRng As Range
  
  Set myRng = ActiveCell
  j = Val(InputBox("How many cells do you want to select diagonally?", "Diagonal Cell Selection"))
  
  For i = 1 To j
    Set myRng = Union(myRng, ActiveCell.Offset(-i, i))
  Next i
  myRng.Select
End Sub
 
Upvote 1
Solution
I imagine all you need to change is the first Offset variable to a negative.

Offset(Row,Col) = Offset(-I,I)
Consider that the selected cell must have enough cells above the captured number.

Try this

VBA Code:
Sub selectDiag_3()
  Dim i As Integer, j As Integer
  Dim myRng As Range
 
  Set myRng = ActiveCell
  j = Val(InputBox("How many cells do you want to select diagonally?", "Diagonal Cell Selection"))
 
  For i = 1 To j
    Set myRng = Union(myRng, ActiveCell.Offset(-i, i))
  Next i
  myRng.Select
End Sub
Thanks a lot guys. Both solutions work... You are awesome :)
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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