Refer to Named Ranges

Sayth

Board Regular
Joined
Jun 4, 2010
Messages
212
I am having a little trouble referring to named ranges.

I had what I feel is a simple problem. I have a sheet("Sheet1") which has a lot of data to make things simpler later column ranges were defined. Two of these are number(represents a class number) and name(its a name).

For each class number I am attempting to add a sheet for that class number and then copy their names to that sheet. So I am trying to do it like this but not any success. I can't seem to refer to i in number.

Code:
i = o
For each i in Worksheet("Sheet1"); 'number'
' find number match add sheet and increment i 1 until no matching i
If i in number =+1 then
Sheets.Add
Sheets.Name = "i"
Next i
End If
If i = Number Then Names Copy
    Sheets("i").Select
    ActiveSheet.Paste
Next i
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
I am having a little trouble referring to named ranges.

I had what I feel is a simple problem. I have a sheet("Sheet1") which has a lot of data to make things simpler later column ranges were defined. Two of these are number(represents a class number) and name(its a name).

For each class number I am attempting to add a sheet for that class number and then copy their names to that sheet. So I am trying to do it like this but not any success. I can't seem to refer to i in number.

Code:
i = o
For each i in Worksheet("Sheet1"); 'number'
' find number match add sheet and increment i 1 until no matching i
If i in number =+1 then
Sheets.Add
Sheets.Name = "i"
Next i
End If
If i = Number Then Names Copy
    Sheets("i").Select
    ActiveSheet.Paste
Next i
End Sub
Not sure I fully understand what you want, but here is some code that you may be able to tweak. I've assumed you will run the code while a sheet with all the class numbers and names on it. I've assumed that the Class Number is in column A and Name in column B with headers for number in cell A1 and name in B1. The code adds worksheets for each number and separates the names by class number to the respective added sheets.
Code:
Sub Test1()
Dim numRng As Range, r As Long, LR As Long
Dim sSht As Worksheet, vArr As Variant
Set sSht = ActiveSheet
LR = sSht.Range("A" & Rows.Count).End(xlUp).Row
Set numRng = sSht.Range("A2", "A" & LR)
'capture current sort order for replacement later
vArr = numRng.CurrentRegion.Value
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'sort by Class #
With numRng.CurrentRegion
    .Sort key1:=numRng.Cells(1, 1), order1:=xlDescending, Header:=xlYes
End With
For i = 1 To numRng.Rows.Count
    If WorksheetFunction.CountIf(Range(Cells(2, 1), Cells(2, 1).Offset(i - 1, 0)), Cells(2, 1).Offset(i - 1, 0).Value) = 1 Then
        Sheets.Add after:=sSht
        ActiveSheet.Name = sSht.Cells(2, 1).Offset(i - 1, 0).Value
        Range("A1:B1").Value = sSht.Range("A1:B1").Value
        For Each cel In numRng
            If CStr(cel.Value) = ActiveSheet.Name Then
               ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Resize(1, 2).Value _
               = sSht.Range(cel, cel.Offset(0, 1)).Value
            End If
        Next cel
        sSht.Select
    End If
 Next i
'Insert headers in each new sheet
     
'put original order back in place
numRng.CurrentRegion = vArr
Application.Calculation = xlCalculationAutomatic

End Sub
 
Upvote 0
Yeah thats basically what I was wanting. I went down the path of creating named ranges as I thought it would make it easier.

Code:
Sudo code
start i at 1 and add 1 to i on each loop
for all values in defined named range = i copy names to sheet "i"
i =+1
next i

See the catch is that for each class, that is what the number represents I want calculated data((this data exists on sheet1) for example Average Median, Range(High to low score).. etc) copied to the respective class sheets.

Gonna be a lotta work it seems.:)
 
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