简体   繁体   English

匹配不同工作表中的单元格值

[英]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.

相关问题 比较不同工作表中的值,如果匹配则复制相应的单元格(Excel 2013) - Compare Values in Different Sheets, then copy corresponding cell if matching (Excel 2013) 在不同的XLS工作表中找到匹配的值,并从匹配的行中复制其他单元格值 - Locate matching values in different XLS sheets and copy other cell values from matching row 检查两个不同工作表中的单元格值 - Check cell values in two different sheets Excel - 查找匹配值并复制不同的单元格 - Excel - Find matching values and copy different cell 修剪不同工作表中行和列中的单元格值 VBA - Trim cell values in rows and columns in different sheets VBA 如何使用JavaScript和ExcelJS检查不同工作表中的单元格值? - How to check cell values in different sheets using JavaScript and ExcelJS? 如何匹配单元格值并从不同的工作表中复制它们 - How to match the cell values and copy them from different sheets 使用工作表似乎不会更改不同工作表上的单元格值 - With Worksheets does not seem to change cell values on different sheets 匹配VBA中的两个字符串,将其他单元格值传输到其他位置 - Matching two strings in VBA, transporting other cell values to a different location 将单元格数据从一张纸复制到另一张纸,具体取决于两张纸上的匹配列值 - copy cell data from one sheet to another depending on matching column values in both sheets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM