Copy and paste based on sheet name and cell value

topi1

Board Regular
Joined
Aug 6, 2014
Messages
161
Office Version
  1. 2010
Is there a way to have vba which does the following.
If the text in the cell A1 in the sheet1 says "sheet2", the vba copies column B from the sheet2 and pastes it the column B of the sheet1. And, if the text in the cell A1 in the sheet1 says "sheet3", the vba copies column B from the sheet3 and pastes it the column B of the sheet1. The workbook will have many more sheets than the three sheets I described. But the goal is to paste into column B of the sheet1 data from the sheet with the matching name. Thank you.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I'd recommend some naming for you sheets and probably a dropdown in A1 of all your sheet names.

Something like this:

VBA Code:
Public Sub CopyDataBasedOnSheetName()
    Dim sourceName As String
    Dim destinationSheet As Worksheet
    Dim sourceSheet As Worksheet
    
    sourceName = ActiveWorkbook.Sheets("Sheet1").Range("A1").Value
    
    On Error Resume Next
    Set sourceSheet = Sheets(sourceName)
    On Error GoTo 0
    
    If Not sourceSheet Is Nothing Then
        Set destinationSheet = Sheets("Sheet1")
        
        destinationSheet.Columns("B").ClearContents
        sourceSheet.Columns("B").Copy destinationSheet.Columns("B")
    End If
End Sub
 
Upvote 0
Solution
I'd recommend some naming for you sheets and probably a dropdown in A1 of all your sheet names.

Something like this:

VBA Code:
Public Sub CopyDataBasedOnSheetName()
    Dim sourceName As String
    Dim destinationSheet As Worksheet
    Dim sourceSheet As Worksheet
  
    sourceName = ActiveWorkbook.Sheets("Sheet1").Range("A1").Value
  
    On Error Resume Next
    Set sourceSheet = Sheets(sourceName)
    On Error GoTo 0
  
    If Not sourceSheet Is Nothing Then
        Set destinationSheet = Sheets("Sheet1")
      
        destinationSheet.Columns("B").ClearContents
        sourceSheet.Columns("B").Copy destinationSheet.Columns("B")
    End If
End Sub

I'd recommend some naming for you sheets and probably a dropdown in A1 of all your sheet names.

Something like this:

VBA Code:
Public Sub CopyDataBasedOnSheetName()
    Dim sourceName As String
    Dim destinationSheet As Worksheet
    Dim sourceSheet As Worksheet
   
    sourceName = ActiveWorkbook.Sheets("Sheet1").Range("A1").Value
   
    On Error Resume Next
    Set sourceSheet = Sheets(sourceName)
    On Error GoTo 0
   
    If Not sourceSheet Is Nothing Then
        Set destinationSheet = Sheets("Sheet1")
       
        destinationSheet.Columns("B").ClearContents
        sourceSheet.Columns("B").Copy destinationSheet.Columns("B")
    End If
End Sub
Thank you so much. Can't wait to try.
 
Upvote 0
I'd recommend some naming for you sheets and probably a dropdown in A1 of all your sheet names.

Something like this:

VBA Code:
Public Sub CopyDataBasedOnSheetName()
    Dim sourceName As String
    Dim destinationSheet As Worksheet
    Dim sourceSheet As Worksheet
  
    sourceName = ActiveWorkbook.Sheets("Sheet1").Range("A1").Value
  
    On Error Resume Next
    Set sourceSheet = Sheets(sourceName)
    On Error GoTo 0
  
    If Not sourceSheet Is Nothing Then
        Set destinationSheet = Sheets("Sheet1")
      
        destinationSheet.Columns("B").ClearContents
        sourceSheet.Columns("B").Copy destinationSheet.Columns("B")
    End If
End Sub
[/C
[/QUOTE]

I'd recommend some naming for you sheets and probably a dropdown in A1 of all your sheet names.

Something like this:

VBA Code:
Public Sub CopyDataBasedOnSheetName()
    Dim sourceName As String
    Dim destinationSheet As Worksheet
    Dim sourceSheet As Worksheet
   
    sourceName = ActiveWorkbook.Sheets("Sheet1").Range("A1").Value
   
    On Error Resume Next
    Set sourceSheet = Sheets(sourceName)
    On Error GoTo 0
   
    If Not sourceSheet Is Nothing Then
        Set destinationSheet = Sheets("Sheet1")
       
        destinationSheet.Columns("B").ClearContents
        sourceSheet.Columns("B").Copy destinationSheet.Columns("B")
    End If
End Sub
Brilliant! Worked exactly as I had hoped. May try to modify it to paste it below last filled cell in the column A. Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,952
Members
449,095
Latest member
nmaske

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