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
Related
I'm trying to select the true last cell data set then insert a subtotal into the cell below. Everything I've tried selects the last cell of my visible data. I need the last cell of the entire sheet.
For instance, my filtered data will have rows 3:10 visible. My code is selecting row 10 when I need it to select the last row of the entire dataset that's hidden (row 100).
This will be performed on multiple sheets at once.
Here's my code:
Sub Example()
Dim LastRow As Long
With ActiveSheet
LastRow = .Range("J2").SpecialCells(xlCellTypeLastCell).Row
End With
Call SubTotalMacro
End Sub
Thanks!
Try this:
MsgBox ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
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
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
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.
I have a spreadsheet I'm using to compile text that changes all the time.
In column AD, Row 4(AD4) I put the contents of text, and it can have data going 1000 to 4000 rows down. It changes every time, so there is no static range name. I need a macro that
finds the final piece of data in that column,
then automatically "drags a box" from that spot two columns to the left (AB4)
and copies it... (A 3000 row piece of text would be AB4:AD3004) (Macro stops there, with text to be copied highlighted)
The current version finds the bottom cell correctly, but if I run the macro a 2nd time, with new data, it keeps trying to copy the same range. (I used the Formula Define.Name method, to name the cell, and then selected AB4:LastRow) but it is ALWAYS 3160 whether data goes to row 4000 or not.....
Sub Last_row()
Cells(Application.Rows.Count, 30).End(xlUp).Select
' following lines of code are useless
Range("AB4:AD3160").Select
Range("AD3160").Activate
Selection.Copy
End Sub
To answer your question directly:
With Sheet1
.Range("AB4", .Cells(Rows.Count, "AD").End(xlUp)).Copy
End With
Copy to specific location WITHOUT using clipboard:
With Sheet1
.Range("AB4", .Cells(Rows.Count, "AD").End(xlUp)).Copy Sheet2.[A1]
End With
Copy and exclude formatting:
With Sheet1
With .Range("AB4", .Cells(Rows.Count, "AD").End(xlUp))
Sheet2.Cells(1, "A").Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
End With
Note: Replace all sheet codenames (sheet1, Sheet2) above with your actual sheet codenames.
Your current code hard-codes the range of interest with
Range("AB4:AD3160").Select
This code will define a dynamic range starting from AB4 to the last non-empty cell in column AD
You can then use this range (without selecting) for changing values elsewhere (note that you may not need to actually copy rng1, it is possible to dump these values to a separate range directly without a copy and paste.
Sub Last_row()
Dim rng1 As Range
Set rng1 = Range([ab4], Cells(Rows.Count, 30).End(xlUp))
rng1.Copy
End Sub
Update: Example of how to copy a dynamic sized range from one sheet to another without a copy and paste:
Sub Last_row2()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng1 As Range
Set ws1 = Sheets(1)
Set ws2 = Sheets(2)
Set rng1 = ws1.Range(ws1.[ab4], ws1.Cells(Rows.Count, 30).End(xlUp))
ws2.[a1].Resize(rng1.Rows.Count, rng1.Columns.Count).Value = rng1.Value
End Sub