VBA Excel cycle through sheets, find last row in table and autofill with data

VfTom

New Member
Joined
Mar 3, 2023
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I need a script that cycles through all the sheets in a workbook and adds 2 (or more) columns at the end of the table of each sheet (except the first sheet).
In this case I have a YES and NO column to be added.

The added columns YES and NO need to be autofilled all the way down to the end with YES or NO like in the example here below.

1700725455481.png


I have the below script but it doesn't work unfortunately:

Sub LoopSheetsAddDataFilledColumns()

Dim LastRow As Long
Dim LastCol As Long
Dim iRow As Long

Dim ws As Worksheet

For Each ws In Worksheets

Set ws = Sheets(2)

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

.Columns(LastCol + 1).EntireColumn.Insert
.Cells(1, LastCol + 1).Value = "YES"
.Columns(LastCol + 2).EntireColumn.Insert
.Cells(1, LastCol + 2).Value = "NO"

End With

Next

End Sub

- this script adds only a few columns with YES and NO on the first sheet,
- it doesn't fill the columns all the way to the end,
- It doesn't make exceptions for particular sheets,

Can someone please help update this scrip? Thank you in advance.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach screenshots (not pictures) of your first sheet and at least one of the other sheets. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
adds 2 (or more) columns at the end of the table of each sheet
Also, please clarify how you determine how many columns to add to each sheet and what the content of those added columns will be.
 
Upvote 0

Forum statistics

Threads
1,223,868
Messages
6,175,082
Members
452,611
Latest member
bls2024

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