Solved

offset function

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

762 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

21 Experts available now in Live!

Get 1:1 Help Now