K
K
Kotar4ik2021-03-04 16:06:48
Visual Basic
Kotar4ik, 2021-03-04 16:06:48

What is wrong with VBA macro processing cell array?

What could be wrong with the macro? The macro itself works, but somehow it is not clear. He must look for each value from the adjacent column (on the right) on page 2 and, having found a cell with this value, insert the value from the array column into the cell 2 cells to the right. But he inserts the first value in general, no one knows where, and inserts the rest with a shift of 1 cell up.

Sub Macro()
 For Each c In Worksheets("Sheet1").Range("AR2:AR31").Cells
    a = c.Value
    ActiveCell.Offset(0, 1).Activate
    b = ActiveCell.Value
    Sheets("Sheet2").Select
    Range("D14:D52").Select
    Selection.Find(What:=b, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    ActiveCell.Offset(0, 2).Activate
    ActiveCell.Value = a
    Application.CutCopyMode = False
    Sheets("For_tool").Select
    c.Select
    ActiveCell.Value = b
 Next
End Sub

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pychev Anatoly, 2021-03-05
@Kotar4ik

1- Set the work cell active at the beginning of the cycle. c.Selectright after line For Each
2 - Remove the command Application.CutCopyMode = False. You are not doing copy/paste.
3 - in the command, Sheets("For_tool").Selectchange the name of the sheet to your original Sheet1
4 - the command is ActiveCell.Value = bsuperfluous, because overwrites data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question