Excel通过用户input显示来自另一个工作表的数据

我需要find某些数字(由用户给出)之间的行,将它们显示在屏幕上以便打印。

例如:用户input104822000011和104822000020作为值和值。 然后,我需要在另一个工作表中search这些之间的任何数字。 工作表中的数据来自数据库。 我需要返回给定数字之间的行中的所有数据。

我对VBA知之甚less,所以如果这可以用工作表函数来完成的话,那就更好了。 我search了一些东西,但是没有一个看起来很容易或者没有工作。 有人能帮我一下吗?

是的,你可以通过复制行(你不想要的)或VBA来做到这一点。 我相信VBA是一个不错的select。

但如果logging号是严格的数字,并没有太多的数据显示,我有一个愚蠢的方法使用数据透视表。

您可以添加引用数据表的pivotTable。 然后将所有字段拖到行标签。 然后对于每个字段设置,在布局中select“以表格forms显示项目标签”,并删除所有小计。 它现在应该看起来类似于原始数据。

然后,您可以selectlogging号码上的任意位置,然后在行标签上select“标签filter==>之间”。 然后input你的价值。

假设您在工作表dataWS上有您的数据,列A中的行号和列B中的数据

在另一个工作表上,您from A1input您的值from然后在B1input您的值

然后在A2 =IF($A1<$B$1,$A1+1,"")显示你想在这一行上显示的行号。

接下来,在B2检索=IF($A2="","",VLOOKUP($A2,dataWS!A:B,2,FALSE)) 。 将A2:B2的公式复制到下面的行中,并且应该被设置。

如果你想用VBA做到这一点,你可以使用下面的代码(假设你的数据中的ID是正确sorting的)。 请注意,在写入新数据之前,它不会清空目标区域。

 Public Sub getData() Dim currentId As Long Dim toId As Long Dim wsTarget As Worksheet Dim targetIdCol As Integer Dim targetDataCol As Integer Dim wsSource As Worksheet Dim sourceIdCol As Integer Dim sourceDataCol As Integer Dim readRow As Long Dim writeRow As Long Set wsTarget = ThisWorkbook.Worksheets("Sheet2") ' name of the target worksheet targetIdCol = 1 'number of the column where the ids are to be written (a=1) targetDataCol = 2 'number of the column where the data is to be written Set wsSource = ThisWorkbook.Worksheets("Sheet1") ' name of the source data worksheet sourceIdCol = 1 'number of the column where the ids are to be read(a=1) sourceDataCol = 2 'number of the column where the data is to be read currentId = wsTarget.Range("A1").Value 'cell in which the from is specified (here "A1" of target worksheet) toId = wsTarget.Range("B1").Value 'cell in which the to is specified (here "B1" of target worksheet) readRow = 1 'row at which the data should start to be read writeRow = 2 'row at which the data should start to be written While (wsSource.Cells(readRow, sourceIdCol) <> "") And (currentId <= toId) If currentId = wsSource.Cells(readRow, sourceIdCol).Value Then wsTarget.Cells(readRow, targetIdCol) = wsSource.Cells(readRow, sourceIdCol).Value wsTarget.Cells(readRow, targetDataCol) = wsSource.Cells(readRow, sourceDataCol).Value readRow = readRow + 1 Else If currentId > wsSource.Cells(readRow, sourceIdCol).Value Then readRow = readRow + 1 Else currentId = currentId + 1 End If End If Wend End Sub