从默认表大小VBA Excel中select所有数据

我有一个电子表格的默认表格大小和布局是由另一个电子表格的信息填充。 该表总是具有相同的列数,但行中的条目数量可能会有所不同。 我想从表中select所有数据,并将其粘贴到另一个表中,而不复制任何空行。

我最初的尝试涉及以下代码:

Set rightcell = Range("B9").End(x1Right) Set bottomcell = Range(rightcell).End(x1Down) 

要定义右下angular应该是什么,所以我可以像这样引用整个表格:

 Range("B9", bottomcell).Select 

或复制或其他。 当我运行这个,它给了我一个“用户定义或对象定义的错误”,我不知道为什么。 我把代码input为一个更大的子的一部分,我已经定义了我的variables作为范围和变体,试图让这个工作。 我花了相当多的时间在互联网上寻找解决scheme,但到目前为止,我发现的信息并没有明确地涉及到我的问题,也没有类似的解决scheme。

有谁知道这是什么适当的编码,或者如果我正在做一些小的错误,抛出一切closures? 我记得在大学的一个项目中遇到同样的问题,但在我的生活中,我不记得解决办法。 这很令人沮丧。

另外,如果我太模糊或者需要更多的解释,请不要犹豫。 先谢谢您的帮助!

编辑:我忽略了一个重要的注意事项是,我想从中提取数据的表格是在与多个其他表格的中间,我不想与之交互。

如果表格总是位于表格上的相同位置,则可以这样做来复制整个表格:

 'Modify this to any cell in your table (like the top left hand cell): Range("B9").CurrentRegion.Copy Sheets("TheSheetYouWantToPasteTo").Range("A1") 

即使表格在工作表上的位置发生了变化,只要您知道表格中的某个单元格,仍然可以使用上述代码复制表格。

如果您想保持与您尝试相同的方法,请尝试以下操作:

 Dim rightcell As Long Dim bottomcell As Long 'Finds the furthest column to the right: rightcell = Cells(5, Columns.Count).End(xlToLeft).Column 'Finds the bottom most row in the table (will stop at the first non-blank cell it finds.) bottomcell = Range("B:B").Find("*", Range("B9"), searchdirection:=xlPrevious).Row 'Reference the variables like this: Range(Cells(9, 2), Cells(bottomcell, rightcell)).copy _ Sheets("TheSheetYouWantToPasteTo").Range("A1") 

这是我使用的

 Public Function last_row() As Long Dim i As Integer Dim l_row As Long 'my sheet has 35 columns change this number to fit your For i = 1 To 35 If Sheet1.Cells(Rows.Count, i).End(xlUp).Row > l_row Then l_row = Sheet1.Cells(Rows.Count, i).End(xlUp).Row End If Next i last_row = l_row End Function 

然后使用

 Dim l_row As Long l_row = last_row 'Again since you know the last column change 35 here to your value 'or use the String ie "AI" Range("B9", Cells(l_row,35)).Select 

这将查看每列以确定包含数据的最后一行