VBA to insert data to specific sheets

Jmoz092

Board Regular
Joined
Sep 8, 2017
Messages
184
Office Version
  1. 365
  2. 2011
Platform
  1. Windows
  2. MacOS
I'm having trouble with a code. I want to insert data into some cells on certain worksheets in my workbook. I can do this for one sheet, but it is not doing it for every sheet that I intend it to. Also, I don't know how to VBA a border around each cell and auto adjust the width of the column to fit the newly inserted text. Any help would be appreciated.

Code:
Sub AddCheckData()
    
    Dim ws As Worksheet
    Dim check(2) As String
            check(0) = "Yes"
            check(1) = "No"


   Application.ScreenUpdating = False
   
   For Each ws In ThisWorkbook.Worksheets
   If ws.Name Like "**-**-** Invoice" Then
   Range("F25").Value = ("Check Received?")
   Range("G25").Value = ("Check #")
   Range("H25").Value = ("Date Received")
   End If
        With Range("F26").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
            Operator:=xlBetween, Formula1:=Join(check, ",")
    End With
    Next ws
    Application.ScreenUpdating = True


End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi,
Your ranges are unqualified so your code will only apply to the activesheet

Update your code where shown in RED

Rich (BB code):
Sub AddCheckData()
    
    Dim ws As Worksheet
    Dim check(2) As String
    check(0) = "Yes"
    check(1) = "No"
    
    Application.ScreenUpdating = False
    
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name Like "**-**-** Invoice" Then
            ws.Range("F25").Value = ("Check Received?")
            ws.Range("G25").Value = ("Check #")
            ws.Range("H25").Value = ("Date Received")
         
        With ws.Range("F26").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
            Operator:=xlBetween, Formula1:=Join(check, ",")
        End With
       End If
     Next ws
     Application.ScreenUpdating = True
End Sub

With regard to the cell formatting - turn the macro recorder on & perform the required format - this will give you the basic code needed which you can clean-up & call from your procedure when required.

Hope Helpful

Dave
 
Last edited:
Upvote 0
Dave, thanks for the help and suggestion. I was able to get what I was after with this:

Code:
Sub AddCheckData()
    
    Dim ws As Worksheet
    Dim check(2) As String
            check(0) = "Yes"
            check(1) = "No"


   Application.ScreenUpdating = False
   
   For Each ws In ThisWorkbook.Worksheets
   If ws.Name Like "**-**-** Invoice" Then
        ws.Range("F25").Value = ("Check Received?")
        ws.Range("G25").Value = ("Check #")
        ws.Range("H25").Value = ("Date Received")
   End If
        With ws.Range("F26").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
            Operator:=xlBetween, Formula1:=Join(check, ",")
    End With
    
    With ws.Range("F25:H26").Borders(xlDiagonalDown).LineStyle = xlNone
    End With
    With ws.Range("F25:H26").Borders(xlDiagonalUp).LineStyle = xlNone
    End With
    
    With ws.Range("F25:H26").Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = 1
    End With
    With ws.Range("F25:H26").Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With ws.Range("F25:H26").Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With ws.Range("F25:H26").Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With ws.Range("F25:H26").Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With ws.Range("F25:H26").Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    
    With ws.Columns("F:H").EntireColumn.AutoFit
    End With
    
    Next ws
    
    Application.ScreenUpdating = True


End Sub
 
Upvote 0
Hi,
glad suggestions helped.
Appreciate your feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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