VBA loop

Cloris

New Member
Joined
Oct 24, 2021
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi, could someone know how to simplify into a loop? using For i = 1 To 4? it is just a sample, total number would until 59 .Thank you
VBA Code:
Sub Macro()


'   Range("K3").Select
    ActiveCell.FormulaR1C1 = "1"
    Range("G5").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("CrossMult_Validation").Select
    Range("B14").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
     
    Sheets("llustration").Select
    Range("K3").Select
    ActiveCell.FormulaR1C1 = "2"
    Range("G5").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("CrossMult_Validation").Select
    Range("C14").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Sheets("llustration").Select
    Range("K3").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "3"
    Range("G5").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("CrossMult_Validation").Select
    Range("D14").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Sheets("llustration").Select
    Range("K3").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "4"
    Range("G5").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("CrossMult_Validation").Select
    Range("E14").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

End Sub
 
Last edited by a moderator:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try this:
VBA Code:
Sub Macro()
Dim WS1 As Worksheet, WS2 As Worksheet, i As Long
Set WS1 = Sheets("Sheet1")
Set WS2 = Sheets("Sheet2")
For i = 1 To 4
WS1.Range("K3").Value = i
WS1.Range("G5", WS1.Range("G5").End(xlDown)).Copy
WS2.Cells(14, i + 1).PasteSpecial Paste:=xlPasteValues
Next i
End Sub
 
Last edited:
Upvote 0
Try this:
VBA Code:
Sub Macro()
Dim WS1 As Worksheet, WS2 As Worksheet, i As Long
Set WS1 = Sheets("Sheet1")
Set WS2 = Sheets("Sheet2")
For i = 1 To 4
WS1.Range("K3").Value = i
WS1.Range("G5", WS1.Range("G5").End(xlDown)).Copy
WS2.Cells(14, i + 1).PasteSpecial Paste:=xlPasteValues
Next i
End Sub
Hi, I hav error on this , WS2.Cells(14, i + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

May i know why? Thankyou in advance !
 
Upvote 0
1. I Edit code, use the last code
2. i always should be number. if i is number, what is error number that you see
 
Upvote 0
1. I Edit code, use the last code
2. i always should be number. if i is number, what is error number that you see
Thanks Maabadi, the problem solved. :)
Another ques,
May I know how to simplify this?

VBA Code:
Sub Macro3()
'
    Range("D2").Select
    Sheets("Base").Select
    Cells.Select
    Selection.Copy
    Sheets("Base").Select
    Sheets.Add After:=Sheets(Sheets.Count)
    Cells.Select
    ActiveSheet.Paste
    Range("B5").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "=Input!R[-4]C[2]"
    Cells.Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    ActiveSheet.Name = Range("B5").Value

End Sub


Can it simplify to like this?

VBA Code:
Dim WS1 As Worksheet
Set WS1 =Sheets (“Base”)
WS1.Range("D2").Select
Cells.Select
Selection.Copy
WS1.Select
Sheets.Add After:=Sheets(Sheets.Count)
Cells.Select
ActiveSheet.Paste
Range("B5").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=Input!R[-4]C[2]"
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.Name = Range("B5").Value

End Sub



Thankyou in advance Maabadi ! :)
 
Upvote 0
Try this:
VBA Code:
Sub Macro3()
    Sheets("Base").Copy After:=Sheets(Sheets.Count)
    Range("B5").Value = Sheets("Input").Range("D1").Value
    ActiveSheet.Name = Range("B5").Value
End Sub

When you write macro, you can remove select method, because 1. make your code slower, 2. you can join select line with selection line after that with removing select & selection
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,397
Members
449,081
Latest member
JAMES KECULAH

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