以下代码单独在项目中时可以正常工作,但是一旦将其放入包含所有代码的项目中,我就会得到一个“ ByRef argument type mismatch error
”。我不知道为什么会这样。
它在以下行上中断:If Left(midText, 2) = "|-" Then
我没有在项目中的任何其他代码中引用midText。
Function ColumnHelper(midText As String, inBold As Boolean, _
inColor As Integer, inStrikeThru As Boolean)
Selection.NumberFormat = "General"
Selection.HorizontalAlignment = xlGeneral
If Selection.Count > 1 Then
'this performs a "softcenter"
Selection.HorizontalAlignment = xlCenterAcrossSelection
Else
Selection.HorizontalAlignment = xlCenter
End If
Dim dashCount As Integer
Dim tempText As String
If Left(midText, 2) = "|-" Then
On Error Resume Next 'count dashes before space
For i = 2 To Len(midText) - 1
If Mid(midText, i, 1) = "-" Then
dashCount = dashCount + 1
Else
Exit For
End If
Next i
tempText = Mid(midText, dashCount + 3, Len(midText) _
- (2 * (dashCount + 2)))
If Err = 0 Then
midText = tempText
End If
End If
Dim selWidth As Integer 'Get the width of the selection
For Each cell In Selection.Rows(1)
selWidth = selWidth + cell.Width
Next cell
Dim dashes As String 'Create a string containing dashes
dashes = "-"
For i = 0 To CInt(selWidth / 6) - (0.9 * Len(midText)) - 5
dashes = dashes + "-"
Next i
Dim dashLen As Integer
dashLen = Len(dashes)
Selection.Item(1).FormulaR1C1 = "|" + dashes + " " + midText + _
" " + dashes + "|" 'Input text into cell
With Selection.Item(1).Font
.Name = "System"
.FontStyle = "Bold"
.Size = 8
.ColorIndex = inColor
.Strikethrough = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlNone
End With
With Selection.Item(1).Characters(Start:=Len(dashes) + 2, _
Length:=Len(midText) + 2).Font
.Name = "Arial"
If inBold Then
.FontStyle = "Bold"
Else
.FontStyle = "Regular"
End If
.Strikethrough = inStrikeThru
End With
End Function
答案 0 :(得分:0)
一个示例调用会有所帮助,但以下是一些观察结果:
1)这不是功能。它可以执行操作,但不返回任何内容。考虑是否实际需要返回任何内容(在这种情况下将是一个函数,并且需要一个赋值,并且签名应指示返回类型)。
2)它修改了midText
的值,因此您可以显式传递ByRef
。
3)您有未声明的变量(例如i, Cell)
,这意味着您不应该在模块顶部使用Option Explicit
。
4)您有一个未公开的On Error Resume Next
,掩盖了任何潜在的错误。
5)使用有意义的变量名和不明确的变量,例如不要使用Cell
作为变量名。
6)使用Long
而非Integer
来减少潜在的溢出风险。
7)您可以使用更高效的类型化函数,例如Mid$
8)您对dashLen
不做任何事情。可以将其删除。
9)您可以将其分解为处理单个任务的较小子功能吗?
其中一些建议:
Option Explicit
Public Sub test()
ColumnHelper "HELLO", True, 2, True
End Sub
Public Sub ColumnHelper(ByRef midText As String, ByVal inBold As Boolean, ByVal inColor As Long, ByVal inStrikeThru As Boolean)
Selection.NumberFormat = "General"
Selection.HorizontalAlignment = xlGeneral
If Selection.Count > 1 Then
Selection.HorizontalAlignment = xlCenterAcrossSelection
Else
Selection.HorizontalAlignment = xlCenter
End If
Dim dashCount As Long, tempText As String, i As Long
If Left$(midText, 2) = "|-" Then
For i = 2 To Len(midText) - 1
If Mid$(midText, i, 1) = "-" Then
dashCount = dashCount + 1
Else
Exit For
End If
Next i
tempText = Mid$(midText, dashCount + 3, Len(midText) - (2 * (dashCount + 2)))
If Err = 0 Then
midText = tempText
End If
End If
Dim selWidth As Long, iCell As Range
For Each iCell In Selection.Rows(1)
selWidth = selWidth + iCell.Width
Next iCell
Dim dashes As String
dashes = "-"
For i = 0 To CInt(selWidth / 6) - (0.9 * Len(midText)) - 5
dashes = dashes + "-"
Next i
Dim dashLen As Long
dashLen = Len(dashes)
Selection.Item(1).FormulaR1C1 = "|" + dashes + " " + midText + _
" " + dashes + "|"
With Selection.Item(1).Font
.Name = "System"
.FontStyle = "Bold"
.Size = 8
.ColorIndex = inColor
.Strikethrough = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlNone
End With
With Selection.Item(1).Characters(Start:=Len(dashes) + 2, Length:=Len(midText) + 2).Font
.Name = "Arial"
If inBold Then
.FontStyle = "Bold"
Else
.FontStyle = "Regular"
End If
.Strikethrough = inStrikeThru
End With
End Sub