[英]Matching cell values in different sheets
so I am having a tough time here. 所以我在这里很难过 I have two different spread sheets.
我有两个不同的电子表格。 Let's say Sheet1 has a column with data in the corresponding rows(A1,A2,A3..) as x, y, z...
假设Sheet1在对应的行(A1,A2,A3 ..)中的数据为x,y,z ...
Now I have another spread sheet(Sheet2) with the same set of data (x, y, z...) in one column say "A". 现在,我在另一列中有另一个具有相同数据集(x,y,z ...)的电子表格(Sheet2),称“ A”。 In column "B" I have corresponding values say (1, 2, 3) such that
在“ B”列中,我有相应的值说(1、2、3),这样
A1 = x, B1 = 1 A1 = x,B1 = 1
I want my macro to read the values off the first column of Sheet1(ie x, y or z) and then match it with the column data("A") of Sheet2 and hence, pick the corresponding values from the column "B" of Sheet2. 我希望我的宏从Sheet1的第一列(即x,y或z)中读取值,然后将其与Sheet2的列数据(“ A”)进行匹配,因此,从“ B”列中选择相应的值Sheet2。
I have been trying for a while, but I am unable to figure out a way. 我已经尝试了一段时间,但无法找到解决方法。 And I am sorry for presenting it in such a haphazard manner.
很抱歉以这种随意的方式介绍它。 But someone please help?
但是有人请帮忙吗? Here is mu attempted code..but this would work(if it does) only when the values in the column are ("2012-2013" etc..) I want it to be dynamic.
这是亩尝试的代码..但是,只有当列中的值为(“ 2012-2013”等)时,这才起作用(如果可以),我希望它是动态的。
Sub CalC_stat()
Dim ws As Sheets
Set ws = ThisWorkbook.Sheets(Array("S1 Fuel Consumption", "EF_Stat", "Summary"))
Dim i As Integer, j As Integer
For j = 7 To 15 Step 4
Select Case ws(1).Range("A" & j).Value
Case "2012-2013"
For i = 1 To 4
With ws(1).Shapes("Fuel " & i).ControlFormat
Select Case .ListIndex
Case 1
ws(3).Range("B" & i).Value = Empty
Case 2
ws(3).Range("B" & i).Value = ws(2).Range("B3").Value
Case 3
ws(3).Range("B" & i).Value = ws(2).Range("C3").Value
Case 4
ws(3).Range("B" & i).Value = ws(2).Range("D3").Value
End Select
End With
Next i
Case "2013-2014"
For i = 5 To 8
With ws(1).Shapes("Fuel " & i).ControlFormat
Select Case .ListIndex
Case 1
ws(3).Range("B" & i).Value = Empty
Case 2
ws(3).Range("B" & i).Value = ws(2).Range("B4").Value
Case 3
ws(3).Range("B" & i).Value = ws(2).Range("C4").Value
Case 4
ws(3).Range("B" & i).Value = ws(2).Range("D4").Value
End Select
End With
Next i
Case "2014-2015"
' and so on..
One VBA way to do it: 一种VBA方式:
Sub test()
dim a as Long, b as Long, c as Long
For a = 1 to 1048576 'last row of your excel sheet
If IsEmpty(Sheet1.Range("A" & a)) Then Exit For
Next a
For b = 1 to a
For c = 1 to a
If Sheet1.Range("A" & b) = Sheet2.Range("A" & c) Then
Sheet1.Range("B" & b) = Sheet2.Range("B" & c)
Exit For
End If
Next c
Next b
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.