In excel sheet named Providers the following columns contain text string. Column B…
- User
In excel sheet named Providers the following columns contain text string. Column B contains text that describes a service. Column C contains text of company names. Column D contains text of Sheet names within the workbook.
When I click on a cell in Column D, it takes me to the Sheet named in the cell I clicked on. I already have VBA code that performs this function perfectly.
What I additionally want is to be able to click on a cell in Column D of Sheet Providers, then take me to the Sheet named in the cell that I clicked on and copy the values of Column B of the same row in Sheet Providers, into the next available blank cell of cloumn B in the new active sheet that is currently opened.
I also want the values in Column C of the same row from Sheet Providers to be copied to Column A on the same row of column B where values were last pasted in the new active sheet that is currently opened.
- Assistant
To achieve this, you can add the following code to the existing code that takes you to the sheet named in the clicked cell:
Sub Worksheet_SelectionChange(ByVal Target As Range) Dim ws As Worksheet Dim rng As Range
Set rng = Range("D:D") 'Change this to the range of your choice If Not Intersect(Target, rng) Is Nothing Then 'If clicked cell is in the defined range Set ws = ThisWorkbook.Sheets(Target.Value) 'Set To the sheet named in the clicked cell ws.Activate 'Activate the sheet 'Find the next available row in Column B of the new sheet Dim nextRow As Long nextRow = ws.Cells(Rows.Count, "B").End(xlUp).Row + 1 'Copy the value from Column B of Sheet Providers Worksheets("Providers").Range("B" & Target.Row).Copy ws.Range("B" & nextRow) 'Copy the value from Column C of Sheet Providers to Column A on the same row of Column B Worksheets("Providers").Range("C" & Target.Row).Copy ws.Range("A" & nextRow) End IfEnd Sub
Note: Make sure to change the range in the code to match the range of Column D in Sheet Providers where you want this functionality to work.
- User
I only want to copy the values from Sheet Providers if the Sheet opened is named ADH
- Assistant
You can modify the existing code to check if the active sheet is named "ADH" before copying the values from Sheet Providers. Here's the modified code:
Sub Worksheet_SelectionChange(ByVal Target As Range) Dim ws As Worksheet Dim rng As Range
Set rng = Range("D:D") 'Change this to the range of your choice If Not Intersect(Target, rng) Is Nothing Then 'If clicked cell is in the defined range Set ws = ThisWorkbook.Sheets(Target.Value) 'Set To the sheet named in the clicked cell ws.Activate 'Activate the sheet 'If the active sheet is named "ADH" If ws.Name = "ADH" Then 'Find the next available row in Column B of the new sheet Dim nextRow As Long nextRow = ws.Cells(Rows.Count, "B").End(xlUp).Row + 1 'Copy the value from Column B of Sheet Providers Worksheets("Providers").Range("B" & Target.Row).Copy ws.Range("B" & nextRow) 'Copy the value of Column C of Sheet Providers to Column A on the same row of Column B Worksheets("Providers").Range("C" & Target.Row).Copy ws.Range("A" & nextRow) End If End If