所以我在这里度过了难关。我有两张不同的电子表格。让我们说Sheet1有一列,相应的行(A1,A2,A3 ..)中的数据为x,y,z ......
现在我有另一张电子表格(Sheet2),在一列中有相同的数据集(x,y,z ......),并说" A"。在专栏" B"我有相应的值说(1,2,3),这样
A1 = x,B1 = 1
我希望我的宏读取Sheet1第一列(即x,y或z)的值,然后将它与Sheet2的列数据(" A")匹配,因此,选择列#B; B"中的相应值Sheet2。
我已经尝试了一段时间,但我无法找到方法。我很抱歉以这种随意的方式呈现它。但有人请帮忙吗?这是mu尝试代码..但是只有当列中的值是(" 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..
答案 0 :(得分:1)
一种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