Error in using VBA Macro with Apply Colors and Line Numbers

Joined
Jan 11, 2024
Messages
19
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
The code is below: See the attachment also. Its overwriting the first column instead of adding a new column before column A

Sub InsertLineNumbersAndApplyColors()
Dim ws As Worksheet
Dim cell As Range
Dim rowNum As Long
Dim colorIndex As Long

' Define an array of light colors
Dim lightColors As Variant
lightColors = Array(vbWhite, vbYellow, vbCyan, vbMagenta, vbGreen, vbRed, vbBlue, RGB(255, 255, 204), RGB(204, 255, 204), RGB(204, 204, 255))

' Loop through each worksheet in the workbook
For Each ws In ThisWorkbook.Worksheets
' Reset the row number
rowNum = 1

' Loop through each cell in the first column of the worksheet
For Each cell In ws.Range(ws.Cells(1, 1), ws.Cells(ws.Rows.Count, 1).End(xlUp))
' Insert the text "Line number X" in the cell
cell.Value = "Line number " & rowNum
' Apply a light color to the cell
colorIndex = (rowNum - 1) Mod UBound(lightColors) + 1
cell.Interior.Color = lightColors(colorIndex)
' Increment the row number
rowNum = rowNum + 1
Next cell
Next ws
End Sub

Error .jpg
Deleting 1st column.jpg
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Please use the code formatting when you post code it makes it easier to read. ( Click the VBA button and then select paste)
Try this additional line:
VBA Code:
For Each ws In ThisWorkbook.Worksheets
    Range("a1:a1").EntireColumn.Insert ' just add this line after your for loop
 
Upvote 1
Sub InsertLineNumberColumn()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long

For Each ws In ThisWorkbook.Worksheets
' Find the last used row in column A
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

' Insert a new column at the beginning
ws.Columns("A:A").Insert Shift:=xlToRight

' Add text and colors to the cells in the new column
For i = 1 To lastRow
ws.Cells(i, 1).Value = "Line Number " & i
' Apply light colors to each cell
Select Case i Mod 5
Case 1
ws.Cells(i, 1).Interior.Color = RGB(230, 230, 250) ' Lavender
Case 2
ws.Cells(i, 1).Interior.Color = RGB(240, 255, 240) ' Honeydew
Case 3
ws.Cells(i, 1).Interior.Color = RGB(255, 228, 225) ' BlanchedAlmond
Case 4
ws.Cells(i, 1).Interior.Color = RGB(245, 245, 220) ' Beige
Case 0
ws.Cells(i, 1).Interior.Color = RGB(240, 255, 255) ' Azure
End Select
Next i
Next ws
End Sub

VBA Code:
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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