vba loop repeat numbers

ramt0001

New Member
Joined
Feb 19, 2014
Messages
2
KInldy support me get the loop as mentioned below using for next loop
Thank you very much for support

1
1
1
2
2
2
3
3
3
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
ramt0001,

Welcome to the MrExcel forum.

What version of Excel and Windows are you using?


You have not given us a good idea of what your want to accomplish.

With your raw data in column A, beginning in cell A1, grouped/sorted per your text example.

We can sum the numbers in each group, and, place the answer in column B, next to the last number in each group?

Sample raw data:


Excel 2007
AB
11
21
31
42
52
62
73
83
93
10
Sheet1


After the macro:


Excel 2007
AB
11
21
313
42
52
626
73
83
939
10
Sheet1


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).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Option Explicit
Sub SumGroups()
' hiker95, 02/19/2014, ME758867
Dim r As Long, lr As Long, n As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 1).End(xlUp).Row
For r = 1 To lr
  n = Application.CountIf(Columns(1), Cells(r, 1).Value)
  If n = 1 Then
    Cells(r, 2) = Cells(r, 1)
  ElseIf n > 1 Then
    Cells(r + n - 1, 2).Value = Evaluate("=Sum(A" & r & ":A" & r + n - 1 & ")")
  End If
  r = r + n - 1
Next r
Application.ScreenUpdating = True
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 SumGroups macro.
 
Upvote 0
Many Thanks you for your reply

I may did not expalin well?
the required output needs as below
i use the loop statement but cannot get required output

Sub LoopA()
Dim i, j As Integer
j = 1
For i = 1 To 15
Cells(i, 1).Value = j
j = j + 1
Next i
End Sub


No.Result
11
21
31
42
52
62
73
83
93
104
114
124
135
145
155

<COLGROUP><COL style="WIDTH: 48pt" span=2 width=64><TBODY>
</TBODY>
 
Upvote 0
ramt0001,

Thanks for the screenshot - now I understand what your results should look like.

Sample raw data:


Excel 2007
AB
1No.
21
32
43
54
65
76
87
98
109
1110
1211
1312
1413
1514
1615
17
18
19
20
21
22
23
24
25
26
27
28
Sheet1


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


Excel 2007
AB
1No.Result
211
321
431
542
652
762
873
983
1093
11104
12114
13124
14135
15145
16155
176
186
196
207
217
227
238
248
258
269
279
289
Sheet1


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:
Option Explicit
Sub ExpandNumbers()
' hiker95, 02/20/2014, ME758867
Dim c As Range, nr As Long, n As Long
Application.ScreenUpdating = False
Columns(2).ClearContents
With Cells(1, 2)
  .Value = "Result"
  .Font.Bold = True
End With
nr = 2: n = 0
For Each c In Range("A2", Range("A" & Rows.Count).End(xlUp))
  If n = 0 Then
    With Cells(nr, 2).Resize(3)
      .Value = c.Value
      .Font.Bold = True
    End With
    n = 1
  ElseIf n = 1 Then
    Cells(nr, 2).Resize(3).Value = c.Value
    n = 0
  End If
  nr = nr + 3
Next c
Application.ScreenUpdating = True
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 ExpandNumbers macro.
 
Upvote 0
Many Thanks you for your reply

I may did not expalin well?
the required output needs as below
i use the loop statement but cannot get required output

Sub LoopA()
Dim i, j As Integer
j = 1
For i = 1 To 15
Cells(i, 1).Value = j
j = j + 1
Next i
End Sub

You were almost there, just add a second loop nested in the first:
Code:
Sub LoopA()
Dim i, j, k As Long
k = 1
For j = 1 To 5
    For i = 1 To 3
        Cells(k, 1).Value = j
        k = k + 1
    Next i
Next j
End Sub

HTH,
~ Jim
 
Upvote 0

Forum statistics

Threads
1,215,474
Messages
6,125,024
Members
449,204
Latest member
LKN2GO

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