代码为userform(微调)返回types不匹配,如果select大量的单元格。 VBA Excel

我明白,types不匹配通常是当你错误地定义一个variables,但我不确定在这种情况下是否合理。 我的代码,下面贴出来,是一个用户窗体上有一个微调,你点击用户窗体之前突出显示的单元格增加/减less指定的百分比,当你点击微调button。

这适用于较less的单元格。 所以我可以select5个不同范围的4个单元格中的每个例如,它将按预期行事,但是当我select比这更多时,我得到一个types不匹配错误,当我使用微调和debugging器突出显示singlecell.Value = singlecell.Value *在下面的pvar代码。 这似乎只影响select的每个范围的最后几行。

有谁知道这是为什么或如何纠正这种情况? 由于我的知识有限,似乎不是完全有意义的,因为types不匹配错误。 我唯一的假设是,当定义AS范围时,可以存储范围的数量是有限制的? 请参阅下面的代码:

'打开sinner userform的代码

Public SelRange As Range Public pvar As Double Public SelVar As Double Public InitVar As Double Public GetAllValuesAtOnceAsArray As Variant Sub Button2_Click() Spinner.Show End Sub 

'用户表单代码'

 Option Explicit 'on opening userform this sets the variables Private Sub UserForm_Activate() pvar = 1 Set SelRange = Selection GetAllValuesAtOnceAsArray = SelRange.Value End Sub 'button to maintain adjusted values Private Sub CommandButton1_Click() UserForm3.Show End Sub 'Button to return to starting values Private Sub DefaultButton_Click() Dim singlecell As Range 'write back the original values SelRange.Value = GetAllValuesAtOnceAsArray 'Adjust every single Cell within range pvar = 1 End Sub 'Spin Up button Private Sub SpinButton1_SpinUp() Application.ScreenUpdating = False pvar = pvar + UpBox.Value / 100 'write back the original values SelRange.Value = GetAllValuesAtOnceAsArray Dim singlecell As Range 'Adjust every single Cell within range For Each singlecell In SelRange.Cells singlecell.Value = singlecell.Value * pvar Next singlecell Application.ScreenUpdating = True End Sub ' Spin Down button Private Sub SpinButton1_SpinDown() pvar = pvar - DownBox.Value / 100 'write back the original values SelRange.Value = GetAllValuesAtOnceAsArray Dim singlecell As Range 'Adjust every single Cell within range For Each singlecell In SelRange.Cells singlecell.Value = singlecell.Value * pvar Next singlecell End Sub ' Reset values when closing userform unless specified otherwise Private Sub UserForm_terminate() 'Now write back the original values SelRange.Value = GetAllValuesAtOnceAsArray End Sub 

你提到你“select了5个不同的4个单元格”,这似乎表明你正在select不连续的范围。 如果是这种情况,那么也许真正的问题是你试图存储的方式,然后恢复variables数组范围内的值。 这种方式不行。 考虑下面的子文件:

 Sub test1() Dim myRange As Range, myCopy As Variant Set myRange = Selection myCopy = myRange.Value myRange.ClearContents 'now restore: myRange.Value = myCopy 'doesn't always work! End Sub 

用不同的值填充范围A1:B2和D1:E4,然后先selectA1:B2,然后selectD1:E4,同时select它们。 调用子。 您应该看D3#E4范围内的#N / A – 这是导致实际types不匹配的原因。

问题是不连续的范围是区域的集合,只有第一个区域被Value属性占用。 如果你真的想要存储,然后恢复值,你可以做如下的事情:

 Sub test2() Dim myRange As Range, myArea As Range Dim myCopy As Variant Dim i As Long, numAreas As Long Set myRange = Selection numAreas = myRange.Areas.Count If numAreas = 1 Then myCopy = myRange.Value Else ReDim myCopy(1 To numAreas) For i = 1 To numAreas myCopy(i) = myRange.Areas(i).Value Next i End If myRange.ClearContents 'now restore: If numAreas = 1 Then myRange.Value = myCopy Else For i = 1 To numAreas myRange.Areas(i).Value = myCopy(i) Next i End If End Sub 

在你的情况下,你可能会想要myCopy和numAreas模块级别的variables。 既可以将用于复制值的代码和用于还原值的代码移动到subs,也可以将用于将范围内的每个单元格乘以一个值的代码(对spinup和spindown都有用)。这由以下3个子节点说明,接下来是一个testing小组,以显示子电话的工作方式:

 Sub CopyVals(R As Range, V As Variant) Dim A As Range Dim i As Long, n As Long n = R.Areas.Count If n = 1 Then V = R.Value Else ReDim V(1 To n) For i = 1 To n V(i) = R.Areas(i).Value Next i End If End Sub Sub RestoreVals(R As Range, V As Variant) Dim A As Range Dim i As Long, n As Long n = R.Areas.Count If n = 1 Then R.Value = V Else For i = 1 To n R.Areas(i).Value = V(i) Next i End If End Sub Sub Multiply(R As Range, p As Double) Dim c As Range For Each c In R.Cells c.Value = p * c.Value Next c End Sub Sub test() Dim myRange As Range, myCopy As Variant Dim pvar As Double Set myRange = Selection CopyVals myRange, myCopy pvar = 0.9 Multiply myRange, pvar pvar = 1.1 RestoreVals myRange, myCopy Multiply myRange, pvar End Sub 

请注意,test()的最后两行不能折叠为

 RestoreVals myRange, myCopy * pvar 

因为这将涉及一个数组时间双重types不匹配