在打开excel而不是更改值时执行VBA代码

时间:2018-09-26 22:44:11

标签: excel vba excel-vba exception access-vba

我有一个Rota Sheet的VBA代码,该代码在行中的任何值更改时都会激活。

我希望在打开Excel时激活代码。

代码:

export const fetchUser = authToken => async dispatch => {
  console.log('dispatching auth token')

  console.log('here goes request')
  let res = await api.get(`/auth/${authToken}`);

  if (res.ok) {
    console.log('have the user')

    // Occurs after network request is complete    
    console.log('show login screen')

    // Add navigation here to go to logged in view now that request is complete
    loggedInView()

    dispatch(
      setUser(res.data)
    )
  } else {
    dispatch({
      type: 'SET_USER_DEFAULT'
    })
}

我通过以下方式进行了努力: 1.在工作簿中使用以下代码:这将引发424错误

Private Sub Worksheet_Change(ByVal Target As Range)

Dim rng As Range
Set rng = Range("B2:V11")

If Not Intersect(Target, rng) Is Nothing Then

    'scan each row (month)
    Dim countRow As Long

    Dim i As Long
    For i = 1 To rng.Rows.count
        If Not Intersect(Target, rng.Rows(i)) Is Nothing Then
            If WorksheetFunction.CountIf(rng.Rows(i), "V") > 0 Then
                countRow = 0

                Dim cel As Range
                For Each cel In rng.Rows(i).Cells
                    If cel.Value2 = "V" Then
                        countRow = countRow + 1
                        VacationChange cel, countRow
                    Else
                        VacationChange cel, 0
                    End If
                Next cel
            End If
        End If
    Next i

    'scan each column (day)
    Dim j As Long
    For j = 1 To rng.Columns.count
        If Not Intersect(Target, rng.Columns(j)) Is Nothing Then
            If WorksheetFunction.CountIf(rng.Columns(j), "V") > 5 Then
                VacationChange rng.Columns(j).Cells(0, 1), 6
            Else
                VacationChange rng.Columns(j).Cells(0, 1), 0
            End If
        End If
    Next j

End If
End Sub

Private Function VacationChange(ByVal rng As Range, ByVal count As Long)

With rng.Interior
    Select Case count
        Case 0
            'clear cell colors
            .Pattern = xlNone
            .Color = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        Case 1 To 3
            'blue
            .Pattern = xlSolid
            .Color = 15773696
            .TintAndShade = 0
            .PatternTintAndShade = 0
        Case 4 To 5
            'yellow
            .Pattern = xlSolid
            .ThemeColor = xlThemeColorAccent4
            .TintAndShade = 0.399975585192419
            .PatternTintAndShade = 0
        Case Else
            'red
            .Pattern = xlSolid
            .Color = 255
            .TintAndShade = 0
            .PatternTintAndShade = 0
    End Select
End With

End Function
  1. 在无法正常工作的Workbook_Open()函数下粘贴整个代码

有人可以建议我在代码中缺少什么吗?

样本输出图像已附加 enter image description here

1 个答案:

答案 0 :(得分:0)

问题在于,目标Variant在您的Workbook_Open实现中是未声明的Range。这意味着当将其作为需要为B2:V11的参数传递时,隐式强制转换失败并导致错误424(需要对象)。

如果您要“模拟”目标范围更改中的每个单元格,则可以简单地遍历Private Sub Workbook_Open() Sheet1.Activate Dim cell As Range For Each cell In Sheet1.Range("B2:V11") 'Worksheet_Change needs to be Public Sheet1.Worksheet_Change cell Next End Sub 并将其传递给每个单元格(未经测试的数据,但应该给出要点):

Worksheet_Change

请注意,这绝不是您要执行的操作的理想解决方案,它表明您需要稍微重构代码以将Worksheet_Change中当前具有的功能提取到独立程序。如果需要从{ "query": { "bool": { "filter": [ { "match": { "status": "publish" } }, { "match": { "authors": "104" } } ], "should": [ { "must_not": { "exists": { "field": "postMeta.byline" } } }, { "filter": { "wildcard": { "postMeta.byline": "*John Doe*" } } } ] } } } 处理程序运行相同的代码,则可以调用该过程。