Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何在下面给定的代码中重新对数组进行尺寸标注_Excel_Vba - Fatal编程技术网

Excel 如何在下面给定的代码中重新对数组进行尺寸标注

Excel 如何在下面给定的代码中重新对数组进行尺寸标注,excel,vba,Excel,Vba,我需要按如下所示扩展阵列。 搜索答案,但似乎没有帮助如下代码 Sub MakeOneColumn() Dim vaCells As Variant Dim vOutput() As Variant Dim i As Long, j As Long Dim lRow As Long If TypeName(Selection) = "Range" Then If Selection.Count > 1 Then If Selection.Count <= S

我需要按如下所示扩展阵列。 搜索答案,但似乎没有帮助如下代码

Sub MakeOneColumn()

Dim vaCells As Variant
Dim vOutput() As Variant
Dim i As Long, j As Long
Dim lRow As Long

If TypeName(Selection) = "Range" Then
    If Selection.Count > 1 Then
        If Selection.Count <= Selection.Parent.Rows.Count Then
            vaCells = Selection.Value

            ReDim vOutput(1 To UBound(vaCells, 1) * UBound(vaCells, 2), 1 To 1)

            For j = LBound(vaCells, 2) To UBound(vaCells, 2)
                For i = LBound(vaCells, 1) To UBound(vaCells, 1)
                    If Len(vaCells(i, j)) > 0 Then
                        lRow = lRow + 1
                        vOutput(lRow, 1) = vaCells(i, j)
                    End If
                Next i
                lRow = lRow + 1
            Next j

            Selection.ClearContents
            Selection.Cells(1).Resize(lRow).Value = vOutput
        End If
    End If
End If

End Sub
子MakeOneColumn()
变暗真空管
Dim vOutput()作为变量
我和我一样长,我和我一样长
暗淡的光线和长的一样
如果TypeName(选择)=“范围”,则
如果Selection.Count>1,则
如果选择。则计数为0
lRow=lRow+1
vOutput(lRow,1)=真空室(i,j)
如果结束
接下来我
lRow=lRow+1
下一个j
选择.ClearContents
Selection.Cells(1).调整大小(lRow).Value=vOutput
如果结束
如果结束
如果结束
端接头

上述代码在没有添加行“lRow=lRow+1”的情况下工作。但是,数组中的每一列都需要一个空行。添加行后,我得到一个运行时错误9,下标超出范围。

之所以得到错误,是因为您重复lrow两次,一次在
I
循环中,一次在
j
循环中。如果选中,您应该会发现,只有当选择中的所有单元格中都有值时,才会发生错误

在j和i循环之外,将初始值设置为
lrow
,然后在将当前单元格的值分配给
vOutput
后进行迭代。看起来是这样的:

  lRow = 1
  For j = LBound(vaCells, 2) To UBound(vaCells, 2)
      For i = LBound(vaCells, 1) To UBound(vaCells, 1)
          If Len(vaCells(i, j)) > 0 Then
              vOutput(lRow, 1) = vaCells(i, j)
              lRow = lRow + 1
          End If
      Next i
  Next j

顺便说一句,我想指出,您不需要进行
TypeName
测试,因为选择总是类型范围。

您得到错误是因为您重复lrow两次,一次在
I
循环中,一次在
j
循环中。如果选中,您应该会发现,只有当选择中的所有单元格中都有值时,才会发生错误

在j和i循环之外,将初始值设置为
lrow
,然后在将当前单元格的值分配给
vOutput
后进行迭代。看起来是这样的:

  lRow = 1
  For j = LBound(vaCells, 2) To UBound(vaCells, 2)
      For i = LBound(vaCells, 1) To UBound(vaCells, 1)
          If Len(vaCells(i, j)) > 0 Then
              vOutput(lRow, 1) = vaCells(i, j)
              lRow = lRow + 1
          End If
      Next i
  Next j

顺便说一句,我想指出的是,您不需要进行
TypeName
测试,因为选择的内容始终是类型范围。

请将您的redim语句更改为下面的内容

  ReDim vOutput(1 To (UBound(vaCells, 1) * UBound(vaCells, 2)) + UBound(vaCells, 2), 1 To 1)

请将您的redim声明更改为以下内容

  ReDim vOutput(1 To (UBound(vaCells, 1) * UBound(vaCells, 2)) + UBound(vaCells, 2), 1 To 1)

我试着运行代码,在我这方面效果很好。我正在使用Excel2007。你能告诉我你使用的是哪个版本的excel吗?您还将提供工作表的屏幕截图,以便我可以模拟问题。@user2063626,我发现当选择的所有单元格中都有值时,代码失败。这是因为lrow.+1的双重迭代使用变量来循环而不是范围。我试着运行代码,在我结束时效果很好。我正在使用Excel2007。你能告诉我你使用的是哪个版本的excel吗?您还将提供工作表的屏幕截图,以便我可以模拟问题。@user2063626,我发现当选择的所有单元格中都有值时,代码失败。这是由于lrow.+1使用变量循环而不是范围的双重迭代造成的。@user2081581如果同意解决方案,请接受/投票。@user2081581如果同意解决方案,请接受/投票。