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
 

Some videos you may like

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Nagini

New Member
Joined
Dec 11, 2016
Messages
34
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
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,828
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
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:

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,828
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
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:

Watch MrExcel Video

Forum statistics

Threads
1,122,509
Messages
5,596,566
Members
414,078
Latest member
Frills

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
Top