VBA批量设置单元格值和数据有效性

需要在excel表格中的A116单元格设置值为"柯达阳图785",然后将这个表格的单元格f2:g52的值设置为序列,来源为=$A$102:$A$116,用VBA实现。

Sub AddValidation()
Dim targetWs As Worksheet
Dim sheetIndex As Integer
Dim wsName As String

For sheetIndex = 16 To 31
  wsName = "10月" & sheetIndex & "日"
  On Error Resume Next
  Set targetWs = Worksheets(wsName)
  On Error GoTo 0
  targetWs.Range("A116").Value = "柯达阳图785"

  If Not targetWs Is Nothing Then
    With targetWs.Range("f2:g52").Validation
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                     Operator:=xlBetween, Formula1:="=$A$102:$A$116"
    .IgnoreBlank = True
    .InCellDropdown = True
    .ShowInput = True
    .ShowError = True
    End With
  End If
Next sheetIndex
MsgBox "运行结束"

End Sub

达到预期效果,代码放这里,以后随时取用。

posted @ 2025-10-15 04:35  孤独的小苗  阅读(7)  评论(0)    收藏  举报