Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
SK SK is offline
external usenet poster
 
Posts: 1
Default Writing VBA function with loop and condition

Hi ,

I have a noob question, and I guess is not that hard thing, just my head
is not functioning right now properly and I cant figure it out.
I have 2 columns of data.
1st column has cells with values and empty cells
second column has multiple rows corresponding to each value in the
column in the right.
Something like that:

A B

1 US region1
2 region2
3 region3
4 region4
5 ..(other regions)
6
7 EU region1
8 region2
9 region3

(All this is a pivot table)


My problem is to rite a formula, that says, based on the value in column
"B", offset to the left and go UP as many times as needed, until you
reach non-blank cell, and return the value of this nonblank cell...
so if i say take "B3" which will be region3, offset to left will be
"A3", then go up as many times as needed, until encounter nonblank cell,
in this case will be cell A1, and return the value of A1

other example: I take B8, which is region2, offset to left, to A8 , go
as many times as needed, until encounter first nonblank cell, which in
this case will be A7 "EU", and return the value of A7, and do not go any
further UP

need that to incorporate in larger getpivotdata formula

Nested if would be one solution, but number of regions is every time
different and might be quite a long list. So my idea was to offset to
the right and loop until find a nonblank cell... But I am still learning
VBA, so I need a little help with writing this function procedure.

Any ideas?

Thank you
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default Writing VBA function with loop and condition

Sub ColAValues()
Dim selectcell As Range
On Error Resume Next
Set selectcell = Application.InputBox("Select a cell in Column B", _
Default:=ActiveCell.Address, Type:=8)
If selectcell.Address = "" Then Exit Sub
If selectcell.Offset(, -1) < "" Then
foundit = selectcell.Offset(, -1).Value
Else
foundit = selectcell.Offset(, -1).End(xlUp).Value
End If
MsgBox (foundit)
End Sub

HTH,
--
Data Hog


"SK" wrote:

Hi ,

I have a noob question, and I guess is not that hard thing, just my head
is not functioning right now properly and I cant figure it out.
I have 2 columns of data.
1st column has cells with values and empty cells
second column has multiple rows corresponding to each value in the
column in the right.
Something like that:

A B

1 US region1
2 region2
3 region3
4 region4
5 ..(other regions)
6
7 EU region1
8 region2
9 region3

(All this is a pivot table)


My problem is to rite a formula, that says, based on the value in column
"B", offset to the left and go UP as many times as needed, until you
reach non-blank cell, and return the value of this nonblank cell...
so if i say take "B3" which will be region3, offset to left will be
"A3", then go up as many times as needed, until encounter nonblank cell,
in this case will be cell A1, and return the value of A1

other example: I take B8, which is region2, offset to left, to A8 , go
as many times as needed, until encounter first nonblank cell, which in
this case will be A7 "EU", and return the value of A7, and do not go any
further UP

need that to incorporate in larger getpivotdata formula

Nested if would be one solution, but number of regions is every time
different and might be quite a long list. So my idea was to offset to
the right and loop until find a nonblank cell... But I am still learning
VBA, so I need a little help with writing this function procedure.

Any ideas?

Thank you
.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default Writing VBA function with loop and condition

Here is the code for a function.

Function FindHeader(rge As Range)
If rge.Offset(, -1).Value < "" Then
FindHeader = rge.Offset(, -1)
Else
FindHeader = rge.Offset(, -1).End(xlUp)
End If
End Function

HTH,
--
Data Hog




I have a noob question, and I guess is not that hard thing, just my head
is not functioning right now properly and I cant figure it out.
I have 2 columns of data.
1st column has cells with values and empty cells
second column has multiple rows corresponding to each value in the
column in the right.
Something like that:

A B

1 US region1
2 region2
3 region3
4 region4
5 ..(other regions)
6
7 EU region1
8 region2
9 region3

(All this is a pivot table)


My problem is to rite a formula, that says, based on the value in column
"B", offset to the left and go UP as many times as needed, until you
reach non-blank cell, and return the value of this nonblank cell...
so if i say take "B3" which will be region3, offset to left will be
"A3", then go up as many times as needed, until encounter nonblank cell,
in this case will be cell A1, and return the value of A1

other example: I take B8, which is region2, offset to left, to A8 , go
as many times as needed, until encounter first nonblank cell, which in
this case will be A7 "EU", and return the value of A7, and do not go any
further UP

need that to incorporate in larger getpivotdata formula

Nested if would be one solution, but number of regions is every time
different and might be quite a long list. So my idea was to offset to
the right and loop until find a nonblank cell... But I am still learning
VBA, so I need a little help with writing this function procedure.

Any ideas?

Thank you
.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
returning back to loop check condition without completing the loop ashish128 Excel Programming 13 April 3rd 08 12:53 PM
Need help writing do loop Aaron Excel Programming 0 August 24th 06 02:47 PM
Add a second condition to loop Casey[_112_] Excel Programming 5 July 6th 06 11:59 PM
Using For loop to condition bundyloco[_6_] Excel Programming 1 August 19th 05 07:25 PM
help writing loop robhargreaves[_7_] Excel Programming 1 July 31st 05 10:18 PM


All times are GMT +1. The time now is 07:58 PM.

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

About Us

"It's about Microsoft Excel"