Variable in VBA to shorten repeated code

ChristineJ

Well-known Member
Joined
May 18, 2009
Messages
761
Office Version
  1. 365
Platform
  1. Windows
I need to use Range("F12").Offset(L, K).Value repeatedly in my VBA code, although the cell reference (F12 in this example) will differ. "L" and "K" will always be the same.

Is it possible to make all that repeats (all but the cell reference) a variable with a short name to shorten the code?
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Please post your code and explain in detail what you are trying to do.
 
Upvote 0
I need to use Range("F12").Offset(L, K).Value repeatedly in my VBA code, although the cell reference (F12 in this example) will differ. "L" and "K" will always be the same.

Is it possible to make all that repeats (all but the cell reference) a variable with a short name to shorten the code?
In that case declare a range object and initialize it as the begining of the code

VBA Code:
Dim ws As Worksheet
Dim rng As Range

'~~> Change as applicable
Set ws = Sheet1
'~~> Set your range here
Set rng = ws.Range("F12")
'
'
'
rng.Offset(L, K).Value = "Something"
'
'
'
rng.Offset(L, K).ClearContents
'
'
'
'And so on

The good thing with this method is that if you want to change the range from say F12 to A1 then you will have to do it at only one location.
 
Upvote 0
Thanks to you both for your replies. I am using this code to evaluate user (student) input. The code below evaluates the offset of cell E12. This is just one of many cells to be evaluated, all with different math to get to the right answer. The ".Offset(L, K).Value" appears many times in the code and I was wondering it a single character could be substituted for it to make all of this code shorter. Not essential, but nice to have. Thanks.

Code:
If Range("A12").Offset(L, K).Value + Range("B12").Offset(L, K).Value + Range("C12").Offset(L, K).Value * Range("D12").Offset(L, K).Value = Range("E12").Offset(L, K).Value Then
Range("F12").Offset(L, K).Value = "Correct"
Else
Range("F12").Offset(L, K).Value = "The amount is incorrect, so the formula will not be evaluated at this time."
End If
 
Upvote 0
What do the variables L and K represent? Can you post some sample data?
 
Upvote 0
Is it always columns A thru F? If so then something like this is possible to shorten line length.

VBA Code:
    With Range("A12")
        If .Offset(L, K).Value + .Offset(L, K + 1).Value + .Offset(L, K + 2).Value * .Offset(L, K + 3).Value = .Offset(L, K + 4).Value Then
            .Offset(L, K + 5).Value = "Correct"
        Else
            .Offset(L, K + 5).Value = "The amount is incorrect, so the formula will not be evaluated at this time."
        End If
    End With
 
Upvote 0
you could use a custom function

VBA Code:
Function CalcCheck(r As Range) As String
    If r(1, 1) + r(1, 2) + r(1, 3) * r(1, 4) = r(1, 5) Then
        CalcCheck = "Correct"
    Else
        CalcCheck = "The amount is incorrect, so the formula will not be evaluated at this time."
    End If
End Function

and invoke it like this

VBA Code:
Set a = Range("A12").Offset(L, K)
a.Offset(0, 5) = CalcCheck(a)
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,093
Latest member
dbomb1414

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