Using Offset to select negative cell range using variables

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
336
Office Version
  1. 2019
I utilise Excel a heap as digital graph paper by reducing column widths to make squares. I then use the border tools to draw, which involves selecting cells. To make this easier I'm working on a shortcut macro to select the required cells with respect to the currently selected cell as the start point. If both X and Y axis values are positive, I have no problem, but I can't work out how to use negative references to go left and/or up from my starting point. My macro as it is currently is:
VBA Code:
Sub SelectCells()

Dim SelCols As Single, SelRows As Single

SelCols = Application.InputBox("Enter X axis cells to select")
If SelCols = False Then
    Exit Sub
End If
SelRows = Application.InputBox("Enter Y axis cells to select")
If SelRows = False Then
    Exit Sub
End If

If SelCols < 0 And SelRows > 0 Then 'if start point cell is TOP RIGHT
        ActiveCell.Offset(0, SelCols + 1).Resize(SelRows, SelCols).Select
    ElseIf SelCols > 0 And SelRows < 0 Then ' if start point cell is BOTTOM LEFT
        ActiveCell.Offset(SelRows + 1, 0).Resize(SelRows, SelCols).Select
    ElseIf SelCols < 0 And SelRows < 0 Then ' if start point cell is BOTTOM RIGHT
        ActiveCell.Offset(SelRows + 1, SelCols + 1).Resize(SelRows, SelCols).Select
    Else: ActiveCell.Resize(SelRows, SelCols).Select 'if start point cell is TOP LEFT
End If

End Sub

I presume my problem is that I can't do a maths operation within the Offset function. Is that correct?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Never mind... I finally realised I was still trying to feed Resize with negative numbers!
 
Upvote 0

Forum statistics

Threads
1,215,500
Messages
6,125,168
Members
449,211
Latest member
ykrcory

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