How to write a loop for to with 2 variables

Nagini

New Member
Joined
Dec 11, 2016
Messages
34
Hello again!!

I've tried to execute this loop but it doesn't work. I need to define 2 variables, "i" and "j". The idea is that if "i" is 8, in the same loop "j" must be 2. And in the next loop "i" must be increased in one unity to de right (next column) and "j" down (next row).

Any idea to solve it??

______________


Sub Macro2()


Dim Indices, Seleccion As Worksheet
Set Indices = Sheets("C.Indices")
Set Seleccion = Sheets("Sel.Mes")

LastCol = Indices.Cells(1, Columns.Count).End(xlToLeft).Column
LastRow = Seleccion.Cells(sht.Rows.Count, "A").End(xlUp).Row


For i = 8 To LastCol
For j = 2 To LastRow


Sheets("C.Indices").Select
Columns(i).Select
Selection.Copy
Sheets("CALCULOS").Select
Columns("D:D").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.Goto Reference:="R318C50"
Range("BR318:CK318").Select
Selection.Copy
Seleccion.Cells(j, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Next i
Next j


End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I've tried this, but It doesn't work either.
Sub Macro2()


Dim Indices, Seleccion As Worksheet
Set Indices = Sheets("C.Indices")
Set Seleccion = Sheets("Sel.Mes")
Dim i As Integer

LastCol = Indices.Cells(1, Columns.Count).End(xlToLeft).Column

For i = 8 To LastCol

Sheets("C.Indices").Select
Columns(i).Select
Selection.Copy
Sheets("CALCULOS").Select
Columns("D:D").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.Goto Reference:="R318C50"
Range("BR318:CK318").Select
Selection.Copy
Seleccion.Cells([i-6], 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Next i

EndSub
 
Upvote 0
After mis-read where does the below leave you?



Code:
Sub Macro2()


Dim Indices, Seleccion As Worksheet
Set Indices = Sheets("C.Indices")
Set Seleccion = Sheets("Sel.Mes")

 LastCol = Indices.Cells(1, Columns.Count).End(xlToLeft).Column
 LastRow = Seleccion.Cells(sht.Rows.Count, "A").End(xlUp).Row

j = 2
For i = 8 To LastCol



 Sheets("C.Indices").Select
 Columns(i).Select
 Selection.Copy
 Sheets("CALCULOS").Select
 Columns("D:D").Select
 Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
 :=False, Transpose:=False
 Application.Goto Reference:="R318C50"
 Range("BR318:CK318").Select
 Selection.Copy
 Seleccion.Cells(j, i).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
 :=False, Transpose:=False
j = j +1
Next i



End Sub
 
Last edited:
Upvote 0
Sorry I should have stated that I am guessing that the i below should be there and not the 1, obviously change it if not the case.

Code:
Seleccion.Cells(j, [COLOR="#FF0000"]i[/COLOR]).
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,795
Members
449,468
Latest member
AGreen17

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