我试图使用VBA在一个工作簿的表单中搜索" Transactional Files"并复制到工作簿中的工作表" Utilities"基于三个标准。第一个是,如果列A =" PV",则第二个标准是列M是否具有" Utilities-Water" "公用设施电动"或者" Utilities Gas",第三个是如果列AE已经不在表单中,那么如果满足所有三个,则复制并粘贴该行。
我找到了这个模板,但不知道在哪里插入我的标准:
Sub copyRow()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim lastrowDest As Long, currowSrc As Long, currowDest As Long, lastrowSrc
As Long
Dim critvalue1 As String
Set ws1 = Sheets("Sheet2")
Set ws2 = Sheets("Sheet1")
lastrowSrc = ws2.Range("AE" & Rows.Count).End(xlUp).Offset(1).Row - 1
lastrowDest = ws1.Range("AE" & Rows.Count).End(xlUp).Row
For currowSrc = 2 To lastrowSrc
critvalue1 = ws2.Range("E" & currowSrc).Value
ws2.Cells(6, 5).Value = critvalue1
For currowDest = 2 To lastrowDest
If ws1.Range("E" & currowDest).Value = critvalue1 Then
ws2.Rows(currowSrc).Copy Destination:=ws1.Range("A" & currowDest)
End If
Next currowDest
Next currowSrc
End Sub
感谢!!!
答案 0 :(得分:0)
您提供的示例代码是从一张到另一张。 您想要检查一个工作簿中的一个工作表,以及另一个工作簿中的另一个工作表。
所以你必须打开那个工作簿(当完成它时)。
然后你会在我们的工作表中逐行查找
Dim trx as workbook ' the other workbook (i.e. the other excel file).
' trx is a vba variable that will hold the contents of the transactions excel file
' a workbook has sheets in it.
dim sheet1 as worksheet, sheet2 as worksheet
' these are two vba variables holding the two sheets we'll work on.
dim criteria1 as boolean, criteria2 as boolean, criteria3 as boolean
' boolean variables can have the value of True or False only.
' just for reading
dim columnAindex as integer, columnMindex as integer, columnAEindex as integer
columnAindex = 1
columnMindex = Range("1M").column ' 13
columnAEindex = Range("AE").column ' yup, that number
set trx = Workbooks.open("trx.xlsx")
set sheet2 = trx.Sheets(1) ' or whatever sheet by index or name.
For Each myrow in Rows
criteria1 = (myrow.cells(1,columnAindex) = "PV') ' .cells(1,1) is columnA of current row
m = myrow.cells(1, columnMindex) ' value in cell of this current row, at column M
criteria2 = ( (m = this) or (m = that) or (m = theother) )
' if the value in AE is unique for each row, this will work:
' I got that from another question on stack overflow
ae = myrow.cells(1, columnAEindex) ' value in our row at column AE
set trxAEcolumnRange = sheet2.Range("AE:AE")
criteria3 = IsError(Application.Match(ae, trxAEcolumnRange, 0)) ' true if could not find a match to the value in our row's ae.
if criteria1 and criteria2 and criteria3 then copypasterow(myrow)
next
trx.close()
end sub
sub CopyPasteRow(aRow)
aRow.copy
sheet2.range.end.paste ' no need for xlup because this is the next empty row after the data
end sub