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
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
you could use, instead of your highlighted text, a direct reference to the cell: cells(selection.row,2)
 
Upvote 0
Try

Selection.Offset(0, r1 + Selection.Offset(0,-5).Value).Select


Note, it's almost never necessary to select ranges to manipulate them.

You could change this
Rich (BB code):
    Range("G6").Select
    Do
        If ActiveCell.Value = "Projected Buy" Then
            Selection.Offset(0, r1 + Selection.Offset(0,-5).Value).Select
                With Selection.Borders(xlEdgeLeft)
                    .LineStyle = xlContinuous
                    .Weight = xlThick
                    .ColorIndex = xlAutomatic
                End With
To
Rich (BB code):
If Range("G6").Value = "Projected Buy" Then
    With Range("G6").Offset(0, r1 + Range("G6").Offset(0, -5).Value)
            With .Borders(xlEdgeLeft)
                .LineStyle = xlContinuous
                .Weight = xlThick
                .ColorIndex = xlAutomatic
            End With
    End With
End If


Hope that helps.
 
Upvote 0
thanks both for your time both the offsets work now thanks

jonmo1,

My code goes down column G (approx 10000+ rows) looking for the word project buy and highlights the cell opposite it. Im not sure how to adjust your code though to fit in my loop??
In most of my other code I try not to select anything but I wasnt sure how to do it in this case to get the out come in needed

:oops:
 
Upvote 0
Try something like

Code:
For i = 6 to LastRow
    If Range("G" & i).Value = "Projected Buy" Then
        With Range("G" & i).Offset(0, r1 + Range("G" & i).Offset(0, -5).Value)
                With .Borders(xlEdgeLeft)
                    .LineStyle = xlContinuous
                    .Weight = xlThick
                    .ColorIndex = xlAutomatic
                End With
        End With
    End If
Next i
 
Upvote 0
Instead of looping, use the Excel MATCH function to identify the line number

You can use this function in VBA by sticking worksheetfunction in front of it, i.e. something like lineNumber = worksheetfunction.match("projected buy",activesheet.columns(7))

then you can refer directly to cells(lineNumber,[your column])
 
Upvote 0
brilliant thanks for you help on this! I've 1 more bit of this code that you may be able to help me with. It does a similar loop but selects alot of cells to put formulas in. Could that be incorperated into your code??

Code:
    Range("G6").Select
    Do
        If ActiveCell.Value = "Open Orders" Then
            Selection.Offset(0, r1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,2,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,2,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,3,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,3,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,4,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,4,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,5,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,5,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,6,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,6,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,7,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,7,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,8,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,8,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,9,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,9,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,10,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,10,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,11,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,11,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,12,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,12,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,13,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,13,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,14,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,14,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,15,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,15,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,16,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,16,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,17,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,17,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,18,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,18,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,19,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,19,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,20,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,20,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,21,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,21,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,22,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,22,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,23,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,23,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,24,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,24,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,25,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,25,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,26,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,26,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,27,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,27,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,28,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,28,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,29,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,29,FALSE))"
            Selection.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = "=IF(ISERROR(VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,30,FALSE)),0,VLOOKUP(R[-1]C2,'latest oo'!R2C1:R257C10,30,FALSE))"
            Selection.Offset(1, r2 - 28).Select
        Else
            Selection.Offset(1, 0).Select
        End If
    Loop Until ActiveCell = ""
 
Upvote 0
Do you actually need the formulas in the cells, or could we just put the resulting values in the cells?
 
Upvote 0
I need the formulas in the cells. What my code basically is doing is updating a spread sheet on a monthly basis (moving everything along to the next column) but the data where the formulas is looked up against can change daily.
 
Upvote 0
sounds like you should use Index and Match rather than Vlookup. Its more flexible in circumstances like this
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,492
Members
448,967
Latest member
visheshkotha

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