Returning an offset value from a range

snowbounduk

New Member
Joined
Apr 14, 2011
Messages
21
I have the following code:

HTML:
Sub AAddEmailAddressToEachProjectSheet()
    myCriteria = Sheets("Recd Plans").Range("C2:C168")
    For Each Crit In myCriteria
        With Sheets(Crit)
            .Range("A1").Value = "Project"
            .Range("A2").Value = "Summary"
        End With
    Next Crit
 
End Sub

Rather than enter "Project" in A1, I need the code to return the appropriate value from column K.

I have tried the follwoing code with various offset statements against
HTML:
.Range("A1").Value = Crit
with no success.

HTML:
Sub AAddEmailAddressToEachProjectSheet()
    myCriteria = Sheets("Recd Plans").Range("C2:C168")
    For Each Crit In myCriteria
        With Sheets(Crit)
            .Range("A1").Value = Crit
            .Range("A2").Value = "Summary"
        End With
    Next Crit
 
End Sub

Any help appreciated.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Thanks for that.

I changed the offset to 8 and It only works if a cell in column C on Recd Plans sheet is selected and then it uses the same value, (K2) across all sheets rather than using K3 for C3 etc.

Any ideas?
 
Upvote 0
I've used
HTML:
            .Range("A1").Value = Range("C2").Offset(0, 8).Value

This still only gives one value for all sheets, the value in K2. ANd will only work if I run the macro from the Recd Plans sheet.

Any other pearls of wisdom?
 
Upvote 0
Any other pearls of wisdom?

Rather snarky today, are we?

Are the values in Column K on the 'Recd Plans' sheet, or each sheet that you're looping.

If the former:
.Range("A1").value=crit.offset(0,8).value
 
Upvote 0
My apologies, it wasn't meant to be snarky! It was meant to be funny. Oops.

The values in column K are in the Recd Plans sheet.

I have tried the Crit.offset(0,8). value and get a Run-time error '424':
Object required.

Any ideas?
 
Upvote 0
why dont you just select the appropriate cell in column A in your code and the offset value will work every time ?
 
Upvote 0
Ok, here's the problem. The variable 'MyCriteria' was being treated as an array, an array of the values in the range C2:C168, as opposed to a range itself. I think changing the line
Code:
    myCriteria = Sheets("Recd Plans").Range("C2:C168")

to

Code:
    Set myCriteria = Sheets("Recd Plans").Range("C2:C168")

fixes the problem. But for good measure use option explicit and define all of the variables. Here's your code adapted for my setup (change ranges and sheetnames).

Code:
Option Explicit

Sub AAddEmailAddressToEachProjectSheet()
    Dim mycriteria As Range
    Dim crit As Range
    Set mycriteria = Sheets(1).Range("C2:C6")
    For Each crit In mycriteria.Cells
        With Sheets(crit.Value)
            .Range("A1").Value = crit.Offset(, 8).Value
            .Range("A2").Value = "Summary"
        End With
    Next crit
 
End Sub
 
Upvote 0
Many thanks for that, a couple of tweaks and it works a treat!

HTML:
Option Explicit
Sub AAddEmailAddressToEachProjectSheet()
    
    Application.ScreenUpdating = False
    Dim myCriteria As Range
    Dim Crit As Range
    
    Set myCriteria = Sheets("Recd Plans").Range("C2:C168")
    Sheets("Recd Plans").Select
    Range("C2").Select
    For Each Crit In myCriteria.Cells
        With Sheets(Crit.Value)
            .Range("A1").Value = Crit.Offset(, 8).Value
            .Range("A2").Value = "Summary"
        End With
    Next Crit
    Sheets("Summary").Select
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,896
Messages
6,181,622
Members
453,058
Latest member
rmd0725

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