Loop required for a macro

Vishaal

Well-known Member
Joined
Mar 16, 2019
Messages
533
Office Version
  1. 2010
  2. 2007
Platform
  1. Windows
  2. Web
Hi,

Please check the following macro code and advice how can we run it in loop,

Code:
Sub Loop()
'
' Loop Macro
'
    Range("D2:D8").Select
    Selection.Copy
    Range("E8").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Range("F2:F8").Select
    Selection.Copy
    Range("G8").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.Copy
    Range("E8").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
try this.

Code:
    For c = 4 To 10 Step 2 'adjust as neeed
    Range(Cells(8, c + 1), Cells(14, c + 1)).Value = Range(Cells(1, c), Cells(7, c)).Value
    Next c

hth,
Ross
 
Upvote 0
tried not working,

we are using this as per following

Code:
Sub Macro2()
'
' Macro2 Macro
'


For c = 4 To 10 Step 2 'adjust as neeed
    Range(Cells(8, c + 1), Cells(14, c + 1)).Value = Range(Cells(1, c), Cells(7, c)).Value
    Next c
End Sub

its right process or not, confirm pls
 
Last edited:
Upvote 0
Hi
The code working Ok
Code:
Sub test()
[COLOR=#40e0d0]Dim c As Long ' [/COLOR]may be missing if Option Explicit
For c = 4 To 10 Step 2 'adjust as neeed
    Range(Cells(8, c + 1), Cells(14, c + 1)).Value = Range(Cells(1, c), Cells(7, c)).Value
    Next c
End Sub
 
Upvote 0
Hi
The code working Ok
Code:
Sub test()
[COLOR=#40e0d0]Dim c As Long ' [/COLOR]may be missing if Option Explicit
For c = 4 To 10 Step 2 'adjust as neeed
    Range(Cells(8, c + 1), Cells(14, c + 1)).Value = Range(Cells(1, c), Cells(7, c)).Value
    Next c
End Sub


Hi Mohadin,

We have change the procedure so that we can do in fast, We have done the following step by step

1. We have used the formula in D8 =IF($C8 = "", "", IF(ISNUMBER(MATCH($C8,D$1:D$5,0)),"","Check")) and after that we drag & drop it till D16
2. After that we have used the formula in E8 =IF(AND(D8="Check",D9=""),COUNTIF(D$1:D8,"Check")-SUM(E$7:F7),"") and after that drag & drop it till F16

Excel 2010 32 bit
A
B
C
D
E
F
G
1
387​
50000​
0​
50000​
2
50​
960​
50​
960​
3
125​
514​
125​
514​
4
220​
265​
220​
265​
5
40000​
62​
40000​
62​
6
7
S.No.​
Month​
Amount​
8
1​
Jan-15​
514​
Check​
1​
9
2​
Feb-15​
220​
10
3​
Mar-15​
125​
11
4​
Apr-15​
125​
12
5​
May-15​
62​
Check​
1​
13
6​
Jun-15​
220​
14
7​
Jul-15​
50​
15
8​
Aug-15​
960​
Check​
16
9​
Sep-15​
265​
Check​
2​
Sheet: Sheet1


Now we are trying to do it in one step in stead of two, we mean that in the above sheet we have used first formula in D and second in E, now we want a VBA/Formula who can show the second formula result in column D

Result Sheet are
Excel 2010 32 bit
A
B
C
D
E
F
G
1
387​
50000​
0​
50000​
2
50​
960​
50​
960​
3
125​
514​
125​
514​
4
220​
265​
220​
265​
5
40000​
62​
40000​
62​
6
7
S.No.​
Month​
Amount​
8
1​
Jan-15​
514​
1​
9
2​
Feb-15​
220​
10
3​
Mar-15​
125​
11
4​
Apr-15​
125​
12
5​
May-15​
62​
1​
13
6​
Jun-15​
220​
14
7​
Jul-15​
50​
15
8​
Aug-15​
960​
16
9​
Sep-15​
265​
2​
Sheet: Sheet1


help pls
 
Upvote 0
We have change the procedure so that we can do in fast, We have done the following step by step

1. We have used the formula in D8 =IF($C8 = "", "", IF(ISNUMBER(MATCH($C8,D$1:D$5,0)),"","Check")) and after that we drag & drop it till D16
2. After that we have used the formula in E8 =IF(AND(D8="Check",D9=""),COUNTIF(D$1:D8,"Check")-SUM(E$7:F7),"") and after that drag & drop it till F16
There is a really easy way to do this.
1. Turn on your Macro Recorder, and record yourself manually entering these equations into cells D8 and E8.
2. When finished, edit the VBA code so that you are entering the formula in the range "D8:D16" all at once, instead of "D8" (and do the same for the other formula).

So, if you turn on your Macro Recorder and record yourself entering the formula into cell D8, you will get VBA code that looks like this:
Code:
    Range("D8").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(RC3 = """", """", IF(ISNUMBER(MATCH(RC3,R1C:R5C,0)),"""",""Check""))"
Because the Macro Recorder is so literal, it records the cell selection, which is unnecessary, and this two lines can be combined to one line like this:
Code:
   Range("D8").FormulaR1C1 = _
        "=IF(RC3 = """", """", IF(ISNUMBER(MATCH(RC3,R1C:R5C,0)),"""",""Check""))"
Instead of using auto-fill to populate all the other cells in that column, if you know how far down you want to go, you can simply apply the formula to the entire range at once.
So, just edit the range to apply the formula to:
Code:
    Range([COLOR=#ff0000]"D8:D16"[/COLOR]).FormulaR1C1 = _
        "=IF(RC3 = """", """", IF(ISNUMBER(MATCH(RC3,R1C:R5C,0)),"""",""Check""))"
That's it! You made Excel do most of the work for you!
You can do the exact same thing with the other formula.
 
Upvote 0
its a very good lines

"Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"
 
Upvote 0
we know it but we cant make it in loop like your reply in thread, if you can do a small guidance we can do it

https://www.mrexcel.com/forum/excel-questions/1108598-loop-copy-paste-4.html

Rich (BB code):
Sub copy_formula()           its vba name
    Application.ScreenUpdating = False              its showing that the screen will not change during the process
    With ActiveSheet                               We have selected the sheet
        Set Rng = Range("a12:a24")           We have selected the range
        Rng.Copy                                      We have copy the range
        For i = 2 To 50                               We have given the instruction, how many times it will do 
            .Cells(12, i).PasteSpecial Paste:=xlPasteFormulas                  we have done paste special through it
            With .Cells(12, i).Resize(13)
                .Value = .Value
            End With
        Next
    End With
End Sub

but how can we do this in the current thread, because we want to first copy formula from two columns and after that cut from second column and paste it on first column and do it untill the last value or column
 
Last edited:
Upvote 0
we mean how we do

Rng = Range("a12:b24")
Rng.copy
For i = 2 To 50
.Cells(12, i).PasteSpecial Paste:=xlPasteFormulas
With .Cells(12, i).Resize(13)
.Value = .Value

now we want to cut the value from column b and paste value in column a in loop
 
Upvote 0

Forum statistics

Threads
1,214,424
Messages
6,119,404
Members
448,893
Latest member
AtariBaby

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