VBA运行时错误' 1004'

时间:2017-01-06 15:33:13

标签: excel vba excel-vba macros

我已经完整地包含了我的宏,因为我觉得必须有一些我遗漏的小细节,这让我发疯了。该行是导致运行时错误的行。

        Workbooks("getVendorChecks.xlsm").Worksheets(Sheet1).Cells(m, 1) = agency

宏会浏览一系列文件夹并在每个文件夹中打开文件(只有一个文件夹),从文件中获取一些数据,然后将其粘贴到宏工作簿中。 agency是一个String变量,它包含文件夹名称,我也希望粘贴它,但由于某种原因,它会导致错误。

奇怪的是,它适用于第一个实例,但是当它尝试粘贴第二个文件夹名称时,会发生错误。我错过了什么?

Option Compare Text

Private Sub CommandButton1_Click()

Application.ScreenUpdating = False

Dim vendor As String, m As Integer

m = 4

'set vendor to user's choice and add wildcards
vendor = Cells(2, 5).Value
vendor = "*" & vendor & "*"

'clear the contents
Worksheets("Raw Data").Range("A4:Z100000").ClearContents

'pull releases from each media, passing the vendor chosen
enterHeldSheets "\CABLE\", vendor, m

End Sub
Sub enterHeldSheets(media As String, vendor As String, m As Integer)

Dim HostFolder As String, subFolder As Object, r As Integer
Dim agency As String, book As String, path As String
Dim FileSystem As Object, FileFolder As Object

HostFolder = Workbooks("getVendorChecks.xlsm").path & media

Set FileSystem = CreateObject("Scripting.FileSystemObject")
Set FileFolder = FileSystem.GetFolder(HostFolder)

'go through each subFolder
For Each subFolder In FileFolder.SubFolders

    agency = subFolder.name
    book = Dir(HostFolder & agency & "\*.xl*")
    path = HostFolder & agency & "\" & book
    Workbooks.Open (path)

    'handling S&C exception
    If agency = "Saatchi & Conill" Then
    m = pullChecks(agency, book, "SAA CHECKS", vendor, m)
    m = pullChecks(agency, book, "CNL CHECKS", vendor, m)
    Else

    m = pullChecks(agency, book, "CHECKS", vendor, m)
    End If

    Workbooks(book).Close False

Next subFolder

End Sub

Function pullChecks(agency As String, book As String, checkTab As String, vendor As String, m As Integer) As Integer

Dim column As String, i As Integer
Dim j As Integer, k As Integer, g As Integer
Dim checks() As String, count As Long, flag As String
Dim name As String, count2 As Long, compare As Boolean

    'remove filters
    If Workbooks(book).Worksheets(checkTab).FilterMode Then
        Workbooks(book).Worksheets(checkTab).ShowAllData
    End If

    'initializations
    flag = "False"
    k = 1
    i = 1
    g = 1

    'Finding the Vendor Name column
    Do While flag <> "True"
        column = Workbooks(book).Worksheets(checkTab).Cells(1, k).Value
        If column = "VENDOR ACCOUNT" Or column = "VENDOR NAME" Then
            flag = "True"
        Else
            flag = "False"
            k = k + 1
        End If
    Loop

    Workbooks(book).Worksheets(checkTab).Cells.RemoveSubtotal

   'resize checks()
    count = Workbooks(book).Worksheets(checkTab).Cells(Rows.count, k).End(xlUp).Row
    ReDim checks(1 To count, 1 To 13)

    'goes through each Vendor column entry looking to see if
    'it's what the user wanted. if yes, then print onto the workbook
    For j = 2 To count
        name = Workbooks(book).Worksheets(checkTab).Cells(j, k).Value
        compare = name Like vendor
        If compare Then
            Workbooks("getVendorChecks.xlsm").Worksheets("Raw Data").Cells(m, 1) = agency
            m = m + 1
        End If

    Next j

pullReleases = m
End Function

0 个答案:

没有答案