Formula to offset based on a change

HeyMike

New Member
Joined
Mar 1, 2012
Messages
22
Hi,

I'm looking for a formula that will take the cells used as a heading and place them cell into columns with the corresponding rows under the heading.

I'm thinking that the formula will have an offset that counts the blank cells in column A then place the text from the heading cells (by heading cells I mean the ones that say "Type A" and "Code X") next to the rows with the products.

Screen_Hunter_02_May_24_12_09.jpg


Maybe a VBA script? What do you think?

Thanks,
Mike
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi Mike

It might be possible to do it by formula but I can't figure it out. So I just put together some vb code based on your screen shot.

Code:
Sub Code()
Dim myrange As Range
Dim Lastrow As Long
Dim Cell As Object
Dim string1, string2 As String
Lastrow = Cells(Rows.Count, 3).End(xlUp).Row
Set myrange = Range(Cells(3, 3), Cells(Lastrow, 3))
For Each Cell In myrange
If Cell.Value <> "" Then
string1 = Cells(Cell.Row, Cell.Column - 2).End(xlUp).Value
string2 = Cells(Cell.Row, Cell.Column - 1).End(xlUp).Value
Cells(Cell.Row, Cell.Column + 1) = string1
Cells(Cell.Row, Cell.Column + 2) = string2
End If
Next Cell
End Sub

Hope it helps.
 
Upvote 0
Formula version, enter the formula in D4, then use the fill handle to copy the formula to E4 and sown as required.

=IF($C4="","",LOOKUP("ZZZZZZ",A$3:A3))

VBA version

Code:
Sub fill()
With Intersect(Range("A:B"), ActiveSheet.UsedRange).Offset(3, 3)
    .Formula = "=IF($C4="""","""",LOOKUP(""ZZZZZZ"",A$3:A3))"
    .Value = .Value
End With
End Sub
 
Upvote 0
1) select the range of interest (in your example, a3:c20)
2) edit | go to | special | blanks
3) in a4 enter = a3. hit control + enter
4) edit | copy. edit | paste special - values
 
Upvote 0
DOSNOX,

Your VBA script worked for placing the values from column A but not for column B. It just repeated the top row in column B.

Thanks!



Hi Mike

It might be possible to do it by formula but I can't figure it out. So I just put together some vb code based on your screen shot.

Code:
Sub Code()
Dim myrange As Range
Dim Lastrow As Long
Dim Cell As Object
Dim string1, string2 As String
Lastrow = Cells(Rows.Count, 3).End(xlUp).Row
Set myrange = Range(Cells(3, 3), Cells(Lastrow, 3))
For Each Cell In myrange
If Cell.Value <> "" Then
string1 = Cells(Cell.Row, Cell.Column - 2).End(xlUp).Value
string2 = Cells(Cell.Row, Cell.Column - 1).End(xlUp).Value
Cells(Cell.Row, Cell.Column + 1) = string1
Cells(Cell.Row, Cell.Column + 2) = string2
End If
Next Cell
End Sub

Hope it helps.
 
Upvote 0
Paddy. This was awesome. Quick and easy.

1) select the range of interest (in your example, a3:c20)
2) edit | go to | special | blanks
3) in a4 enter = a3. hit control + enter
4) edit | copy. edit | paste special - values
 
Upvote 0
Hey Mike

Sorry about that tested it on my spreadsheet and it works not sure why it didn't work for you mate. Glad your problem got solved.
 
Upvote 0

Forum statistics

Threads
1,222,749
Messages
6,167,967
Members
452,158
Latest member
MattyM

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top