Trying to create a loop

Oliver1804

New Member
Joined
Jan 30, 2020
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
ThisWorkbook.Save
Sheet4.Range("A1").Value = Environ("Username")
Sheet4.Range("B1").Value = Time()
Sheet4.Range("C1").Value = Date
Sheet4.Range("D1").Value = ActiveWorkbook.Name


I'm sorry if this is a stupid question, I'm new to this, how can I get this code to repeat itself. It works but obviously only on the top row.

Thank you for your time :)
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
How many times do you want it to repeat?
 
Upvote 0
Thanks for your reply, I would like to have it repeat a hundred times.
 
Upvote 0
I would assume that there must be some kind of conditions to go with the requirement, repeating that code in a loop 2 times, 100 times, or 1,000,000,000 times would have no real purpose.

This will do what you ask, but I suspect that it will be far from what you need.
VBA Code:
Dim n As Long
For n = 1 To 100
    ThisWorkbook.Save
    Sheet4.Range("A1").Value = Environ("Username")
    Sheet4.Range("B1").Value = Time()
    Sheet4.Range("C1").Value = Date
    Sheet4.Range("D1").Value = ActiveWorkbook.Name 
Next
To loop code, you can use For or For Each and Next, either Do, Do While or Do Until and Loop or While and Wend.
For most loops you could use any of those methods, however there advantages to certain ones depending on the actual appliaction of the code.
 
Upvote 0
I think I may have worded my question wrong, as the code just creates a loop of saving the file a 100 times, I understand why you said it has no real purpose.

What I meant to say is that when the button is clicked the username, time, date, and file name is filled out on the 1st row of sheet4 (columns A-D), then when its next clicked again the second row is filled etc
 
Upvote 0
I don't think you are referring to a loop, you just want to have data entry in the next available row in Sheet4, is that correct? If so, please check the following code.

VBA Code:
Sub Oliver1804()
Dim lastRow As Long
With Sheets("Sheet4")
    lastRow = .Cells(Rows.Count, 1).End(xlUp).Row
    .Cells(lastRow, 1).Offset(1, 0).Resize(1, 4).Value = Array(Environ("Username"), Time(), Date, ActiveWorkbook.Name)
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,220
Messages
6,123,693
Members
449,117
Latest member
Aaagu

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