Solved

offset function

Posted on 2013-11-19
2
316 Views
Last Modified: 2013-11-21
folks

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
0
Comment
Question by:rutgermons
2 Comments
 
LVL 46

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
ID: 39661192
ActiveSheet.Cells(row, col + 1).Offset(0, 1).Value = Rslt

or

ActiveSheet.Cells(row, col + 2).Value = Rslt
0
 
LVL 4

Accepted Solution

by:
yuppydu earned 250 total points
ID: 39661856
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
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question