Define Range Correctly...

m0atz

Board Regular
Joined
Jul 17, 2008
Messages
247
Hi all,

I'm struggling refining my code after getting some great help on previous posts, i have put together the following code.

The values in CRSInfo sheet are shop names in Col S and Col T has their sales total for the week. The code is designed to go through the range in CRSInfo Col S and store the shop name as 'shop'. It then finds this value in the activesheet (i.e. jan, feb, mar etc) and adds these sales to the sales already in the cell which appear in Col F.

The whole thing works great for me, there are a couple of enhancements i wish to make however, first being the line:

Code:
For Each c In Worksheets("CRSInfo").Range("S2:S100")

The shop names start at S2, I've put in S100 as there's never usually more than 100 lines, however the code is looping through all the blank cells in this range and putting a £0 in the first blank cell in my activesheet. How do I refer to just the range of cells in CRSInfo?

Full code =

Code:
Sub crs_add()
 
Dim c As Range
Dim lastrow1, rng1, lastrow2, rng2, shop, salesthiswk
Dim found
Dim ans
    ans = MsgBox("Are You Sure?", vbYesNo + vbExclamation, "Update Sales?")
    If ans = vbNo Then Exit Sub
 
lastrow1 = Worksheets("CRSInfo").Range("S65536").End(xlUp).Row
rng1 = Worksheets("CRSInfo").Range("S1:S1000") 'range of shopnames
 
For Each c In Worksheets("CRSInfo").Range("S2:S100")
 
    shop = c.Value 
    salesthiswk = Worksheets("CRSInfo").Cells(c.Row, 20).Value 
    Set found = Columns("A").Find(what:=shop, lookat:=xlWhole, LookIn:=xlValues)
    If Not found Is Nothing Then found.Select
    ActiveCell.Offset(0, 5).Value = ActiveCell.Offset(0, 5).Value + salesthiswk
 
    Next c
 
End Sub
 

Some videos you may like

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
For Each c In Worksheets("CRSInfo").Range("S2:S" & lastrow1)
 

Watch MrExcel Video

Forum statistics

Threads
1,122,822
Messages
5,598,307
Members
414,224
Latest member
Crazy_FC

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
Top