VBA to reference from worksheet names and insert value into columns

Neveidas

New Member
Joined
Oct 6, 2021
Messages
44
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to use VBA to fill an entire column with the value of choice if the worksheet name is right, and after the column is filled, it will proceed to the next worksheet and do the same. I am unsure
if I am writing this correctly, greatly appreciate it if anyone is able to advise or help correct the codes!

VBA Code:
Private Sub Fill()
Dim WS As Worksheet

Dim i As Long
Dim r As Range


i = Range("B" & Rows.Count).End(xlUp).Row
Set r = Range("A2:A" & i)

For Each WS In ActiveWorkbook.Worksheets

If WS.Name = "Update(Pre)" Or WS.Name = "Update XB(Pre)" Then r.Value = "Update(Pre)"
If WS.Name = "Update(Breach)" Or WS.Name = "Update XB(Breach)" Then r.Value = "Update(Breach)"
If WS.Name = "Lost(Pre)" Or WS.Name = "Lost XB(Pre)" Then r.Value = "Lost(Pre)"
If WS.Name = "Lost(Breach)" Or WS.Name = "Lost XB(Breach)" Then r.Value = "Lost(Breach)"

Next WS





End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I would do it this way:
VBA Code:
Sub Fill()
'Modified  10/6/2021  5:40:58 AM  EDT
Application.ScreenUpdating = False
Dim WS As Worksheet
Dim i As Long
Dim r As Range

    For Each WS In ActiveWorkbook.Worksheets

        i = WS.Range("B" & Rows.Count).End(xlUp).Row
        Set r = WS.Range("A2:A" & i)
        
        Select Case WS.Name
            Case "Update(Pre)", "Update XB(Pre)": r.Value = "Update(Pre)"
            Case "Update(Breach)", "Update XB(Breach)": r.Value = "Update(Breach)"
            Case "Lost(Pre)", "Lost XB(Pre)": r.Value = "Lost(Breach)"
            Case "Lost(Breach)", "Lost XB(Breach)": r.Value = "Lost XB(Breach)"
        End Select

    Next WS
Application.ScreenUpdating = False
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,064
Messages
6,122,936
Members
449,094
Latest member
teemeren

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