Simple loop based on Defined Name

mwf05

New Member
Joined
Jun 19, 2006
Messages
7
I am having a problem with a simple loop that I want to run x number of times based upon a defined name in a worksheet. My defined name is Clients=counta($A:$A)-9

I have a macro that I am trying to execute that will copy a range down a number of times based upon the value found in the defined name of Clients, as shown above. However, nothing I have tried works. Here is the macro/loop I have setup.

Sub UpdateTable()
'
' UpdateTable Macro
'
Dim l As Integer
Dim m As Integer
'
Set m = Worksheets("Summary").Range("Clients")
Range("B6").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
For l = 1 To m
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Next l
End Sub

Currently, Clients is equal to 7. When I replace m with 7 everything works fine. However, since the number of clients will vary, I want to be able to import the current client count which is the value in the defined name.

My experience level is, well moderate at best. Looked all over for simple answer and have not been able to find it
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I think this should do the trick...

Code:
Sub UpdateTable()
'
' UpdateTable Macro

[B6].Resize(, [B6].End(xlToRight)).Copy
ActiveCell.Offset(1).Resize([Clients]).PasteSpecial xlPasteFormulas
End Sub
 
Upvote 0
You need to Dim "m" as a Range.
The code below should also work
Code:
[COLOR="Navy"]Sub[/COLOR] MG08May50
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] m [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] m = Worksheets("Summary").Range("Clients")
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("B6"), Range("B6").End(xlToRight))
Rng.Resize(m, Rng.Columns.Count) = Rng.Value
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
I think this should do the trick...

Code:
Sub UpdateTable()
'
' UpdateTable Macro

[B6].Resize(, [B6].End(xlToRight)).Copy
ActiveCell.Offset(1).Resize([Clients]).PasteSpecial xlPasteFormulas
End Sub

Tried the above, however, I get a Run-time error of '1004': Application-defined or object-defined error.
 
Upvote 0
As with the other solution provided, I get the same error.
Run-time error of '1004': Application-defined or object-defined error.
 
Upvote 0
I think this should do the trick...

Code:
Sub UpdateTable()
'
' UpdateTable Macro

[B6].Resize(, [B6].End(xlToRight)).Copy
ActiveCell.Offset(1).Resize([Clients]).PasteSpecial xlPasteFormulas
End Sub


Tried the above, however, I get a Run-time error of '1004': Application-defined or object-defined error.

Where does the code bomb? What version of Excel are you using?
 
Upvote 0
Try this...

Code:
Sub UpdateTable()
Dim LC As Long
    LC = Cells(6, Columns.Count).End(xlToLeft).Offset(, -1).Column

    [B6].Resize(, LC).Copy ActiveCell.Offset(1).Resize([Clients])
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,790
Messages
6,121,608
Members
449,038
Latest member
apwr

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