find范围地址#2

所以,因为我没有具体与我以前的问题,我需要你的帮助了。

基本上,我问是否有办法find一些值/单元格之间的范围,因为我确定,当我将得到这个范围这个下面将工作(所以我可以select让我们说所有的数据列的“标题”下面:

totalRange(Selection,Selection.End(xlDown))。select

所以你们中的一位提出了帮助,并提供了下面的代码,这工作得很好,但我不确定我是否可以使用它在我的情况。 因为正如我所说,我试图做的是首先find第一层的两个单元格之间的范围,然后select下面的所有数据。 就像下面的截图一样。 我想findCol7和Col12,然后select下面的整个范围。 问题是这个Col7 / Col12范围可能从每个文件的不同列开始。

https://ibb.co/gtuvEb

Sub RangeBetween() Dim totalRange As Range Dim c1 As Long, c2 As Long Dim r1 As Long, r2 As Long r1 = 0 r2 = 0 c1 = 1 c2 = 1 With Worksheets("Sheet1") 'Change to your worksheet c1 = 1 Do Until Name = "A" Name = Cells(1, c1) c1 = c1 + 1 Loop c1 = c1 - 1 c2 = 1 Do Until Name = "B" Name = Cells(1, c2) c2 = c2 + 1 Loop c2 = c2 - 1 On Error Resume Next r1 = Application.WorksheetFunction.Match("A", .Columns(c1), 0) r2 = Application.WorksheetFunction.Match("B", .Columns(c2), 0) On Error GoTo 0 If r1 > 0 And r2 > 0 Then Set totalRange = .Range(.Cells(r1, c1), .Cells(r2, c2)) totalRange.Select Else MsgBox "One or both items not found in range" End If End With End Sub 

感谢您的任何build议。

 Sub RangeBetween() Dim totalRange As Range Dim c1 As Long, c2 As Long Dim r1 As Long With Worksheets("Sheet1") 'Change to your worksheet On Error Resume Next 'Find the Columns c1 = Application.WorksheetFunction.Match("Col7", .Rows(1), 0) c2 = Application.WorksheetFunction.Match("Col12", .Rows(1), 0) On Error GoTo 0 If c1 > 0 And c2 > 0 Then 'Find last row with data r1 = .Cells(.Rows.Count, c2).End(xlUp).Row 'Set the range to the whole Set totalRange = .Range(.Cells(1, c1), .Cells(r1, c2)) totalRange.Select Else MsgBox "One or both items not found in range" End If End With End Sub 

看起来你试图在标题中查找某些值并select这些列之间的值。 如果我理解正确,你的问题可以帮助你。

 Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("your sheet") Dim header1 As Range, header2 As Range On Error Resume Next Set header1 = ws.Rows(1).Find(what:="your header value 1", LookIn:=xlValues, lookat:=xlWhole) Set header2 = ws.Rows(1).Find(what:="your header value 2", LookIn:=xlValues, lookat:=xlWhole) On Error GoTo 0 If Not header1 Is Nothing And Not header2 Is Nothing Then Range(header1, header2).EntireColumn.SpecialCells(xlCellTypeConstants).Select Else: MsgBox "Header not fount" End If