Copy Cell From Range On Active Row - excel

I'm trying to use VBA to copy a cell on the active row of a range.
A1:A10 is a range named "Panel_Length". I want to click a button to copy the cell from column "A" of the active row. The code works if I specify column "A" and active row but there is a chance that the range "Panel_Length" may get moved to another column.
Sub Plen_Click()
Dim PLength as Range
Set PLength = Sheet1.Range("Panel_Length")
Range(PLength & ActiveCell.Row).Copy
End Sub

Don't know if I understand the question correct. but if you try to copy the cell from column of the named range and the row form the selected cell you can try the code below
Sub Plen_Click()
Sheet1.Cells(Selection.Row, Sheet1.Range("Panel_Length").Column).Copy
End Sub

Related

How to paste/equalize specific range value in one sheets into another?

I have named "FormEntry" in Range("E7,F11,H12,I8,E16") in Sheet1.
and picture above is Sheet2.
Is that possible to copy Range "FormEntry" from Sheet1 and paste/equalize into the same Range in Sheets2 without other cells range in Sheet2 being replaced?
I've tried this syntax but the values on Sheets2 are A's
Sub test()
Sheets("Sheet2").Range("E7,F11,H12,I8,E16").Value = Sheets("Sheet1").Range("E7,F11,H12,I8,E16").Value
End Sub
You can use:
For Each cell In Range("FormEntry")
Sheets("Sheet2").Cells(cell.Row, cell.Column).Value = cell.Value
Next

Referencing a range on another sheet in vba code to copy a formula down to the last row of data

I have a workbook with two sheets. On sheet1 I have a column (A) for which I need to copy the formula in A2 down for as many rows as there are in sheet2 column (B).Currently I use the following code
Range("B3:B" & Cells(Rows.Count,"C").End(xlUp).Row).Formula = Range ("B2")
to do something similar but where the range referred to is on the same worksheet. What I need to know is how to adapt this code to replace the "C" above with the range on sheet 2. Can someone please help?
you could use:
With Sheet1 ' reference Sheet1 worksheet
.Range("B3:B" & Sheet2.Cells(Rows.Count, "B").End(xlUp).Row).Formula = .Range("B2").Formula
End With
to copy sheet1 cell B2 formula down as many rows as Sheet2 column B last not empty cell row index
For every Range(), Cells(), Rows() and Columns() object specify in which sheet they are. Don't miss any of them or your code can randomly fail.
Example
Worksheets("Sheet1").Range("A1") 'addresses cell A1 in Sheet1
You can use variables as references:
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
ws.Range("A1") 'now also addresses cell A1 in Sheet1
while
Range("A1")
will address the sheet that has focus (is on top) while the code runs. Don't rely on this because this can easily change by a single mouse click of the user. Therefore always specify which sheet you mean.

Copy Pasting Range of Cells with Dynamic Start Point

I'm having trouble copy pasting a range of cells based on a dynamic start point (the start point is 1 cell beneath a cell with a specified value).
This specific range of cells will always be in columns A-Z, but the row is dynamic across worksheets. However, this range of cells is always preceded by a row above with a specific cell value, let's say "Dataset Here".
So in an example sheet, I need to copy A650:Z700, and the cell "Dataset Here" is in A649.
How can I copy a range based on the requirement that the range falls underneath the cell with the value "Dataset Here"?
I was able to identify the starting row, but am lost how to turn it into a dynamic version of Range("A650:Z700").Copy:
Dim StartRow As Long
StartRow = Range("A:BA").Find("Dataset Here").Row + 1
Hello Crystal and Welcome.
You could do something like the below. This works with the assumption that all your cells and columns below the 'Dataset Here' are populated (otherwise the copy range might not be correctly defined). The following example copies cells A6:B9 to cell F1
Option Explicit
Public Sub sCopyCells()
Call sCopyCellsForWorksheet(Worksheets("Sheet1"))
End Sub
Private Sub sCopyCellsForWorksheet(ByRef ws As Worksheet)
Dim target As Range, source As Range, destination As Range
Set target = ws.Range("A:A").Find("Dataset Here")
Set source = Range(target.Offset(1, 0), target.End(xlDown).End(xlToRight))
Set destination = ws.Range("F1")
source.Copy
destination.PasteSpecial xlPasteAll
End Sub

Insert new row in a different sheet and paste value

I am trying to write a code for inserting a new row in sheet "Sheet3" in which the first column value is a value I have stored as a Range.
I have made a formula for identifying value in the active sheet, as follows>
Dim cellTaxa As Range
Set cellTaxa = ActiveSheet.Buttons(Application.Caller).TopLeftCell.Offset(1, -2)
End Sub
Basically, the above mentioned formula stores a value in a cell that is one cell down and two cells left from the button that I clicked.
The next thing i want to do is to insert a new row in Sheet 3 in which the first column value is cellTaxa.
I couldn't find anything that will work on my structure.
Any help or hint is appreciated.
Thank you!
Option Explicit
Public Sub yoursub()
Dim cellTaxa As Range
Set cellTaxa = Sheets("Sheet1").Range("G3") 'replace with your range
'Would be even better to not use range for a single cell
' E.g.: Set cellTaxa = Sheets("Sheet1").Cells(3,7) OR Cells(3,"G")
'same format can be applied for the .pastespecial part
Sheets("Sheet3").Range("A1").EntireRow.Insert 'adds empty row at "A1"
cellTaxa.Copy
Sheets("Sheet3").Range("A1").PasteSpecial 'pastes CellTaxa into the newly created row
End Sub

Use pivot table cell value to lookup same value in original worksheet

In my original worksheet of data I've created a VBA script that creates a hyperlink for cell values in one column, column "A".
The displayed text in the column "A" cells after running the script is the original cell value. This script works just fine.
This worksheet data is then used to create a pivot table. Of course the hyperlinks are gone in the pivot table.
What I'd like to be able to do is click on a cell value in the pivot table and have it look for that value in the original worksheet and then open the hyperlink.
I'm using Excel 2013 and the pivot table is the classic pivot table view.
Solution: Let's say this is your source sheet, called Sheet1:
Sheet1
Cell A2 is linking to a website. And your second sheet, Sheet2 contains a PivotTable of the data in Sheet1:
Sheet2
If I understand your request correctly, you want a click on "Peter" in Sheet2 to open the URL linked in the cell containing "Peter" in Sheet1. The way you can accomplish that is by adding the below code to Sheet2 (right click Sheet2 in the tabs row and select "View Code").
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Columns.Count = 1 And Target.Rows.Count = 1 And Target.Column = 1 Then
Dim SourceSheet As Worksheet
Dim SourceRange As Range
Dim HyperlinkAddress As Range
Set SourceSheet = ThisWorkbook.Sheets("Sheet1")
Set SourceRange = SourceSheet.Range("A:A")
' Find cell address in Sheet1
On Error Resume Next
Set HyperlinkAddress = SourceSheet.Range("A" & _
Application.WorksheetFunction.Match(Target.Value, SourceRange, 0))
If HyperlinkAddress.Hyperlinks.Count = 1 Then
' Click hyperlink
ThisWorkbook.FollowHyperlink (HyperlinkAddress.Hyperlinks(1).Address)
End If
End If
End Sub
Explanation: The event Worksheet_SelectionChange is fired when you select a different cell in an Excel sheet. The above code will first check whether you selected a (and only 1) cell in column A and if you did, find the row number corresponding to the cell containing the value of the cell you clicked but in Sheet1. Once it's found, it's verifying the cell contains a link address and - if it does - click it.

Resources