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
 

Some videos you may like

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,095
Office Version
365, 2010
Platform
Windows
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
 

Sayth

Board Regular
Joined
Jun 4, 2010
Messages
212
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.:)
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,658
Messages
5,512,663
Members
408,909
Latest member
Burnrose

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top