Excel – select列的结尾+其他行

我试图做一个macros,select我的工作表中的某些数据。 我有一个数据表被拉入它使用:

Windows("Item checkout workbook_New.xlsx").Activate Range("A2:G300").Select Selection.Copy Windows("VLookup test.xlsx").Activate Sheets("Sheet1").Select Range("A2:G2").Select ActiveSheet.Paste Application.CutCopyMode = False Sheets("Sheet1").Range("A2:G300").Copy Sheets("Sheet2").Range("A2") Sheets("Sheet2").Select Application.CutCopyMode = False 

一旦input了这个数据,我有两列H2:H300I2:I300 ,其中已经有了用于Vlookup的公式从A2:G300获取信息。

然后,我只需要select相关数据并将其复制回Windows("Item checkout workbook_New.xlsx") 。 通过相关数据,我只需要selectA2:G300范围内的数据以及匹配的H2:I300单元。 看到所有的H2:I300单元格都有数据,我不知道如何做到这一点。 我试图创build一个macros使用END来select所有的列A,然后与它一起行,但这是我得到的,你可以看到它不会工作:

 Range("A2").Select Range(Selection, Selection.End(xlDown)).Select Range("A2:I78").Select Selection.Copy 

我对VBA不是很了解,所以很难提出一些事情,但我觉得应该有办法让这个工作起作用。 任何build议将是伟大的!

 Range("A2").Select Range(Selection, Selection.End(xlDown)).EntireRow.Select Selection.Copy Windows("Item checkout workbook_New.xlsx").Activate Sheets("Sheet1").Select Range("A2").Select ActiveSheet.Paste Application.CutCopyMode = False 

得到它的工作!

根据你自己的回答,似乎我可能误解了你的问题。 我收集的是您正在寻找一种方法来select工作表中的相关单元格。 相关单元可能属于两个范围之一。 在一个范围内,应该select不为空的单元格。 在另一个范围内,应该select与值匹配的单元格。 (你可以添加你的复制/粘贴代码)

我解决了下面的问题。

 Sub test() 'store results here Dim result As Range setupTest 'check this range and return items that are not empty selectMatchingCells Range("A1:D1"), result 'check this range and return items that match value selectMatchingCells Range("B2:C4"), result, "hi" result.Select End Sub Function setupTest() Range("A1").Value = "anything" Range("c1").Value = "may go" Range("D1").Value = "here" Range("B2").Value = "hi" Range("B3").Value = "but not here" End Function Function selectMatchingCells(search As Range, result As Range, Optional searchValue As String = "") For Each cell In search 'are we checking that cell value matches string, or if cell has a value at all? If searchValue = vbNullString Then 'check if cell is not empty If IsEmpty(cell) = False Then selectCell result, cell Else 'check if value matches If cell.Text = searchValue Then selectCell result, cell End If Next cell End Function Function selectCell(result As Range, cell As Variant) 'check if result range already exists or not If result Is Nothing Then 'make range equal to cell Set result = cell Else 'add cell to existing range Set result = Union(result, cell) End If End Function 

请更清楚避免沟通不畅,谢谢!