VBA中的worksheetfunction.match属性困难

时间:2018-07-24 03:29:38

标签: excel vba excel-vba runtime-error match

当我的宏到达该行时,我不断收到错误“运行时错误'91':“未设置对象变量或带块变量”

matchrange = Workbooks("tracker test").Sheets(start_sheet).Range("F" & h).Value

在这里我试图定义将与命名范围的PL_compare_list比较的PLnumber。如果我尝试不定义该变量,而是直接将引用直接放入下面一行中的我的match函数中,则会收到错误“运行时错误'1004':无法获取WorksheetFunction类的Match属性”

我想做的是让这段代码在start_sheet的H列上查看是否有数据。然后,如果没有,则将F列中start_sheet上的PL编号与B列中“计算表”上的PL编号进行比较以找到一行,然后打开该行A列中的相应文件名。有想法吗?

这是我的全部代码,但我认为最相关的部分将接近底部:

Option Explicit


Sub GetFileNames()

Range("A1").Select
ActiveCell.FormulaR1C1 = _
"=REPLACE(CELL(""filename""),FIND(""["",CELL(""filename"")),LEN(CELL(""filename"")),MID(CELL(""filename""),FIND(""]"",CELL(""filename""),1)+1,255))&""_samples shipment PO_PL_Invoice_ attachment\""&TRIM(MID(CELL(""filename""),FIND(""]"",CELL(""filename""),1)+1,255))&""_PL\"""
Range("B1").Select
ActiveCell.FormulaR1C1 = _
"=left(RC[-1],len(RC[-1])-10)"

Range("A1:B1").Select
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Dim directory As String
    directory = Range("B1").Value


Dim start_sheet As String
start_sheet = ActiveSheet.Name
Sheets("Calculation Sheet").Activate

Range("D1") = Sheets(start_sheet).Range("A1").Value

Columns("B:B").Select
Application.CutCopyMode = False
Selection.ClearContents

ActiveSheet.Cells(1, 1).Select

Dim xRow As Long
Dim xDirect$, xFname$, InitialFoldr$

InitialFoldr$ = directory
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a folder to list Files from"
.InitialFileName = InitialFoldr$
.Show
If .SelectedItems.Count <> 0 Then
xDirect$ = .SelectedItems(1) & "\"
xFname$ = Dir(xDirect$, 7)
Do While xFname$ <> ""
ActiveCell.Offset(xRow) = xFname$
xRow = xRow + 1
xFname$ = Dir
Loop
End If
End With

Dim i As Integer
Dim j As Integer
Dim filenumber As Integer
filenumber = Evaluate("CountA(A:A)")

Columns("A:A").Select
Selection.NumberFormat = "@"
j = 1
For i = 1 To filenumber
    If InStr(1, Range("A" & i), "xlsx") Then
    ActiveSheet.Range("B" & j).Value = ActiveSheet.Range("D1").Value & ActiveSheet.Range("A" & i).Value
    j = j + 1
    End If
Next i

    Columns("B:B").Select
    Selection.Copy
    Columns("A:A").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Columns("B:E").Select
    Application.CutCopyMode = False
    Selection.ClearContents

Dim xlfilenumber As Integer
Dim PL_list_length As Integer

xlfilenumber = Evaluate("CountA(A:A)")
ActiveSheet.Range("A1:A" & xlfilenumber).Select
Selection.Name = "list_of_files"

For i = 1 To xlfilenumber
    Range("B" & i).Select
        ActiveCell.FormulaR1C1 = _
        "=MID(RC[-1],FIND(""_PL"",RC[-1],FIND(""_PL\"",RC[-1],1)+4)+1,7)"
Next i

xlfilenumber = Evaluate("CountA(B:B)")
ActiveSheet.Range("A1:A" & xlfilenumber).Select
Selection.Name = "PL_compare_list"

Sheets(start_sheet).Activate
PL_list_length = Evaluate("CountA(F:F)") - 1



Dim h As Integer
Dim g As Integer
Dim filerownum As Integer
Dim matchrange As Range
Dim comparerange As Range
Dim filename As String

    For h = 6 To 9
            If IsEmpty(Range("J" & h)) Then
                matchrange = Workbooks("tracker test").Sheets(start_sheet).Range("F" & h).Value
                filerownum = Application.WorksheetFunction.Match(matchrange, Worksheets("Calculation Sheet").Range("PL_compare_list"), 0)
                    filename = Range("A" & filerownum).Value
                    Workbooks.Open filename
            End If

    Next h



Workbooks("tracker test").Sheets(start_sheet).Activate

If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then
  ActiveSheet.ShowAllData
End If


ActiveSheet.Cells(1, 1).Select
Application.CutCopyMode = False
Selection.ClearContents

ActiveSheet.Cells(1, 2).Select
Application.CutCopyMode = False
Selection.ClearContents

ActiveSheet.Cells(1, 3).Select
Application.CutCopyMode = False
Selection.ClearContents

End Sub

1 个答案:

答案 0 :(得分:0)

理想情况下,您还应该考虑很多其他变化,但是要解决如何处理失败的比赛的问题:

from bs4 import BeautifulSoup as Soup
import pandas

xml = """
<?xml version="1.0"?><results>
<header>
  <cloc_url>github.com/AlDanial/cloc</cloc_url>
  <cloc_version>1.74</cloc_version>
  <elapsed_seconds>0.940369129180908</elapsed_seconds>
  <n_files>124</n_files>
  <n_lines>8440</n_lines>
  <files_per_second>131.863112209998</files_per_second>
  <lines_per_second>8975.19892784178</lines_per_second>
  <report_file>/Users/hariomsingh/Desktop/ignitechute/Repo/ignite-chute-aem_cloc.xml</report_file>
</header>
<files>
  <file name="/Users/hariomsingh/Desktop/ignitechute/Repo/ignite-chute-aem/aem-parent/pom.xml" blank="13" comment="23" code="491"  language="Maven" />
  <file name="/Users/hariomsingh/Desktop/ignitechute/Repo/ignite-chute-aem/aem-core/aem-core-bundle/src/test/resources/assets.json" blank="0" comment="0" code="357"  language="JSON" />
  <file name="/Users/hariomsingh/Desktop/ignitechute/Repo/ignite-chute-aem/aem-core/aem-core-bundle/src/main/java/com/chute/aem/core/api/impl/UserServiceImpl.java" blank="26" comment="21" code="202"  language="Java" />
"""

soup = Soup(xml, 'lxml')

records = []

for file in soup.findAll('file'):
    records.append(file.attrs)

data_table = pandas.DataFrame(records)

# this prints the table without the long file name to ease seeing all other fields
print(data_table.drop('name', axis=1))

# this prints just the names (or at least the bit that pandas prints by default)
print(data_table['name'])

# saving them to disk so you can see the entire table in excel or similar
data_table.to_csv('output.csv', index=False)