在VBA UDF中键入不匹配错误

时间:2014-07-08 01:13:59

标签: excel excel-vba vba

我的代码出现“类型不匹配”错误,我不确定它的来源。精彩的VBA编辑器并没有告诉我它在哪里,我想知道我是否能得到一些帮助来追踪它。这是我的代码:

Private Sub refresh_Click()

Dim random As Integer
Dim passedSheet As Worksheets
    Set passedSheet = Sheets("Org Chart - JC")
Dim inputSheet As Worksheets
    Set inputSheet = Sheets("Analytics - JC")


Application.ScreenUpdating = False

random = createHistogram(Sheets("Org Chart - JC").Range("A2:A1000"), 3, 1, passedSheet, inputSheet)
random = createHistogram(Sheets("Org Chart - JC").Range("C2:C1000"), 3, 4, passedSheet, inputSheet)
random = createHistogram(Sheets("Org Chart - JC").Range("D2:D1000"), 3, 7, passedSheet, inputSheet)
random = createHistogram(Sheets("Org Chart - JC").Range("E2:E1000"), 3, 10, passedSheet, inputSheet)
random = createHistogram(Sheets("Org Chart - JC").Range("F2:F1000"), 3, 13, passedSheet, inputSheet)
random = createHistogram(Sheets("Org Chart - JC").Range("H2:H1000"), 3, 16, passedSheet, inputSheet)

'Format the width of all columns'
With Range("A:Q")
    .Select
    .EntireColumn.AutoFit
End With

With Range("C1, F1, I1, L1, O1").EntireColumn
    .ColumnWidth = 2
End With

Range("A1").Select

Application.ScreenUpdating = True

End Sub

这是createHistogram的功能:

Function createHistogram(rng As Range, pasteRow As Integer, pasteColumn As Integer, passedSheet As Worksheets, inputSheet As Worksheets)

Dim rngCount As Integer
Dim uniqueResultsCount As Integer

'Get info from "passedSheet" sheet'
passedSheet.Select
rngCount = WorksheetFunction.CountA(rng)
rng.Select
Selection.Copy
inputSheet.Select
Cells(pasteRow, pasteColumn).Select
ActiveSheet.Paste
Application.CutCopyMode = False

'Remove duplicates leaving unique values and count remaining values'
ActiveSheet.Range(Cells(pasteRow, pasteColumn), Cells(pasteRow + 200, pasteColumn)).RemoveDuplicates Columns:=1
uniqueResultsCount = WorksheetFunction.CountA(Range(Cells(pasteRow, pasteColumn), Cells(pasteRow + 200, pasteColumn)))
'    MsgBox ("uniqueResultsCounter has a value of: " & uniqueResultsCount)

'Count all of the existing values in the "passedSheet" sheet'
For i = 1 To uniqueResultsCount
    Cells(pasteRow - 1 + i, pasteColumn + 1).Value = _
        WorksheetFunction.CountIf(rng, Cells(pasteRow - 1 + i, pasteColumn))
Next i

'Clear existing formating for selection'
With Range(Cells(pasteRow, pasteColumn), Cells(pasteRow + uniqueResultsCount + 200, pasteColumn + 1))
    .Interior.ColorIndex = 0
    .Borders.LineStyle = xlNone
    .Font.Bold = xlFalse
End With

'Create new formatting for selection'
With Range(Cells(pasteRow, pasteColumn), Cells(pasteRow + uniqueResultsCount - 1, pasteColumn + 1))
    .Interior.ColorIndex = 0
    .Borders.LineStyle = xlContinuous
    .Font.Bold = xlFalse
End With
With Range(Cells(pasteRow, pasteColumn), Cells(pasteRow + uniqueResultsCount - 1, pasteColumn + 1))
    .HorizontalAlignment = xlCenter
End With

'Sort the column from A to Z'
Range(Cells(pasteRow, pasteColumn), Cells(pasteRow + uniqueResultsCount - 1, pasteColumn + 1)).Sort _
    key1:=Cells(pasteRow, pasteColumn)

createHistogram = 1

End Function

我知道我的一些变量名称不是最好的,但是没有人应该看到这个代码。

我认为主要错误在于passedSheetinputSheet变量。我仍然习惯了VBA,我无法理解工作表和工作表之间的区别是什么以及何时使用它们。

再次感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我认为这是因为你正在制作工作表而不是工作表。可能不是答案,但试试看,让我知道。