intellitechdataservices
New Member
- Joined
- Jan 11, 2024
- Messages
- 19
- Office Version
- 2019
- 2016
- Platform
- 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
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