寻找在Excel中select一个未确定数量的行作为更大的VBAmacros的一部分

我正在用一本包含大量表格的excel书籍工作; 第一张纸连接到外部程序并通过外部function拉入数据,并且导入的行数变化很大。

这个块数据是通过一些后续的工作表传播的。 第一步是用表1中的行数填充列A(行名)。从这里数据被分割成多个列(当前B-> L)。 最上面的一行使用IF()函数来填充第一行,我正在寻找一个干净的macros来将这个公式复制到第x行(每个数据导入刷新都不相同),然后粘贴一个可pipe理文件大小的值。

这是我迄今为止所得到的; 它工作,但它是相当(阅读:非常!)笨拙:

Sub Refresh_Data() Sheets("Sheet2").Select ActiveWindow.ScrollWorkbookTabs Sheets:=13 Sheets(Array("Sheet2" ... "Sheet25")).Select Sheets("Sheet2").Activate Sheets("Sheet25").Select Replace:=False Range("B1:L1").Select Selection.Copy Range("__B2:B1000__").Select ActiveSheet.Paste Application.Calculate ActiveWindow.ScrollWorkbookTabs Position:=xlFirst Sheets(Array("Sheet2" ... "Sheet25")).Select Sheets("Sheet2").Activate Sheets("Sheet25").Select Replace:=False Sheets("Sheet2").Select Range("B3").Select Sheets(Array("Sheet2" ... "Sheet25")).Select Sheets("Sheet2").Activate Sheets("Sheet25").Select Replace:=False Range("B3:L4").Select Range("__B2:L1000__").Select Application.CutCopyMode = False Selection.Copy Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Sheets("Check_sheet").Select MsgBox "Update complete" End Sub` 

我期望实现的主要目标是将代码B2:L1000replace为可以评估列A中的行数并相应地select行B到L中的范围的内容。

由于列L是最后一个填充列,我不明白为什么这不能也是水平完成,而不是定义“B:L”incase未来列需要添加。

虽然较早的答案有以下优点:

1)我不会使用COUNTA,因为如果在行或列中有空单元格,那么底部或右边的单元格将被忽略。

2)我永远不会依赖用户在运行macros之前select正确的表单来使用; 特别是有这么多床单的人。

我对这个问题的反应是,你已经设置了macroslogging,在你的工作簿中漫游,然后停止logging。 你select一个,然后select另一个。 您滚动浏览表单。 对我来说,大部分的陈述并不笨拙,毫无意义。

以下内容确实包含了有关查找列A最后一行的问题的答案,但它更多的是关于如何查找范围的维度,如何从范围中获取数据然后将其放在其他地方的教程。 这似乎是你正在尝试做的最基本的VBA理解的大部分。 如果这个批评是不公平的,但是这是你的问题给我的印象。

 Sub Test() Dim RowS01Max As Integer Dim Sheet1Data() As Variant ' With Sheets("Sheet1") allows you to access data within worksheet Sheet1 ' without selecting it. ' Range("A1:C11") refers to a range within the active sheet ' .Range("A1:C11") refers to a range within the sheet identified in the ' With statement. ' ^ Note the dot With Sheets("Sheet1") ' Rows.Count is the number of rows for the version of Excel you are using. ' .Cells(Rows.Count, "A") address the bottom row of column A of worksheet ' Sheet1. ' .Cells(Rows.Count, 1) refer to column A by number. ' End(xlUp) is the VBA equivalent of Ctrl+Up. ' If you positioned the cursor at the bottom of column A and pressed ' Ctrl+Up, the cursor would jump to the last row in column A with a value. ' The following statement gets that row number without actually moving ' the cursor. RowS01Max = .Cells(Rows.Count, "A").End(xlUp) ' The following statement loads the contents of range A1:C11 of ' Sheets("Sheet1") into array Sheet1Data. Sheet1Data = .Range("A1:C11").Value ' This is the same statement but the range is specified in a different way. ' .Cells(Row,Column) identifies a single cell within the sheet specified in ' the With statement. .Cells(1,1) identifies row 1, column 1 which is A1. '. Cells(11, "C") identifies row 11, column C which is C11. Sheet1Data = .Range(.Cells(1, 1), .Cells(11, "C")).Value ' This statement uses RowS01Max to specify the last row Sheet1Data = .Range(.Cells(1, 1), .Cells(RowS01Max, 1)).Value ' In all three examples above, the contents of the specified range will ' be loaded to array Sheet1Data. Whichever range you pick, Sheet1Data ' will always be a two dimensional array with the first dimension being ' the row and the second dimension being the column. ' In the first two examples Sheet1Data(5,3) contains the contents ' of cell C5. In the third example, I have only loaded column A but the ' array will still has two dimensions but the only permitted value for the ' second dimension is 1. ' The following statement writes the contents of Sheet1Data to column "E" .Range(.Cells(1, 5), .Cells(RowS01Max, 5)).Value = Sheet1Data End With With Sheets("Sheet2") ' The following statement writes the contents of Sheet1Data to column "E" ' of worksheet Sheet2. .Range(.Cells(1, 5), .Cells(RowS01Max, 5)).Value = Sheet1Data End With End Sub 

不要绝望! 我们大多数人开始使用macroslogging器,仍然使用它来发现不熟悉命令的语法。 看看其他问题。 有些人会问一些奇特的function,但是很多都是关于如何将数据转移到有经验的程序员的简单方法。 设置与提问者的问题一些工作簿。 将解决scheme复制并粘贴到模块中。 使用F8(参见debugging程序),在Excel和Editor之间切换,观察工作表中发生了什么,并将光标移到variables上以查看其当前值。 花半天时间玩。 你会惊讶于它开始有意义的速度。 祝你好运,编程好。

以下应该做的伎俩:

 Sub Refresh_Data() Dim lastRow As Integer Dim lastCol As Integer Dim entireRange As Range Dim targetRange As Range lastRow = Excel.Evaluate("COUNTA(A:A)") ''// count the rows in column A lastCol = Excel.Evaluate("COUNTA(1:1)") ''// count the columns in row 1 Set entireRange = Range(Cells(1, 2), Cells(lastRow, lastCol)) Set targetRange = Range(Cells(2, 2), Cells(lastRow, lastCol)) entireRange.FillDown Application.Calculate targetRange.Copy targetRange.PasteSpecial Paste:=xlPasteValues End Sub 

笔记:

Excel.Evaluate(...)允许您在VBAmacros中使用工作表函数的结果。

COUNTA(range)是一个工作表函数,用于计算给定范围内的非空白单元格的数量。 在这种情况下,可以使用它来确定数据集中的总行数,以及第1行中具有公式的列数。