Can you please write me a VBA code that will do the following: Moving up the Find the…
- User
Can you please write me a VBA code that will do the following: Moving up the Find the first row with data, and on that row write todays date to the following columns B, E, H, K, N, Q, T
- Assistant
Certainly! Below is a VBA code snippet that will find the first row with data in an Excel worksheet and then write today's date in the specified columns (B, E, H, K, N, Q, T) of that row.
To use this code, follow these steps:
- Open Excel and press
ALT + F11to open the VBA editor. - In the VBA editor, choose
Insert > Moduleto create a new module. - Copy and paste the following code into the module window:
Sub WriteTodaysDate() Dim ws As Worksheet Dim firstRow As Long Dim columnsToWrite As Variant Dim i As Long ' Set the worksheet you are working on Set ws = ThisWorkbook.Worksheets("Sheet1") ' Change "Sheet1" to your actual sheet name ' Find the first row with data firstRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row ' Define the columns to write the date columnsToWrite = Array(2, 5, 8, 11, 14, 17, 20) ' B, E, H, K, N, Q, T ' Write today's date in the specified columns For i = LBound(columnsToWrite) To UBound(columnsToWrite) ws.Cells(firstRow, columnsToWrite(i)).Value = Date Next i ' Optional: Inform user the operation is complete MsgBox "Today's date has been written to the row " & firstRow & " in specified columns." End Sub - Open Excel and press
- User
Can you ammend this VBA to do the following: After the user has beem prompted to select a range if the range is selected is not from from A to U and A to U only then pop up the message "Your Row selection must be from A to U" line break "Archive Aborted, please start again", vbCritical, "ROW A TO U" Exit Sub If the row selected is fro A to U, the continue with the rest of the code.
- Assistant
- User
Even when I select A to U, it still does not recognise my range selection and it exits the sub
- Assistant