setting an offest based on a cell value

Perksy_no1

Well-known Member
Joined
Oct 27, 2011
Messages
598
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I've got a macro which loops down column G looking for the word "projected buy". I then offset using a variable (r1) I then need to off set it some more based on the value in the corresponding row in column B, format the cell then carrying on down column G looking for the next "projected buy"

I can most of this code working but I cant get it to offset buy the value in column B. I've highlighted the part of the code im struggling with

any help would be much appreciated, Thanks in advance Mark

Rich (BB code):
Dim r1 As Long
Dim r2 As Long
Dim LastRow As Long
r1 = Sheets("Sheet1").Range("B1").Value
r2 = Sheets("Sheet1").Range("B2").Value
LastRow = Cells(Rows.Count, "G").End(xlUp).Row
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
    Range("G6").Select
    Do
        If ActiveCell.Value = "Projected Buy" Then
            Selection.Offset(0, r1 + "RC[-5]").Select
                With Selection.Borders(xlEdgeLeft)
                    .LineStyle = xlContinuous
                    .Weight = xlThick
                    .ColorIndex = xlAutomatic
                End With
                With Selection.Borders(xlEdgeTop)
                    .LineStyle = xlContinuous
                    .Weight = xlThick
                    .ColorIndex = xlAutomatic
                End With
                With Selection.Borders(xlEdgeBottom)
                    .LineStyle = xlContinuous
                    .Weight = xlThick
                    .ColorIndex = xlAutomatic
                End With
                With Selection.Borders(xlEdgeRight)
                    .LineStyle = xlContinuous
                    .Weight = xlThick
                    .ColorIndex = xlAutomatic
                End With
                With Selection.Interior
                    .ColorIndex = 6
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                End With
            Selection.Offset(1, r2).Select
        Else
            Selection.Offset(1, 0).Select
        End If
    Loop Until ActiveCell = ""
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
 
sounds like you should use Index and Match rather than Vlookup. Its more flexible in circumstances like this

That would be my choice but its an inhertiated spread sheet which a lot of people use. unfortunatly the cell where you would normaly match isnt the same on all the sheets it looks up from. as this isnt the only formula on the sheet :(
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try

Code:
Sub test()
Dim r1 As Long, i As Long, x As Long, LastRow As Long
r1 = Sheets("Sheet1").Range("B1").Value
LastRow = Cells(Rows.Count, "G").End(xlUp).Row
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For i = 6 To LastRow
    With Range("G" & i)
        If .Value = "Open Orders" Then
            For x = 0 To 28
                .Offset(0, r1 + x).FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10," & x + 2 & ",FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10," & x + 2 & ",FALSE))"
            Next x
        End If
    End With
Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,986
Members
449,058
Latest member
oculus

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