• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 369
  • Last Modified:

offset function


my below statement write a value based on a criteria in colum A2 to B1

ActiveSheet.Cells(row, col + 1).Value = Rslt

how can I get it to offset to B2 instead

all help will do
2 Solutions
Martin LissOlder than dirtCommented:
ActiveSheet.Cells(row, col + 1).Offset(0, 1).Value = Rslt


ActiveSheet.Cells(row, col + 2).Value = Rslt
Hi rutgermons, your problem is an easy issue and MartinLiss gave you the quick answer. If I may I'd like to suggest you a couple of coding procedure which will make you code easier to maintain and debug.
Try not to use ActiveSheet, it is too generic and you have to read the full code to understand which is the active sheet. One good way to organize your code would be to:
1) Rename the sheets, not the tab name but the actual name in VBA. One good standard could be to give a name which starts with wks (i.e. wksDataSheet). Doing this no matter how you name the sheet in the sheet tabs your code will still work.
2) Give a range name to the leftmost cell in you working area with the scope set to the sheet and not to the workbook. (this allows to have the same name which indicates the cell where to start offsetting throughout the code). Let's make that name  StartingCell
3) at this point your code could look like this: wksDataSheet.Range("StartingCell").offset(0,2).value = Rslt
4) To make it even simpler to use, in case you have to perform many offsets on different sheets, you can:
declare a global variable in a different module as
 Public Const gszSTARTING_CELL As String = "StartingCell"

in the code you then add:
 Dim rngStartingCell as Excel Range
Set rngStartingCell = wksDataSheet.Range(gszSTARTING_CELL)

and you can then perform all your offsets simply with:

rngStartingCell.offset(0,2) = Rslt

If you have multiple offsets in the same sheet you could use With, which reduces the amount of code you have to write.

With rngStartingCell
.Offset(0,2) = Rslt
.offset(0,3) = Rslt +1
.Offest1(1,0) = Rslt +2
End With

Moreover with this method you do not need to use Application.ScreenUpdating = False as you are not activating any sheet.

Hope it is clear and it helps writing easier code.
Have a good day

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Tackle projects and never again get stuck behind a technical roadblock.
Join Now