A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Excel » General Discussion
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

VBA to find difference in cell locations



 
 
Thread Tools Display Modes
  #1  
Old December 30th, 2009, 08:16 PM posted to microsoft.public.excel.misc
JAmes
external usenet poster
 
Posts: 904
Default VBA to find difference in cell locations

Is it possible to find the difference between cell locations. Such as if I
am on cell E1 and pick another cell H1, I would like to know the difference
between the locations. In this case there are 2 cells between E1 and H1.

I am writting a code that needs to know the difference between the cell
location, I just don't know what the VBA looks like for something like this.

Thanks
  #2  
Old December 30th, 2009, 08:39 PM posted to microsoft.public.excel.misc
Dave Peterson
external usenet poster
 
Posts: 19,791
Default VBA to find difference in cell locations

You don't need VBA for this.

You could use:
=column(h1)-column(e1)
and if you wanted the row difference:
=row(h1)-row(e1)

If you wanted to ignore the end points, you could just subtract 1 from the
formula.
=column(h1)-column(e1)-1

But I'm not sure I do that.

How many columns are between H1 and H2?

In code, you could use:

Dim Cell1 as range
dim Cell2 as range

with worksheets("SomeSheetNameHere")
set cell1 = .range("h1")
set cell2 = .range("e1")
end with

msgbox cell1.column - cell2.column '-1 'if you want.



James wrote:

Is it possible to find the difference between cell locations. Such as if I
am on cell E1 and pick another cell H1, I would like to know the difference
between the locations. In this case there are 2 cells between E1 and H1.

I am writting a code that needs to know the difference between the cell
location, I just don't know what the VBA looks like for something like this.

Thanks


--

Dave Peterson
  #3  
Old December 31st, 2009, 04:00 PM posted to microsoft.public.excel.misc
JAmes
external usenet poster
 
Posts: 904
Default VBA to find difference in cell locations

Dave,
That was to easy, I guess I tried to get to complicated the other day.
Thank you again.

"Dave Peterson" wrote:

You don't need VBA for this.

You could use:
=column(h1)-column(e1)
and if you wanted the row difference:
=row(h1)-row(e1)

If you wanted to ignore the end points, you could just subtract 1 from the
formula.
=column(h1)-column(e1)-1

But I'm not sure I do that.

How many columns are between H1 and H2?

In code, you could use:

Dim Cell1 as range
dim Cell2 as range

with worksheets("SomeSheetNameHere")
set cell1 = .range("h1")
set cell2 = .range("e1")
end with

msgbox cell1.column - cell2.column '-1 'if you want.



James wrote:

Is it possible to find the difference between cell locations. Such as if I
am on cell E1 and pick another cell H1, I would like to know the difference
between the locations. In this case there are 2 cells between E1 and H1.

I am writting a code that needs to know the difference between the cell
location, I just don't know what the VBA looks like for something like this.

Thanks


--

Dave Peterson
.

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 09:31 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.