Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 29
Default Macro to paste x number of times

Hi! Can you help me with a macro to copy and paste a cell x number of times
dependent on a number that is entered into a cell.

Ex: If A2 = 2 then cell B2 is copied and pasted into the next 2 empty cells
in column C. But then if A2 = 5, B2 is copied and pasted into the next 5
empty cells in column C.

As always, any help is much appreciated! Oh yeah, I'm working in Excel 2003.

Thanks,
Holly
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,203
Default Macro to paste x number of times

There are several ways to code this up. I'll give two ways. First is fairly
clear in what it's doing and probably "good enough" for relatively small
values in A2.

The second may not be so clear, but can be very effective/fast, especially
if the values in A2 are going to be very large.

To get them into your workbook: open it up, press [Alt]+[F11] to enter the
VB Editor. In there, choose Insert -- Module. Copy and paste the code
below into the module presented to you. Close the VB Editor and test them to
see which you like the best.

Sub PasteItWithLoop()
Dim pCount As Integer
Dim LC As Integer

pCount = ActiveSheet.Range("A2")
If pCount < 1 Then
Exit Sub
End If
'the clear way to do it
Application.ScreenUpdating = False
For LC = 1 To pCount
ActiveSheet.Range("B" & Rows.Count) _
.End(xlUp).Offset(1, 0) = ActiveSheet.Range("B2")
Next
End Sub

Sub PasteUsingRange()
Dim pCount As Integer
Dim pRange As Range
Dim pRangeAddress As String

pCount = ActiveSheet.Range("A2")
If pCount < 1 Then
Exit Sub
End If

Application.ScreenUpdating = False
'another way, effective for large counts in A2
pRangeAddress = _
ActiveSheet.Range("B" & Rows.Count) _
.End(xlUp).Offset(1, 0).Address
pRangeAddress = pRangeAddress & ":" & _
Range(pRangeAddress).Offset(pCount, 0).Address
Set pRange = ActiveSheet.Range(pRangeAddress)
pRange.Value = ActiveSheet.Range("B2").Value
Set pRange = Nothing
End Sub

"hnyb1" wrote:

Hi! Can you help me with a macro to copy and paste a cell x number of times
dependent on a number that is entered into a cell.

Ex: If A2 = 2 then cell B2 is copied and pasted into the next 2 empty cells
in column C. But then if A2 = 5, B2 is copied and pasted into the next 5
empty cells in column C.

As always, any help is much appreciated! Oh yeah, I'm working in Excel 2003.

Thanks,
Holly

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 29
Default Macro to paste x number of times

Thanks so much! I used the first "straight forward" method, as there
shouldn't be any more than 4 repetitions. Thanks again!

"JLatham" wrote:

There are several ways to code this up. I'll give two ways. First is fairly
clear in what it's doing and probably "good enough" for relatively small
values in A2.

The second may not be so clear, but can be very effective/fast, especially
if the values in A2 are going to be very large.

To get them into your workbook: open it up, press [Alt]+[F11] to enter the
VB Editor. In there, choose Insert -- Module. Copy and paste the code
below into the module presented to you. Close the VB Editor and test them to
see which you like the best.

Sub PasteItWithLoop()
Dim pCount As Integer
Dim LC As Integer

pCount = ActiveSheet.Range("A2")
If pCount < 1 Then
Exit Sub
End If
'the clear way to do it
Application.ScreenUpdating = False
For LC = 1 To pCount
ActiveSheet.Range("B" & Rows.Count) _
.End(xlUp).Offset(1, 0) = ActiveSheet.Range("B2")
Next
End Sub

Sub PasteUsingRange()
Dim pCount As Integer
Dim pRange As Range
Dim pRangeAddress As String

pCount = ActiveSheet.Range("A2")
If pCount < 1 Then
Exit Sub
End If

Application.ScreenUpdating = False
'another way, effective for large counts in A2
pRangeAddress = _
ActiveSheet.Range("B" & Rows.Count) _
.End(xlUp).Offset(1, 0).Address
pRangeAddress = pRangeAddress & ":" & _
Range(pRangeAddress).Offset(pCount, 0).Address
Set pRange = ActiveSheet.Range(pRangeAddress)
pRange.Value = ActiveSheet.Range("B2").Value
Set pRange = Nothing
End Sub

"hnyb1" wrote:

Hi! Can you help me with a macro to copy and paste a cell x number of times
dependent on a number that is entered into a cell.

Ex: If A2 = 2 then cell B2 is copied and pasted into the next 2 empty cells
in column C. But then if A2 = 5, B2 is copied and pasted into the next 5
empty cells in column C.

As always, any help is much appreciated! Oh yeah, I'm working in Excel 2003.

Thanks,
Holly

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 29
Default Macro to paste x number of times

I'm sorry to bug again, but is there any reason why this macro won't work
when i set up a Private Sub to execute when a cell value changes? I've tried
a couple of different codes, but here's the latest. Nothing happens when I
update D11, but if I just run the sub it works perfectly?

Private Sub Worksheet_Calculate()
Static OldValD11 As Variant

If Me.Range("D11").Value = OldValD11 Then
'do nothing, it didn't change
Else
OldValD11 = Me.Range("d11").Value
'then go off and do what you want
Application.EnableEvents = False
Application.Run "PasteItWithLoop"
Application.EnableEvents = True
End If
End Sub

Thanks,
Holly


"JLatham" wrote:

There are several ways to code this up. I'll give two ways. First is fairly
clear in what it's doing and probably "good enough" for relatively small
values in A2.

The second may not be so clear, but can be very effective/fast, especially
if the values in A2 are going to be very large.

To get them into your workbook: open it up, press [Alt]+[F11] to enter the
VB Editor. In there, choose Insert -- Module. Copy and paste the code
below into the module presented to you. Close the VB Editor and test them to
see which you like the best.

Sub PasteItWithLoop()
Dim pCount As Integer
Dim LC As Integer

pCount = ActiveSheet.Range("A2")
If pCount < 1 Then
Exit Sub
End If
'the clear way to do it
Application.ScreenUpdating = False
For LC = 1 To pCount
ActiveSheet.Range("B" & Rows.Count) _
.End(xlUp).Offset(1, 0) = ActiveSheet.Range("B2")
Next
End Sub

Sub PasteUsingRange()
Dim pCount As Integer
Dim pRange As Range
Dim pRangeAddress As String

pCount = ActiveSheet.Range("A2")
If pCount < 1 Then
Exit Sub
End If

Application.ScreenUpdating = False
'another way, effective for large counts in A2
pRangeAddress = _
ActiveSheet.Range("B" & Rows.Count) _
.End(xlUp).Offset(1, 0).Address
pRangeAddress = pRangeAddress & ":" & _
Range(pRangeAddress).Offset(pCount, 0).Address
Set pRange = ActiveSheet.Range(pRangeAddress)
pRange.Value = ActiveSheet.Range("B2").Value
Set pRange = Nothing
End Sub

"hnyb1" wrote:

Hi! Can you help me with a macro to copy and paste a cell x number of times
dependent on a number that is entered into a cell.

Ex: If A2 = 2 then cell B2 is copied and pasted into the next 2 empty cells
in column C. But then if A2 = 5, B2 is copied and pasted into the next 5
empty cells in column C.

As always, any help is much appreciated! Oh yeah, I'm working in Excel 2003.

Thanks,
Holly

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 29
Default Macro to paste x number of times

O.K. now I'm just being a pain. I figured it out... change it from

Private Sub Worksheet_Calculate()

to

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Learn something new everyday.

"JLatham" wrote:

There are several ways to code this up. I'll give two ways. First is fairly
clear in what it's doing and probably "good enough" for relatively small
values in A2.

The second may not be so clear, but can be very effective/fast, especially
if the values in A2 are going to be very large.

To get them into your workbook: open it up, press [Alt]+[F11] to enter the
VB Editor. In there, choose Insert -- Module. Copy and paste the code
below into the module presented to you. Close the VB Editor and test them to
see which you like the best.

Sub PasteItWithLoop()
Dim pCount As Integer
Dim LC As Integer

pCount = ActiveSheet.Range("A2")
If pCount < 1 Then
Exit Sub
End If
'the clear way to do it
Application.ScreenUpdating = False
For LC = 1 To pCount
ActiveSheet.Range("B" & Rows.Count) _
.End(xlUp).Offset(1, 0) = ActiveSheet.Range("B2")
Next
End Sub

Sub PasteUsingRange()
Dim pCount As Integer
Dim pRange As Range
Dim pRangeAddress As String

pCount = ActiveSheet.Range("A2")
If pCount < 1 Then
Exit Sub
End If

Application.ScreenUpdating = False
'another way, effective for large counts in A2
pRangeAddress = _
ActiveSheet.Range("B" & Rows.Count) _
.End(xlUp).Offset(1, 0).Address
pRangeAddress = pRangeAddress & ":" & _
Range(pRangeAddress).Offset(pCount, 0).Address
Set pRange = ActiveSheet.Range(pRangeAddress)
pRange.Value = ActiveSheet.Range("B2").Value
Set pRange = Nothing
End Sub

"hnyb1" wrote:

Hi! Can you help me with a macro to copy and paste a cell x number of times
dependent on a number that is entered into a cell.

Ex: If A2 = 2 then cell B2 is copied and pasted into the next 2 empty cells
in column C. But then if A2 = 5, B2 is copied and pasted into the next 5
empty cells in column C.

As always, any help is much appreciated! Oh yeah, I'm working in Excel 2003.

Thanks,
Holly

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
copy and paste 2900 times macro DLB Excel Discussion (Misc queries) 2 May 15th 09 07:20 PM
Copy & paste block of text number of times equal to value in cell azoll Excel Worksheet Functions 1 July 29th 08 06:03 AM
Macro - copy a range and paste it in a new sheet 12 times Eva Excel Worksheet Functions 0 September 26th 07 07:20 PM
Loop Macro a variable number of times thesaxonuk Excel Discussion (Misc queries) 11 October 31st 06 06:05 PM
Running a Macro a number of times JWF Excel Discussion (Misc queries) 2 March 14th 06 11:10 AM


All times are GMT +1. The time now is 06:04 PM.

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

About Us

"It's about Microsoft Excel"