Specify conditions in one sheet to fill another sheet

gubertu

Board Regular
Joined
May 24, 2015
Messages
147
Hi all,

I am looking for a VBA code/macro for doing the following (see below example).

1. In sheet "Data", column "A = Tab name" you need to specify the name of the sheet (in this case, in the workbook there is a sheet called "BS".

2. Also in sheet "Data", you need to specify the "Destination Cell = column B" where the value in "column C = Value".

3. The macro/code should find the sheet mentioned in column A "Tab name" and place the values (if there are several values in the same destination cell, they should be added) in the destination cell of that mentioned sheet.

I trust I explained myself well.
Thanks in advance!

EXAMPLE

Sheet "Data".


ABC
1Tab NameDestination CellValue
2BSA11.000
3BSA1500
4BSA21.000

<tbody>
</tbody>

Sheet "BS"

A
11.500
21.000

<tbody>
</tbody>
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hello,

have assumed the destination sheets already exist in the workbook. Also assumed Column D is available in the Data Sheet.


Code:
Sub ADD_UP()
    Application.ScreenUpdating = False
    With Sheets("Data")
        For MY_ROWS = 2 To .Range("A" & Rows.Count).End(xlUp).Row
            If .Range("D" & MY_ROWS).Value <> "Y" Then
                .Range("D" & MY_ROWS).Value = "Y"
                MY_TOTAL = .Range("C" & MY_ROWS).Value
                For MY_NEXT_ROWS = MY_ROWS + 1 To .Range("A" & Rows.Count).End(xlUp).Row
                 If .Range("A" & MY_NEXT_ROWS).Value & .Range("B" & MY_NEXT_ROWS).Value = _
                    .Range("A" & MY_ROWS).Value & .Range("B" & MY_ROWS).Value Then
                        MY_TOTAL = MY_TOTAL + .Range("C" & MY_NEXT_ROWS).Value
                        .Range("D" & MY_NEXT_ROWS).Value = "Y"
                End If
                Next MY_NEXT_ROWS
            Sheets(.Range("A" & MY_ROWS).Value).Range(.Range("B" & MY_ROWS).Value).Value = MY_TOTAL
            MY_TOTAL = 0
            End If
        Next MY_ROWS
        .Columns("D").ClearContents
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi and thanks for your help!

The code works perfect, but I would like to know if there´s a possibility to delete the values that are placed in sheet "BS", when I delete the values in column "C" of sheet "Data".

Because If I run the macro and then I delete the values in column C, Sheet "Data", the linked values still continue in sheet "BS".

Thanks!
 
Upvote 0
Hello,

This code needs to go in to the 'Data' Sheet code window, not a standard module.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("C1:C" & Range("A" & Rows.Count).End(xlUp).Row)) Is Nothing Then
    Application.ScreenUpdating = False
    With Sheets("Data")
        For MY_ROWS = 2 To .Range("A" & Rows.Count).End(xlUp).Row
                If .Range("A" & MY_ROWS).Value & .Range("B" & MY_ROWS).Value = _
                    .Range("A" & Target.Row).Value & .Range("B" & Target.Row).Value Then
                        MY_TOTAL = MY_TOTAL + .Range("C" & MY_ROWS).Value
                End If
        Next MY_ROWS
        Sheets(.Range("A" & Target.Row).Value).Range(.Range("B" & Target.Row).Value).Value = MY_TOTAL
    End With
    Application.ScreenUpdating = True
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,580
Messages
6,125,652
Members
449,245
Latest member
PatrickL

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top