隐藏参考单元格时宏不起作用

时间:2015-07-16 09:15:07

标签: excel vba excel-vba

我正在使用此VBA宏但注意到一旦隐藏了单元格P7P8引用(隐藏了列P),宏就无效了。可能出现什么问题?

如果P列显示在Sheet12上,它可以正常工作。 顺便说一句,单元P8使用了一个作业号(例如15123),因此命令Dir可以找到保存工作簿的正确文件夹。 P7的类别基于工号(例如15100)。

Sub Save_As()

'
Dim JobCat As String, JobNumber As String, JobNumberName As String
JobCat = Sheet12.Range("P7").Text
JobNumber = Sheet12.Range("P8").Text
JobNumberName = Dir("F:\Client Documents\" & JobCat & "\" & JobNumber & "*", vbDirectory)

If JobNumberName <> "" Then
    ActiveWorkbook.SaveAs Filename:= _
        "F:\Client Documents\" & JobCat & "\" & JobNumberName & "\Scheduling\" & JobNumber & "_Take off.xlsm" _
        , FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End If
End Sub

2 个答案:

答案 0 :(得分:0)

将您的.text更改为.value2&#39;

Public WithEvents A As Excel.Application

Private Sub A_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
If Workbooks.Count > 1 Then
    If Windows(ThisWorkbook.Name).Visible = False Then
        If Wb.Name = ThisWorkbook.Name Then
            Cancel = True
        End If
    End If
End If
End Sub

答案 1 :(得分:0)

@ 99moorem,感谢您的帮助,经过几个小时的重新调整和测试,这是我的最终剧本:

Sub Save_As()

'
Dim JobCat As String, JobNumber As String, JobNumberName As String, FilePath As String, answer As Integer
JobCat = Sheet12.Range("P7").Value2
JobNumber = Sheet12.Range("P8").Value2
JobNumberName = Dir("C:\test\" & JobCat & "\" & JobNumber & "*", vbDirectory)
FilePath = "C:\test\" & JobCat & "\" & JobNumberName & "\Scheduling\"

If Dir(FilePath, vbDirectory) <> "" Then 'Check for folder
    If Dir(FilePath & JobNumber & "_Take off.xlsm") <> "" Then ' check for file
        'File already exists
        answer = MsgBox(JobNumber & "_Take off.xlsm" & "  Already exist" & Chr(10) & Chr(10) & "Overwrite Exsisting?", vbYesNo, "Confirm Overwrite")
        If answer = vbYes Then
            'save over
            Application.DisplayAlerts = False
            ActiveWorkbook.SaveAs Filename:= _
                FilePath & JobNumber & "_Take off.xlsm" _
                , FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
        Else
            Exit Sub
            'do nothing
        End If
    Else
        'does not exsist, so can create
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs Filename:= _
            FilePath & JobNumber & "_Take off.xlsm" _
            , FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    End If
Else
    MsgBox FilePath & Chr(10) & Chr(10) & " Do not exsist"
End If
End Sub

我必须保持JobNumberName = Dir("C:\test\" & JobCat & "\" & JobNumber & "*", vbDirectory)作为我面临的问题之一,在JobNumber之后,每个文件夹都有一个客户端名称,例如。 12345-John Smith这用于查找客户端名称。 到目前为止,工作正常,没有任何错误。再次感谢