VBA Macro Programming: Fix "Selection.Offset()" Problem

  • Thread starter big man
  • Start date
  • Tags
    Programming
In summary, John Walkenbach says that the Offset method is useful for referring to ranges. The Offset method assigns a value to a cell by offsetting it by a number of rows and columns. The Sub FillRange2() example fills a range (rowwise) with consecutive numbers from 1 to 100.
  • #1
big man
254
1
Hi

I just have a quick question about a VBA program I've been writing. It's mostly working, but there is a slight problem when I use the "Selection.Offset" capability. For example, if I had a range selected of $C$23:$C$34 then I would have thought that if I did "MsgBox Selection.Offset(0, 1).Address" I would get a new range of $D$23:$D$34. However, what I get is actually D23 and that's all. There essentially isn't any range anymore. I've attached the section of code for you to look at so that maybe it makes more sense, but hopefully you have an idea as to why this isn't bloody working. I have to warn you though that I am a messy coder and so it isn't the most elegant work.

For Counter = 1 To NumberofColumns

If Counter = NumberofColumns - 3 Or Counter = NumberofColumns - 2 Or Counter = NumberofColumns - 1 Then
GoTo 124
End If


If AccessNoLoc - Counter = 0 Then
GoTo 124 'GOTO "Next Counter" in this loop
ElseIf AccessNoLoc - Counter < 0 Then
GoTo 115 'GOTO "Index = Counter - AccessNoLoc"
Else
For n = 1 To Rangarrsize
SelRange = Beginning(n) & ":" & Final(n)
Range(SelRange).Select
MsgBox Selection.Offset(0, -Counter).Address
With Selection
.MergeCells = True
End With
Next n
End If

115 Index = Counter - AccessNoLoc

If Index > 0 Then
For n = 1 To Rangarrsize
SelRange = Beginning(n) & ":" & Final(n)
Range(SelRange).Select
MsgBox Selection.Offset(0, Index).Address
With Selection
.MergeCells = True
End With
Next n
End If

124 Next Counter

Appreciate any assistance!
 
Technology news on Phys.org
  • #2
This is from John Walkenbach's web site. He's the guru on this:
http://www.j-walk.com/ss/

The Offset method is another useful way to refer to ranges. The Offset method returns a Range object, and takes two arguments. The first argument represents the number of rows to offset; the second represents the number of columns to offset.

The following statement assigns the value 1 to the cell that is one row below cell C2 and two cells to the right of C2 (i.e., cell E3):

Range("C2").Offset(1,2).Value = 1

The Offset method is most useful when the arguments are variables, rather than numbers. The subroutine below fills a 10X10 range (rowwise) with consecutive numbers from 1 to 100.
Code:
Sub FillRange2()
      Num = 1
      For Row = 0 To 9
          For Col = 0 To 9
              Sheets("Sheet1").Range("A1").Offset(Row,Col).Value = Num
              Num = Num + 1
          Next Col
      Next Row
End Sub
I think with a little stroking you can make this fir your needs.
 
  • #3
Thanks for that Fred. I've modified it now so that I've broken the "range" up into a beginning point and end point and so now I offset each part.

So as you can see by the code below I have a beginning cell (beg) and an end cell (fin).
Now when I put a MsgBox to print both of these cell addresses I get say D1 for beg and D3 for fin. Then when I do Range(Beg).Select and I offset the Beg cell by 1 column using Selection.Offset(0,1).* and I print the address I get E1. However, the problem arises in the next line when I do a new selection. The new selection is selecting the end cell. But when I print the offset for this selection I again get E1 instead of getting E3. Is there something I'm missing?

For n = 1 To Rangarrsize
Beg = Beginning(n)
Fin = Final(n)
Range(Beg).Select
def = Selection.Offset(0, 1).Address
MsgBox def
Range(Fin).Select
abc = Selection.Offset(0, 1).Address
MsgBox abc
Next n
 

1. What is VBA Macro Programming?

VBA Macro Programming is a programming language used to automate tasks in Microsoft Excel. It allows users to create custom functions, automate repetitive tasks, and manipulate data within Excel.

2. What is the "Selection.Offset()" Problem in VBA Macro Programming?

The "Selection.Offset()" Problem refers to a common issue in VBA Macro Programming where the code does not properly refer to the correct cell or range. This can cause errors and unexpected results in the program.

3. How can I fix the "Selection.Offset()" Problem?

To fix the "Selection.Offset()" Problem, you can use the "ActiveCell" function instead of "Selection". This ensures that the code refers to the active cell, rather than the selected cell. Alternatively, you can specify a specific cell or range in the code instead of relying on the selection.

4. Can I use VBA Macro Programming in other Microsoft Office programs?

Yes, VBA Macro Programming can be used in other Microsoft Office programs such as Word and PowerPoint. However, the syntax and commands may differ slightly from those used in Excel.

5. Do I need to have prior programming experience to use VBA Macro Programming?

While prior programming experience can be helpful, it is not necessary to use VBA Macro Programming. The language is designed to be user-friendly and can be learned through online tutorials and practice. However, a basic understanding of Excel and its functions is recommended.

Similar threads

  • Programming and Computer Science
Replies
22
Views
749
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
5
Views
879
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top