How can I use the offset property in VBA to populate a variable in Excel?

  • Thread starter Thread starter jsbxd9
  • Start date Start date
  • Tags Tags
    Debugging Excel
Click For Summary

Discussion Overview

The discussion revolves around using the offset property in VBA to manipulate cell values in Excel. Participants are addressing a specific coding issue related to coloring cells based on the values of referenced cells, particularly focusing on how to correctly use the offset property to avoid errors.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a problem with populating a variable 'strMyVal' using the offset property, indicating a potential error in the code.
  • Another participant suggests checking if the object referenced by ActiveCell.Offset(-3, 0) exists to avoid null reference errors.
  • A different participant points out that the issue may arise from attempting to access a cell with a negative row number, suggesting the use of a debug print statement to identify the active cell before the error occurs.
  • A participant, identifying as an engineer rather than a programmer, asks for clarification on how to reference a cell to the left of the active cell.
  • Another participant explains the offset method, detailing how to reference adjacent cells using row and column offsets.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the specific coding issue, as multiple perspectives on the problem and its potential solutions are presented. The discussion remains unresolved regarding the best approach to address the error encountered.

Contextual Notes

There is uncertainty regarding the conditions under which the offset property may lead to errors, particularly concerning negative row references and the existence of the referenced cells.

jsbxd9
Messages
5
Reaction score
0
Hello all. I am trying to get my code to color cells if a referenced cell is populated. That part works but when I tried to populate another cell a different color I encountered a problem. You can see where the mistake comes in on the code. It is when I try to populate a variable 'strMyVar' with an offset property. I have underlined where I am having the problem. Any suggestions?

Sub colorcells()
Dim x, n, y, i As Double
Dim strMyVal As String
For n = 1 To 3000
Cells(n, 1).Activate
If Cells(n, 1) <> ("") Then
For x = 1 To 7
If x < 7 Then
Cells(n, x).Activate
ActiveCell.Interior.Color = RGB(252, 213, 180)
End If
If x >= 7 Then
Cells(n, x).Activate
strMyVal = ActiveCell.Offset(-3, 0).Value
If strMyVal <> ("") Then
ActiveCell.Interior.Color = RGB(255, 255, 153)
End If
End If
Next x
x = 1
End If
Next n
End Sub
 
Technology news on Phys.org
jsbxd9 said:
Hello all. I am trying to get my code to color cells if a referenced cell is populated. That part works but when I tried to populate another cell a different color I encountered a problem. You can see where the mistake comes in on the code. It is when I try to populate a variable 'strMyVar' with an offset property. I have underlined where I am having the problem. Any suggestions?

Code:
Sub colorcells()
Dim x, n, y, i As Double
Dim strMyVal As String
For n = 1 To 3000
Cells(n, 1).Activate
    If Cells(n, 1) <> ("") Then
        For x = 1 To 7
            If x < 7 Then
                Cells(n, x).Activate
                ActiveCell.Interior.Color = RGB(252, 213, 180)
            End If
            If x >= 7 Then
                Cells(n, x).Activate
                [U]strMyVal = ActiveCell.Offset(-3, 0).Value[/U]
                    If strMyVal <> ("") Then
                        ActiveCell.Interior.Color = RGB(255, 255, 153)
                    End If
            End If
        Next x
        x = 1
    End If
Next n
End Sub
I wrapped CODE tags around your code so that it would stay formatted. It's been a long time since I've coded anything in VBA but the first thing that jumps out at me is the question of whether ActiveCell.Offset(-3, 0) is ever null. If so, attempting to retrieve value from a null object will break.
 
Following on from what Borg mentioned above, can you first check that your object being referenced exists?

What is the ActiveCell object structure and what are the valid values for the offset index parameter?
 
The problem is you are trying to address a cell with a negative row number. If you put a debug print statement in your code like the below, you'll see that the active cell is $G$1 just before the error. The offset operation tries to address a negative row value, which is illegal.

Code:
Cells(n, x).Activate
Debug.Print ActiveCell.Address
strMyVal = ActiveCell.Offset(-3, 0).Value
 
Thanks for all the input. Very helpful. I am not a programmer just an engineer trying to make my job a little easier.

Hotvette, how should I reference a cell to the left of an active cell??
 
For the offset method (should be documented in VBA help), the first parameter is row offset and the 2nd is column offset:

Code:
strMyVal = ActiveCell.Offset(0, -1).Value   'cell on immediate left of active cell
strMyVal = ActiveCell.Offset(0, 1).Value    'cell on immediate right of active cell
strMyVal = ActiveCell.Offset(-1, 0).Value   'cell immediately above the active cell
strMyVal = ActiveCell.Offset(1, 0).Value    'cell immediately below the active cell
 

Similar threads

  • · Replies 18 ·
Replies
18
Views
6K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K