[英]Excel VB searching script
我正在尝试在Visual Basic中编写一个脚本,该脚本可以做到这一点:我有2个Excel工作表,并且我想从#2 Excel工作表中的#1 Excel工作表中逐行搜索所有行,如果相似,我要替换该行中的特定列(在#2 Excel工作表中)。
问题是,我不知道如何在VB中的Excel文档中进行搜索,然后使用搜索结果。
一个例子:
#1 Excel sheet
A B C
Ferrari|1997|5.0|
Porsche|1998|7.0|
#2 Excel Sheet
A B C
Audi |1993|4.0|
Ferrari|1997|2.0|
Porsche|1998|3.0|
Opel |1999|1.4|
因此,在那种情况下,我想替换#1 Excel Sheet中的#2 Excel Shhet法拉利和保时捷行,并用新值替换“ C”列。
提前致谢!
好像您想在两个表上保留外部联接。 我通常在Excel中使用SQL进行此操作。 为Excel安装我的SQL加载项: http : //www.analystcave.com/excel-sql-add-in-run-sql-in-excel-with-ease/
假设您有一个工作表,而在Sheet1中有一个工作表
A B C
Ferrari 1 2
Porsche 2 3
Opel 3 10
Audi 4 11
在另一个工作表Sheet2中,
A B C
Ferrari 1 20
Porsche 2 21
从“加载项”->“运行SQL”中打开“运行SQL”命令。 在“输出范围”中,选择任何单元格。 在SQL中:
SELECT T1.A, T1.B, Iif(T2.C IS NULL,T1.C,T2.C) As C FROM [Sheet1$] As T1 LEFT OUTER JOIN [Sheet2$] AS T2 ON T1.A = T2.A
单击“运行SQL”,就是这样!
该代码既不好也不漂亮,但是可以工作
Sub test()
Dim car As String
Dim car_year As String
Dim i As Integer
'read the first document and store it in an array(list)
i = 1
car = Range("A" & i).Value
car_year = Range("B" & i).Value
While car <> ""
MsgBox car & "-" & car_year
'check if the combination car+car_year exist the the first document and replace cell C(i)
i = i + 1
car_year = Range("B" & i).Value
car = Range("A" & i).Value
Wend
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.