我正在尝试检查某个值是否在某个范围内。如果出现该值,则该enty的相应数据将复制到另一个工作表。技巧是它必须动态确定,因为列大小因输入表到输入表而异。在Java中,可以使用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
答案 0 :(得分:0)
您的问题分为两部分:
使用快速Google轻松找到关于查找上次使用行的第一部分:Error in finding last used cell in VBA
要从列的开头到结尾循环,请使用:
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
答案 1 :(得分:0)
假设我们有以下数据:
我们希望在前两列中找到幸福并检索该行中的 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
答案 2 :(得分:0)
您可能会觉得这很有用:http://support.microsoft.com/kb/830287
但我个人在这种情况下所做的事情涉及更多的代码,但是灵活而快速。首先创建一个类并调用它" 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
然后为您的代码,使用:
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