简体   繁体   中英

Excel vba - How to replace a cell with a complex value

I have two sheets. WIRES and BOM.

WIRES looks like this:

| Ltg-Nr_ | Kurzname | Pin | Kurzname | Pin | Farbe |
|---------|----------|-----|----------|-----|-------|
|  712001 | AJ4      |  11 | LSTS     |   7 | GE    |
|  712002 | AJ4      |  10 | LSTS     |   8 | SW    |
|  712003 | KM_23.1  |   1 | KM_12.4  |   1 | BR    |
|  712004 | AJ4      |  19 | GSR2     |   2 | GN    |
|  712005 | AJ4      |  18 | GSR2     |   1 | SW    |
|  712006 | AJ4      |  46 | CR_31AB  |   1 | BR    |
|  712007 | AJ4      |  49 | CR_CANP  |   1 | OR/SW |
|  712008 | AJ4      |  50 | CR_CANM  |   1 | OR/BR |

BOM looks like this:

| Con  |
|------|
| GSR2 |
| AJ4  |

I want to do a macro in excel to search for each values from rows in sheet BOM, in sheet WIRES, and where it find the value to replace with value_"cell_in_front_of_it". After this put 1 in front of cells replaced. For example, it has to search for GSR2 in sheet WIRES and where it find GSR2 to replase it with GSR2_2; next row replace GSR2 with GSR2_1. After macro run i want table to look like this:

| Ltg-Nr_ | Kurzname | Pin | Kurzname | Pin | Farbe |
|---------|----------|-----|----------|-----|-------|
|  712001 | AJ4      |  11 | LSTS     |   7 | GE    |
|  712002 | AJ4      |  10 | LSTS     |   8 | SW    |
|  712003 | KM_23.1  |   1 | KM_12.4  |   1 | BR    |
|  712004 | AJ4      |  19 | "GSR2_2" | "1" | GN    |
|  712005 | AJ4      |  18 | "GSR2_1" | "1" | SW    |
|  712006 | AJ4      |  46 | CR_31AB  |   1 | BR    |
|  712007 | AJ4      |  49 | CR_CANP  |   1 | OR/SW |
|  712008 | AJ4      |  50 | CR_CANM  |   1 | OR/BR |

I tried this:

I tried something like this:

Sub Macro1()
'
' Macro1 Macro
'

'
Dim Col As Integer
Dim Row As Integer

For Col = 2 To 4
    For Row = 2 To 10
    colo = Row + 1
    Rows = Row + 1
    Sheets("WIRES").Columns(2).Replace What:=Sheets("BOM").Cells(Row, 1).Text, Replacement:=Sheets("WIRES").Cells(Row, 2).Text & "_" & Sheets("WIRES").Cells(Rows, colo).Text, LookAt:=xlWhole, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Next Row
Next Col

End Sub

I made this, and it worked exactly as i wanted. Thanks for your answers.

Dim roww, rowb As Integer
For roww = 2 To 150
    For rowb = 1 To 10

        Sheets("WIRES").Select
        Cells(roww, 2).Select

        If Selection.Value = Sheets("BOM").Cells(rowb, 1).Value Then
                Selection.Value = Selection.Value & "_" & Selection.Offset(0, 1)
                Selection.Offset(0, 1).Value = "1"
        End If

        Sheets("WIRES").Select
        Cells(roww, 4).Select

        If Selection.Value = Sheets("BOM").Cells(rowb, 1).Value Then
                Selection.Value = Selection.Value & "_" & Selection.Offset(0, 1)
                Selection.Offset(0, 1).Value = "1"
        End If

    Next rowb
Next roww

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM