vba

coquinn

New Member
Joined
Oct 23, 2005
Messages
24
Hi I am trying to link a loop in VBA to a referenced worksheet. Specfically I am trying to let values in two sheets equal each other i.e.
For i = i_start To 17
For j = j_start1 To 14
Worksheets("Analysis").Cells(i, j + 55).Value = Worksheets("Analysis FR").Cells(i + 164, j + 54).Value
Next j
Next i
However the loop gets violated whenever I move a column. Does anyone know what the bext way to achiev this would be? Many thanks
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
coquinn.

i guess i'm confused a bit by what you mean when you say "violated." if you mean that your loop no longer works with the right columns, then it seems like you just need to set your i_start and j_start to the appropriate starting columns. i think this would most easily be accomplished via an inputbox or perhaps a userform.

another option would be to use a named range for your cells, and then reference the named range in your program. that way, as long as the named range moves when you insert columns, your program will access the right cells.

am i missing something?
ben.
 
Upvote 0
coquinn.

you would want to define a name for the cell Cell(i_start, j_start). you can do this in your workbook with the shortcut key ctrl+F3

once you have this name defined, you can refer to it in your code as
Code:
Range("MyNamedRange")
since you have defined MyNamedRange to be the cell with at row i_start and column j_start, all you need to do to get these values in VBA is
Code:
i_start = Range("MyNamedRange").Row
j_start = Range("MyNamedRange").Column
if you insert columns, your named range will move correspondingly, which will ensure that i_start and j_start are correct.

let me know if this makes sense...
ben.
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

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