Question : using a variable to define/select a range in excel VBA

I have never written code before this. I am trying to write code to solve for displacements in a truss. I have almost finished however I am stuck on selecting a range that is dependent on a variable.

It is for a square matrix.
The size of the matrix is an input in another sheet.
I want to select a range of n X n cells in one sheet and set them equal the inverse of the same cells in another sheet using minverse().
Is this possible?

This is one thing I have tried

Dim zzz As Integer
Dim yyy As Integer
zzz = 1
yyy = Sheets("input").Range("F6").Value
Sheets("kff-1").Activate
Cells(yyy, yyy).Select
ActiveCell.Range(selection, Cells(1)).Select


I have also tried

Sheets("kff-1").Activate
ActiveSheet.Range(Cells(1, 1), Cells(5, 5)).Select

neither will work.

Answer : using a variable to define/select a range in excel VBA

<using a variable to define/select a range in excel VBA>

At the most basic level, this macro will define a range variable and select that range. The sub assumes A1 as a starting point and allows the user to define the end point by supplying numeric coordinates. This can be modified easily so that the user can set both the start and end points. You would simply need to use variables instead of "Cells(1, 1)" for the starting point in the range.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
Sub SelectMatrix(intRow As Integer, intCol As Integer)
 
    Dim rng As Range '<----- This is your range variable
    Dim ws As Worksheet <---- This is your worksheet
 
    Set ws = Application.ActiveSheet '<---- Set the worksheet to be the current active sheet
    Set rng = ws.Range(Cells(1, 1), Cells(intRow, intCol)) '<--- define the range as A1 (1,1) to the user-supplied endpoint
 
    rng.Select '<--- Select the range
 
    Set ws = Nothing '<--- Clear the objects
    Set rng = Nothing
End Sub
Random Solutions  
 
programming4us programming4us