[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

offset function

Posted on 2013-11-19
2
Medium Priority
?
368 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 50

Assisted Solution

by:Martin Liss
Martin Liss earned 1000 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 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

872 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