Solved

offset function

Posted on 2013-11-19
2
300 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

914 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now