VBA code to copy certain values from another tab to the other based on certain conditions

Aleribagclara91

New Member
Joined
Jan 9, 2021
Messages
3
Office Version
  1. 2010
Platform
  1. Windows
Hi all,

Could I kindly ask for help with a basic code on the following:
- i have a first tab with data arranged by columns : entity, analyst, date of input, region, etc.
- i want to create in a second tab a table with various information from the first tab, which will fill automatic by my code, as soon as I select from a drop down list the name of the entity i want the informations for

There is no need for the full code, but I would be extremely grateful if you can help with the basic code to paste cells based on a condition and any other tip!!

Thank you!!!
 

Attachments

  • Tab 1.PNG
    Tab 1.PNG
    26.4 KB · Views: 15
  • Tab 2.PNG
    Tab 2.PNG
    11.6 KB · Views: 14

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Assume the drop down in column G of Sheet 1 has 3 values of "A", "B" and "C" for the user to select. Also assume these are validation droipdown and not combobox.
2 methods
1. If...Then...ElseIf...Then...End If method
Code:
With Sheets("Sheet1")
    If .Range("G1").Value = "A" Then
        .Range("A2:F5").Copy Sheets("Sheet2").Range("D2")
    ElseIf .Range("G1").Value = "B" Then
        .Range("A2:F5").Copy Sheets("Sheet2").Range("D3")
    ElseIf .Range("G1").Value = "C" Then
        .Range("A2:F5").Copy Sheets("Sheet2").Range("D4")
    End If
End With

2. Select Case Method
Code:
Dim rng As Range
With Sheets("Sheet1")
    Select Case .Range("G1").Value
        Case "A": Set rng = Sheets("Sheet2").Range("D2")
        Case "B": Set rng = Sheets("Sheet2").Range("D3")
        Case "C": Set rng = Sheets("Sheet2").Range("D4")
    End Select
    Range("A2:F5").Copy rng
End With

Both methods do the same thing.
 
Last edited:
Upvote 0
Hi,
You can use Index Match formula to get the required information (see image)

=IFNA(INDEX(Sheet1!$B$2:$J$5,MATCH(Sheet2!$B$1,Sheet1!$G:$G,0),MATCH(Sheet2!A3,Sheet1!$B$1:$J$1,0)),"Not Available")

Thanks,
Saurabh
 

Attachments

  • indexMatchTab2.PNG
    indexMatchTab2.PNG
    15.1 KB · Views: 7
Upvote 0
Hi all,

Could I kindly ask for help with a basic code on the following:
- i have a first tab with data arranged by columns : entity, analyst, date of input, region, etc.
- i want to create in a second tab a table with various information from the first tab, which will fill automatic by my code, as soon as I select from a drop down list the name of the entity i want the informations for

There is no need for the full code, but I would be extremely grateful if you can help with the basic code to paste cells based on a condition and any other tip!!

Thank you!!!

Assume the drop down in column G of Sheet 1 has 3 values of "A", "B" and "C" for the user to select. Also assume these are validation droipdown and not combobox.
2 methods
1. If...Then...ElseIf...Then...End If method
Code:
With Sheets("Sheet1")
    If .Range("G1").Value = "A" Then
        .Range("A2:F5").Copy Sheets("Sheet2").Range("D2")
    ElseIf .Range("G1").Value = "B" Then
        .Range("A2:F5").Copy Sheets("Sheet2").Range("D3")
    ElseIf .Range("G1").Value = "C" Then
        .Range("A2:F5").Copy Sheets("Sheet2").Range("D4")
    End If
End With

2. Select Case Method
Code:
Dim rng As Range
With Sheets("Sheet1")
    Select Case .Range("G1").Value
        Case "A": Set rng = Sheets("Sheet2").Range("D2")
        Case "B": Set rng = Sheets("Sheet2").Range("D3")
        Case "C": Set rng = Sheets("Sheet2").Range("D4")
    End Select
    Range("A2:F5").Copy rng
End With

Both methods do the same thing.

Thank you very much the quick reply. I may have been too brief on explaining the additional difficulty:

1. In my data tab, the name of columns represent the names of the rows i would like to have in the table tab

2. the table should have a specific project in every column ("EntityName" in image 1), under which i will show certain information (country, date, analyst, etc).

3. in my data tab, there are more columns that I need but I shouldnt delete anything in the data tab - so I needed to specifically add in the code which elements i need, for example for a given project A, i would need the columns "country' and "analyst" from the data tab to be my first two rows for the table tab (in reality the final table will have about 20-30 rows)

4. again, in my table tab, the name of the columns = project i am looking for. However, the selection of the project is made based on a drop down list which includes all the projects appearing in the data tab, because if not there would be too many columns in my table. The purpose of the table tab is to allow comparison between different projects that have appeared in the data tab (already subject to a filter) but on which i decide to put into a comparable table: once a project name is selected in cell D2 (second image), the rest of the rows should be automatically updated.

I am not sure if all this is possible...

Again, thank you very much for your time.
 
Upvote 0
Hi all,

Could I kindly ask for help with a basic code on the following:
- i have a first tab with data arranged by columns : entity, analyst, date of input, region, etc.
- i want to create in a second tab a table with various information from the first tab, which will fill automatic by my code, as soon as I select from a drop down list the name of the entity i want the informations for

There is no need for the full code, but I would be extremely grateful if you can help with the basic code to paste cells based on a condition and any other tip!!

Thank you!!!

Hi,
You can use Index Match formula to get the required information (see image)

=IFNA(INDEX(Sheet1!$B$2:$J$5,MATCH(Sheet2!$B$1,Sheet1!$G:$G,0),MATCH(Sheet2!A3,Sheet1!$B$1:$J$1,0)),"Not Available")

Thanks,
Saurabh
Thank you but i would need unfortunately to do it with VBA!
 
Upvote 0
if you can help with the basic code to paste cells based on a condition
Post #2 does this. Now iit seems that a fully developed code was expected.
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,702
Members
449,048
Latest member
81jamesacct

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