request for a loop in VBA

vimala

New Member
Joined
Jun 22, 2010
Messages
14
hi,

i have a blank sheet and i printed 'X' in column AA5 by macro. now i need a loop in this macro which can print 'X' continuosly till AZ5.

Please help me on this.

regards,
Vimala
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi,

Thanks for your reply. I know that we can directly type X in selected area by giving from and to cells.

But I wanted to learn a loop function by using activecell.offset so that it will print X in next column and it should continue till where I want to.

Thanks,
Vimala
 
Upvote 0
Hi,

Thanks for your reply. I know that we can directly type X in selected area by giving from and to cells.

But I wanted to learn a loop function by using activecell.offset so that it will print X in next column and it should continue till where I want to.

Thanks,
Vimala

Here's a copy of one of the results of looking up LOOP in VBE Help :-

Looping Through a Range of Cells

When using Visual Basic, you often need to run the same block of statements on each cell in a range of cells. To do this, you combine a looping statement and one or more methods to identify each cell, one at a time, and run the operation.
One way to loop through a range is to use the For...Next loop with the Cells property. Using the Cells property, you can substitute the loop counter (or other variables or expressions) for the cell index numbers. In the following example, the variable <CODE>counter</CODE> is substituted for the row index. The procedure loops through the range C1:C20, setting to 0 (zero) any number whose absolute value is less than 0.01.




<TABLE><TBODY><TR><TD>

<CODE>Sub RoundToZero1()</CODE>
<CODE>For Counter = 1 To 20</CODE>
<CODE>Set curCell = Worksheets("Sheet1").Cells(Counter, 3)</CODE>
<CODE>If Abs(curCell.Value) < 0.01 </CODE><CODE>Then </CODE><CODE>curCell.Value = 0</CODE>
<CODE>Next Counter</CODE>
<CODE>End Sub</CODE>






</PRE></TD></TR></TBODY></TABLE></P>Another easy way to loop through a range is to use a For Each...Next loop with the collection of cells specified in the Range property. Visual Basic automatically sets an object variable for the next cell each time the loop runs. The following procedure loops through the range A1:D10, setting to 0 (zero) any number whose absolute value is less than 0.01.




<TABLE><TBODY><TR><TD>

<CODE>Sub RoundToZero2()</CODE>
<CODE>For Each c In Worksheets("Sheet1").Range("A1:D10").Cells</CODE>
<CODE>If Abs(c.Value) < 0.01 Then </CODE><CODE>c.Value = 0</CODE>
<CODE>Next</CODE>
<CODE>End Sub</CODE>






</PRE></TD></TR></TBODY></TABLE></P>If you do not know the boundaries of the range you want to loop through, you can use the CurrentRegion property to return the range that surrounds the active cell. For example, the following procedure, when run from a worksheet, loops through the range that surrounds the active cell, setting to 0 (zero) any number whose absolute value is less than 0.01.




<TABLE><TBODY><TR><TD>

<CODE>Sub RoundToZero3()</CODE>
<CODE>For Each c In ActiveCell.CurrentRegion.Cells</CODE>
<CODE>If Abs(c.Value) < 0.01 Then </CODE><CODE>c.Value = 0</CODE>
<CODE>Next</CODE>
<CODE>End Sub</CODE>






</PRE></TD></TR></TBODY></TABLE></P>
 
Last edited:
Upvote 0
The below is my actual script and it's printing X only in 25,26 columns of the sheet its not going to 27th column to print X.

So can you please change the script to contine print X till 35th column.

Sub test()
Dim row As Integer

Cells(5, 25).Select
ActiveCell.FormulaR1C1 = "X"

a = 5

Do

If Cells(a, 25) <> "" Then
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "X"
Else
a = a + 1
End If
Loop Until Cells(5, 35) = ""
End Sub
 
Upvote 0
What is it exactly you want to do?

Put X in all cells from AA5 to AJ5 (or maybe some other cells?) only if they are blank ?
 
Upvote 0
There's a problem in your code
Rich (BB code):
Sub test()
Dim row As Integer
 
Cells(5, 25).Select
ActiveCell.FormulaR1C1 = "X"
 
a = 5
 
Do
 
If Cells(a, 25) <> "" Then
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "X"
Else
a = a + 1
End If
Loop Until Cells(5, 35) = ""
End Sub
You're starting in cell Y5, printing an X and then testing cell Z5 and printing another X. Then you're testing cell AI5 (where I have coloured red) to see if it's empty. I am guessing it is empty which is why no more X's are printed because you've stopped looping

Another comment is you really should try to avoid using .select in VBA code where possible.

Try running this code instead:
Rich (BB code):
Sub PuppyPower ()
 
Dim a as Integer
 
a = 25
Do
  cells(5,a) = "X"
  a = a + 1
Loop Until a = 53
 
End Sub
 
Upvote 0
Although, generally speaking if you know you the last value for your loop, you're better of using a FOR NEXT loop, e.g
Code:
Sub PuppyPower()
 
Dim a as Integer
 
For a = 25 to 52
  Cells(5,a) = "X"
Next a
 
End Sub
This is covered in Boller's postings too
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,839
Members
452,948
Latest member
UsmanAli786

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