copying cells in Excel based on values in other ce

2019-08-01 06:39发布

I would like to know how to write a Macro to copy the value of L3 up to L2 based on the fact that A2 and A3 are equal and B2 and B3 are also equal. Where subsequent rows of columns A and B are not equal the macro does not need to push data up. In some instances there may be as many as 10 subsequent rows where A & B column vales are equal and the oldest month value would need to roll right up to the top row where A & B match.

The entire sheet has about 150 rows with many City and Company values. I am currently performing this operation manually and it is time consuming. The purpose of the spreadsheet is to provide a sales visit summary based on data that is extracted out of our SQL based CRM package. Row 4 has no meaningful data is that context but it does not hurt if the data is rolled up Row 2 as long as all months with visits are represented on the top row where columns A & B match.

I'm guessing that the macro needs to loop through JAN (Column E) and complete that column and then loop through columns G through P sequentially after that. Only Columns E through P need to be manipulated. I don't think it will have any negative consequences if the data is concatenated in the cells or just overwritten. Once the Macro is complete the top row for a given Company at a certain site should contain values in all of the months where it was visited.

My programming knowledge is limited but I can usually reverse engineer code enough to modify it if I get a good sample. I hope my request is clear.

标签: excel vba
1条回答
ゆ 、 Hurt°
2楼-- · 2019-08-01 07:05

Based on your first initial question "how to copy L3 to L2 if B2=B3 and A2=A3", this basic macro will get you started along those lines:

Option Explicit

Sub CopyUP()
Dim LR As Long, Rw As Long, col As Long

LR = Range("A" & Rows.Count).End(xlUp).Row  'last row with data

For Rw = LR To 2 Step -1                    'from the bottom up, compare
    If Range("A" & Rw) = Range("A" & Rw - 1) And _
       Range("B" & Rw) = Range("B" & Rw - 1) Then

        For col = 5 To 16       'columns E:P
            If Cells(Rw, col) <> "" Then Cells(Rw - 1, col) = Cells(Rw, col)
        Next col

    End If
Next Rw

End Sub
查看更多
登录 后发表回答