VBA Loop Through an Array

Ben AFF

Board Regular
Joined
Sep 21, 2023
Messages
55
Office Version
  1. 365
Platform
  1. Windows
Hi, please can you help me with the following.
I have worked on a bit of VBA code that loops through and array to change the values of the cells to 1 until last empty cell for all columns in the array.
However its not working as intended. Only if I change the value of J to any of the columns ("A","B",etc..) does work but only changes the values for the specified column.

Can you help with what needs to be changed for the code to loop through all columns and rows? Much thanks.

VBA Code:
Sub Loop2()

Dim LastRow As Long, i As Long, j As Long, ws As Worksheet
Set ws = Sheets("Sheet1")

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

With ws

    For j = 1 To LastCol
               
      For i = 1 To LastRow
     
        .Range(j & i) = 1
     
        Next i

    Next j

End With

End Sub
 
Last edited by a moderator:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
You could do it in a loop or all at once. Loop2, I believe, fixes yours. Loop3 is not a loop, but just simply applies the number to all cells you wanted.

VBA Code:
Sub Loop2()

  Dim LastRow As Long, i As Long, j As Long, ws As Worksheet
  Set ws = Sheets("Sheet1")

  LastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
  lastcol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
  
  With ws
    For j = 1 To lastcol
      For i = 1 To LastRow
        .Cells(i, j) = 1
      Next i
    Next j
  End With

End Sub

Sub Loop3()
  Dim Cel As Range
  Dim Rng As Range
  Dim ws As Worksheet
  
  Set ws = Sheets("Sheet1")

  LastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
  lastcol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
  
  With ws
    Set Cel = .Cells(1, 1)
    Set Rng = .Range(Cel, .Cells(LastRow, lastcol))
    Rng.Value = 1
  End With
  
End Sub
 
Upvote 0
Solution
Try this one liner:
VBA Code:
Sub ChangeValues()
    ActiveSheet.UsedRange.Cells.Value = 1
End Sub
 
Upvote 0
Much appreciated Sir.
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.


Also, when posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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