Vba 检测列中的最后一个条目以搜索所用范围内的值
我试图检查某个值是否在某个范围内。如果出现该值,则该条目对应的数据将复制到另一张图纸。诀窍在于它必须动态确定,因为列大小因输入表而异。在Java中,可以使用Vba 检测列中的最后一个条目以搜索所用范围内的值,vba,excel,Vba,Excel,我试图检查某个值是否在某个范围内。如果出现该值,则该条目对应的数据将复制到另一张图纸。诀窍在于它必须动态确定,因为列大小因输入表而异。在Java中,可以使用hasNext()函数。我认为VBA最类似的函数应该是Sheet1.Column.End(xlup)。如何确定vba中do while循环测试条件的列结尾的最佳方法 伪示例: 'This is part of a nested loop, this segment traverses the column looking for 'speci
hasNext()
函数。我认为VBA最类似的函数应该是Sheet1.Column.End(xlup)
。如何确定vba中do while循环测试条件的列结尾的最佳方法
伪示例:
'This is part of a nested loop, this segment traverses the column looking for 'specified data.
Do While (!Sheets(inputSheetName).SyntaxToDetermineEndOfColumn))
If(someCell = someValue)
Copy values from the corresponding row to fields in newSheet
End If
Next r 'This increments the row within the current column
Loop
Next c 'This increments to the next column of data
你的问题分为两部分: 关于查找最后使用的行的第一部分可以通过快速Google轻松找到: 要从列的开始到结束循环,请使用以下命令:
Dim ws1 as Worksheet, LastRow as Long, CurRow as Long, DataFind as String
Set ws1 = Sheets("Name of Sheet")
LastRow = ws1.Range("Column letter" & ws1.Rows.Count).End(xlUp).Row
DataFind = Inputbox("What are you looking for?")
For CurRow = 1 to LastRow
If ws1.Range("Column Letter" & CurRow).Value = DataFind Then
ws1.Range("Column Letter" & CurRow).EntireRow.Copy
Sheets("Dest Sheet").Range("Whatever").PasteSpecial
End If
Next CurRow
Option Explicit
Public Sub Example()
'Set these as needed:
Const sheetName As String = "MySheet"
Const columnNumber As Long = 2&
Const criteria As String = "*foo#"
Dim wsIn As Excel.Worksheet
Dim wbOut As Excel.Workbook
Dim wsOut As Excel.Worksheet
Dim ri As RangeInfo
Dim rowIn As Long
Dim rowOut As Long
Dim col As Long
Set wbOut = Excel.Workbooks.Add
Set wsOut = wbOut.Worksheets(1)
Set wsIn = Excel.Worksheets(sheetName)
Set ri = New RangeInfo
ri.Initialize wsIn.UsedRange
rowOut = 1&
With ri
For rowIn = .RowTop To .RowBottom
If wsIn.Cells(rowIn, columnNumber) Like criteria Then
rowOut = rowOut + 1&
For col = .ColumnLeft To .ColumnRight
wsOut.Cells(rowOut, col).Value = wsIn.Cells(rowIn, col).Value
Next
End If
Next
End With
End Sub
假设我们有如下数据:
我们希望在前两列中找到快乐,并检索该行中的C列值:
Sub LookingForHappiness()
Dim i As Long, j As Long, N As Long, h As String
h = "happiness"
For i = 1 To 2
N = Cells(Rows.Count, i).End(xlUp).Row
For j = 1 To N
If Cells(j, i).Value = h Then
MsgBox Cells(j, "C").Value
MsgBox Cells(j, i).Address(0, 0)
Exit Sub
End If
Next j
Next i
End Sub
您可能会发现这很有用: 但我个人在这种情况下所做的工作涉及到更多的代码,但是非常灵活和快速。首先创建一个类并将其命名为“RangeInfo”。那么过去呢,
Option Explicit
Private Type Properties
Intialized As Boolean
Object As Excel.Range
RowBottom As Long
RowCount As Long
RowTop As Long
ColumnLeft As Long
ColumnCount As Long
ColumnRight As Long
End Type
Private this As Properties
Public Property Get Initialized() As Boolean
Initialized = this.Intialized
End Property
Public Property Get Object() As Excel.Range
Set Object = this.Object
End Property
Public Property Get ColumnLeft() As Long
ColumnLeft = this.ColumnLeft
End Property
Public Property Get ColumnCount() As Long
ColumnCount = this.ColumnCount
End Property
Public Property Get ColumnRight() As Long
ColumnRight = this.ColumnRight
End Property
Public Property Get RowBottom() As Long
RowBottom = this.RowBottom
End Property
Public Property Get RowCount() As Long
RowCount = this.RowCount
End Property
Public Property Get RowTop() As Long
RowTop = this.RowTop
End Property
Public Sub Initialize(ByRef rng As Excel.Range)
With this
Set .Object = rng
.RowTop = rng.row
.RowCount = rng.Rows.Count
.RowBottom = .RowTop + .RowCount - 1&
.ColumnLeft = rng.Column
.ColumnCount = rng.Columns.Count
.ColumnRight = .ColumnLeft + this.ColumnCount - 1&
.Intialized = True
End With
End Sub
Public Sub Clear()
Dim emptyProperties As Properties
this = emptyProperties
End Sub
Private Sub Class_Terminate()
Set this.Object = Nothing
End Sub
然后,对于您的代码,请使用以下命令:
Dim ws1 as Worksheet, LastRow as Long, CurRow as Long, DataFind as String
Set ws1 = Sheets("Name of Sheet")
LastRow = ws1.Range("Column letter" & ws1.Rows.Count).End(xlUp).Row
DataFind = Inputbox("What are you looking for?")
For CurRow = 1 to LastRow
If ws1.Range("Column Letter" & CurRow).Value = DataFind Then
ws1.Range("Column Letter" & CurRow).EntireRow.Copy
Sheets("Dest Sheet").Range("Whatever").PasteSpecial
End If
Next CurRow
Option Explicit
Public Sub Example()
'Set these as needed:
Const sheetName As String = "MySheet"
Const columnNumber As Long = 2&
Const criteria As String = "*foo#"
Dim wsIn As Excel.Worksheet
Dim wbOut As Excel.Workbook
Dim wsOut As Excel.Worksheet
Dim ri As RangeInfo
Dim rowIn As Long
Dim rowOut As Long
Dim col As Long
Set wbOut = Excel.Workbooks.Add
Set wsOut = wbOut.Worksheets(1)
Set wsIn = Excel.Worksheets(sheetName)
Set ri = New RangeInfo
ri.Initialize wsIn.UsedRange
rowOut = 1&
With ri
For rowIn = .RowTop To .RowBottom
If wsIn.Cells(rowIn, columnNumber) Like criteria Then
rowOut = rowOut + 1&
For col = .ColumnLeft To .ColumnRight
wsOut.Cells(rowOut, col).Value = wsIn.Cells(rowIn, col).Value
Next
End If
Next
End With
End Sub
So then.Range(“E”和.Rows.Count).End(xlUp).Row可能是SyntaxToDetermineEndOfColumn部分的解决方案?当范围由列“E”指定时,我使用变量“c”作为循环的列。我应该可以使用变量“c”对吗?这是我第一个使用VBA的项目。请在下面查看我的答案,就像这样!我有一个完整的工作表作为输入,我必须找到与字符x在同一行中的数据。所以我找到X并将数据复制到新的工作表中。我的循环变量是整数,我不想在这里听起来像noobish,但是使用长类型有什么好处呢?此外,如果底部有一个格式图例,该怎么办。有没有一个好办法可以解决这个问题?与运行n测试一样,删除标准化图例。再次运行n测试?我使用Long而不是Integer,因为Integer只能处理“小”数字。如果工作表底部有您希望从搜索中排除的材料,请在J循环中使用类似于N-7或类似的内容。非常感谢,这非常有用!我在这里使用integer作为特定类型的数据,对于这个特定的输入,我从未超过每列350条记录。这对于新的工作表变量来说是一个很好的观点,尽管根据列的数量,我可能会遇到问题。N-7在逻辑上看起来很明显!我只是对新语法有点不知所措。再次感谢!谢谢你的主意!它看起来肯定比我习惯的要熟悉得多。我喜欢创建一个类的概念;因为我将以稍微不同的方式使用这种类型的流程。不幸的是,我认为就我案件的具体情况而言,采用程序方法更好。我以后一定会记住这一点!