Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
JZ JZ is offline
external usenet poster
 
Posts: 7
Default Macro to define ranges

I have data as follows in A1:B10.

ACCMLS $31,416.00
ACCPRO $35,430.00
ACCSUP $25,834.00
ACCSUP $27,167.47
ACCSUP $18,969.39
ACCSUP $30,281.00
ACCSUP $25,234.00
ACCSUP $26,114.00
ACCSUP $27,296.00
ACCSUP $25,234.00

I need a macro that names each range of numbers with the name in column A
just to the left of the first instance. For example, B1 should be named
ACCMLS. B2 should be named ACCPRO, and B3:B10 should be named ACCSUP.

Can anyone help?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Macro to define ranges

JZ,

Yes, you could do that, but it is not needed. Better would be to use subtotals, a pivot table, data
filters, or SUMIF, COUNTIF, or SUMPRODUCT formulas.

HTH,
Bernie
MS Excel MVP


"JZ" wrote in message
...
I have data as follows in A1:B10.

ACCMLS $31,416.00
ACCPRO $35,430.00
ACCSUP $25,834.00
ACCSUP $27,167.47
ACCSUP $18,969.39
ACCSUP $30,281.00
ACCSUP $25,234.00
ACCSUP $26,114.00
ACCSUP $27,296.00
ACCSUP $25,234.00

I need a macro that names each range of numbers with the name in column A
just to the left of the first instance. For example, B1 should be named
ACCMLS. B2 should be named ACCPRO, and B3:B10 should be named ACCSUP.

Can anyone help?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Macro to define ranges

Give this a try:


Function NameExist(s As String) As Boolean
Dim n As Name
NameExist = False
If ActiveWorkbook.Names.Count = 0 Then Exit Function
For Each n In ActiveWorkbook.Names
If s = n.Name Then
NameExist = True
Exit Function
End If
Next
End Function


Sub nameLister()
Dim r As Range
Dim v As String
Dim s1 As String
s1 = "=" & ActiveSheet.Name & "!"
n = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To n
Set r = Cells(i, 1)
v = r.Value
If NameExist(v) Then
s = ActiveWorkbook.Names(v).RefersTo & ","
ActiveWorkbook.Names.Add Name:=v, RefersTo:=s & r.Offset(0, 1).Address
Else
ActiveWorkbook.Names.Add Name:=v, RefersTo:=s1 & r.Offset(0,
1).Address
End If
Next
End Sub
--
Gary''s Student - gsnu200826


"JZ" wrote:

I have data as follows in A1:B10.

ACCMLS $31,416.00
ACCPRO $35,430.00
ACCSUP $25,834.00
ACCSUP $27,167.47
ACCSUP $18,969.39
ACCSUP $30,281.00
ACCSUP $25,234.00
ACCSUP $26,114.00
ACCSUP $27,296.00
ACCSUP $25,234.00

I need a macro that names each range of numbers with the name in column A
just to the left of the first instance. For example, B1 should be named
ACCMLS. B2 should be named ACCPRO, and B3:B10 should be named ACCSUP.

Can anyone help?

  #4   Report Post  
Posted to microsoft.public.excel.programming
JZ JZ is offline
external usenet poster
 
Posts: 7
Default Macro to define ranges

Thank you! I think it's close. It seems to be getting stuck in a loop within
the function when I step through it. I also get a Run time error 1004:
Application-defined or object-defined error when I run it, and it is
highlighting the Else (ActiveWorkbook.Names.Add Name:=v, RefersTo:=s1 &
r.Offset(0, 1).Address) when I debug. Any ideas?

"Gary''s Student" wrote:

Give this a try:


Function NameExist(s As String) As Boolean
Dim n As Name
NameExist = False
If ActiveWorkbook.Names.Count = 0 Then Exit Function
For Each n In ActiveWorkbook.Names
If s = n.Name Then
NameExist = True
Exit Function
End If
Next
End Function


Sub nameLister()
Dim r As Range
Dim v As String
Dim s1 As String
s1 = "=" & ActiveSheet.Name & "!"
n = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To n
Set r = Cells(i, 1)
v = r.Value
If NameExist(v) Then
s = ActiveWorkbook.Names(v).RefersTo & ","
ActiveWorkbook.Names.Add Name:=v, RefersTo:=s & r.Offset(0, 1).Address
Else
ActiveWorkbook.Names.Add Name:=v, RefersTo:=s1 & r.Offset(0,
1).Address
End If
Next
End Sub
--
Gary''s Student - gsnu200826


"JZ" wrote:

I have data as follows in A1:B10.

ACCMLS $31,416.00
ACCPRO $35,430.00
ACCSUP $25,834.00
ACCSUP $27,167.47
ACCSUP $18,969.39
ACCSUP $30,281.00
ACCSUP $25,234.00
ACCSUP $26,114.00
ACCSUP $27,296.00
ACCSUP $25,234.00

I need a macro that names each range of numbers with the name in column A
just to the left of the first instance. For example, B1 should be named
ACCMLS. B2 should be named ACCPRO, and B3:B10 should be named ACCSUP.

Can anyone help?

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Macro to define ranges

I am not sure, but we can avoid the RefersTo logic if all the names in column
A are blocked together (sorted rather than random):

Sub RangeRover()
'gsnuxx
Range("B1").Name = Range("A1").Value
Dim n As Long, i As Long
n = Cells(Rows.Count, "A").End(xlUp).Row
Dim r As Range
Set r = Range("B1")
For i = 2 To n
If Cells(i, 1).Value = Cells(i - 1, 1).Value Then
Set r = Union(r, Cells(i, 2))
Else
Set r = Cells(i, 2)
End If
r.Name = Cells(i, 1).Value
Next
End Sub

Try this one out.
--
Gary''s Student - gsnu200826


"JZ" wrote:

Thank you! I think it's close. It seems to be getting stuck in a loop within
the function when I step through it. I also get a Run time error 1004:
Application-defined or object-defined error when I run it, and it is
highlighting the Else (ActiveWorkbook.Names.Add Name:=v, RefersTo:=s1 &
r.Offset(0, 1).Address) when I debug. Any ideas?

"Gary''s Student" wrote:

Give this a try:


Function NameExist(s As String) As Boolean
Dim n As Name
NameExist = False
If ActiveWorkbook.Names.Count = 0 Then Exit Function
For Each n In ActiveWorkbook.Names
If s = n.Name Then
NameExist = True
Exit Function
End If
Next
End Function


Sub nameLister()
Dim r As Range
Dim v As String
Dim s1 As String
s1 = "=" & ActiveSheet.Name & "!"
n = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To n
Set r = Cells(i, 1)
v = r.Value
If NameExist(v) Then
s = ActiveWorkbook.Names(v).RefersTo & ","
ActiveWorkbook.Names.Add Name:=v, RefersTo:=s & r.Offset(0, 1).Address
Else
ActiveWorkbook.Names.Add Name:=v, RefersTo:=s1 & r.Offset(0,
1).Address
End If
Next
End Sub
--
Gary''s Student - gsnu200826


"JZ" wrote:

I have data as follows in A1:B10.

ACCMLS $31,416.00
ACCPRO $35,430.00
ACCSUP $25,834.00
ACCSUP $27,167.47
ACCSUP $18,969.39
ACCSUP $30,281.00
ACCSUP $25,234.00
ACCSUP $26,114.00
ACCSUP $27,296.00
ACCSUP $25,234.00

I need a macro that names each range of numbers with the name in column A
just to the left of the first instance. For example, B1 should be named
ACCMLS. B2 should be named ACCPRO, and B3:B10 should be named ACCSUP.

Can anyone help?



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
What does it mean define ranges in Excell for import Ron New Users to Excel 2 September 7th 06 05:38 PM
Using Cells Method to define Ranges [email protected] Excel Programming 3 February 23rd 06 04:06 PM
Automatically define series of named ranges Jon Tillman Excel Programming 7 June 18th 04 07:39 AM
Define name in macro Walleye Hunter Excel Programming 1 March 4th 04 02:09 AM
Macro to Define Range Name Phil Excel Programming 6 October 23rd 03 04:00 PM


All times are GMT +1. The time now is 07:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"