Copy and paste based on criteria

mfarr76

New Member
Joined
Jan 3, 2014
Messages
36
Hello,

In column A (shown below) I have a list of dates and in column B I have a number which can range from 0 - 9. Based upon the number in B1, I want to use vba to copy cell A1 and paste in C1 (and C2 if B1 has something larger than a 1). I would like to do this for a hundred dates.

The table below shows an example.

Column A contains the dates and column B has a number from 0 - 9. Column C, I want to paste the number of dates based upon the value in column B.

Any help would be greatly appreciated. I am having to do this manually and it can be very time consuming when you have a hundred of these dates to do.

ABC
12/1/201532/1/2015
24/1/201512/1/2015
36/1/201502/1/2015
44/1/2015

<tbody>
</tbody>
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
The fix I gave you should have prevented the infinate loop, so I'm going to assume you didn't put the fix in the right location. Here is the code with the fix.
Code:
Sub myMacro()
    lastRowA = Range("A" & Rows.Count).End(xlup).Row
    lastRowL = Range("L" & Rows.Count).End(xlup).Row
    If lastRowL > 1 Then  'If L1 is the only row in column L with data, it won't clear contents.
         Range("L2:L" & lastRowL).ClearContents
     End If
    i = 2
    iL = 2
    Do Until i > lastRowA
        pasteQuantity = Range("B" & i).Value
        If pasteQuantity > 0 Then
            pasteValue = Range("A" & i).Value
            c = 1
            Do Until c > pasteQuantity
                Range("L" & iL).Value = pasteValue
                iL = iL + 1
                c = c + 1
            Loop
         End If
        i = i + 1
    Loop
End Sub
 
Last edited:
Upvote 0
I actually had it in the same place but I figured out the infinite loop was being created on the back end. Shown below is an example.

I have several dates that do not have values in B, is there a way to end the loop after say 5 cells are less than 1? Or if there is a better way to do it?

Excel 2010
AB
1001/1/20232
1012/1/20232
1023/1/20232
1034/1/20232
1045/1/20232
1056/1/2023
1067/1/2023
1078/1/2023
1089/1/2023
10910/1/2023
11011/1/2023
MAIN
Worksheet Formulas
CellFormula
A10044927

<colgroup><col span="3"></colgroup><tbody>
</tbody>
 
Upvote 0
mfarr76,

Thanks for the workbook.

I assume that we are working in worksheet MAIN?

New sample raw data:


Excel 2007
ABCDE
1DPW WoH Replicator Spreadsheet
2
3
4
5Number of Rigs3
6Spud to RR (INTIAL)16
7Spud to RR (FINAL)15
8Switch Month12
9RR to Sales15
10Start Date1/1/20165/6/2016
11Months BTW Rig start6
12Wells Per Month1.90
13Total Wells0
14MonthlyAries
15MonthWell/MonthScheduleMonth
161/1/201601/1900
172/1/2016101/1900
183/1/2016101/1900
194/1/2016201/1900
205/1/2016201/1900
216/1/2016201/1900
227/1/2016201/1900
238/1/2016301/1900
249/1/2016301/1900
2510/1/2016401/1900
2611/1/2016401/1900
2712/1/2016401/1900
281/1/2017401/1900
292/1/2017501/1900
303/1/2017501/1900
314/1/2017601/1900
325/1/2017601/1900
336/1/2017601/1900
347/1/2017601/1900
MAIN


After the new macro (not all rows are shown for brevity):


Excel 2007
ABCDE
1DPW WoH Replicator Spreadsheet
2
3
4
5Number of Rigs3
6Spud to RR (INTIAL)16
7Spud to RR (FINAL)15
8Switch Month12
9RR to Sales15
10Start Date1/1/20165/6/2016
11Months BTW Rig start6
12Wells Per Month1.90
13Total Wells456
14MonthlyAries
15MonthWell/MonthScheduleMonth
161/1/20162/1/1602/2016
172/1/201613/1/1603/2016
183/1/201614/1/1604/2016
194/1/201624/1/1604/2016
205/1/201625/1/1605/2016
216/1/201625/1/1605/2016
227/1/201626/1/1606/2016
238/1/201636/1/1606/2016
249/1/201637/1/1607/2016
2510/1/201647/1/1607/2016
2611/1/201648/1/1608/2016
2712/1/201648/1/1608/2016
281/1/201748/1/1608/2016
292/1/201759/1/1609/2016
303/1/201759/1/1609/2016
314/1/201769/1/1609/2016
325/1/2017610/1/1610/2016
336/1/2017610/1/1610/2016
347/1/2017610/1/1610/2016
MAIN


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub DuplicateDates_V2()
' hiker95, 11/01/2014, ME815376
Dim a, o
Dim i As Long, j As Long
Dim lr As Long, n As Long, k As Long
With Sheets("MAIN")
  .Activate
  lr = .Cells(Rows.Count, 1).End(xlUp).Row - 1
  a = .Range("A1:B" & lr).Value
  n = Evaluate("=Sum(B16:B" & lr & ")")
  ReDim o(1 To n, 1 To 1)
  For i = 16 To lr
    If a(i, 2) <> "" Then
      For k = 1 To a(i, 2)
        j = j + 1
        o(j, 1) = a(i, 1)
      Next k
    End If
  Next i
  With .Range("D16").Resize(n, 1)
    .Value = o
    .NumberFormat = "m/d/yy"
  End With
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the DuplicateDates_V2 macro.
 
Upvote 0
Hiker95,

It worked great!! Thank you for the effort and being patient with me. As someone new to this I appreciate it.

Have a great day!!
 
Upvote 0
I see you have a solution so I won't continue my efforts to try to get my solution to work. I am not sure why it is stuck in an infinate loop. You stated that you think the problem was in the second loop and that you think it is due to that you have blank cells after 5 rows. I don't think that is the problem. That was the purpose of my if statement. The if statement checks if the value of the row in the B column is greater than 0. Since a blank is not greater than 0, it would not enter that loop. If I was wrong in that assumption, then you could have easily modified the if statement to say if the row in the B column is greater than 0 and is not blank then continue onto the Do Loop.

Code:
If pasteQuantity > 0 AND pasteQuantity <> "" Then
 
Upvote 0
mfarr76,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,215,339
Messages
6,124,363
Members
449,155
Latest member
ravioli44

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