I have a cell (S1) that has a dynamic value in it on 'sheet 1' on 'sheet 2' I would like to take the value of (S1) and copy the state of the (S1) cell - to the second sheet but retaining the value before it was changed. I would like this to be done dynamically is there a way?
With vba you can get the value like the following, just link the macro to a button:
Worksheets(2).Range("s2").value = worksheets(1).Range("S1").value
Or you can cut and paste special, values only
If you prefer using vba you need to paste the code to sheet1 ( code section).
When the value in cell S1 of Sheet1 changes then Worksheet_Change event will be called and it will copy the new value of the cell (it does not retain the old value). This will be be helpful if you want to create a log of all the values for S1 Sheet1.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Application.EnableEvents = False
Dim rng As Range
Set rng = Range("S1")
If Not Intersect(Target, rng) Is Nothing Then
' Sheets("Sheet2").Range("S1") = Target.Value
Sheets("Sheet2").Range("S" & Sheets("Sheet2").Range("S" & Rows.Count).End(xlUp).Row + 1) = Target.Value
End If
Application.EnableEvents = True
End Sub
Try this:
Sheets("Sheet1").Range("S1").Copy
Sheets("Sheet2").Range("S1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
This is a simple way of transferring value of S1 from Sheet1 to S1 of Sheet2.
Im not sure how your Sheet1-S1 value changes but you can put this code in between the transition.
Related
First of all I am super new to VBA and coding in general, however, I am building an excel workbook to automatically transfer a row in a table based off a single cell in a row. when this happens I need it to copy only the values in the cells as I have several formulas. when the copy paste operation is done I need to delete the row and re-order everything to the top while not deleting the formulas of the row. below is what I have got so far which mostly works for what I need. the only issues are it copies the entire row so I cant have a merged group of cells to the right of the row and it deletes the formulas from the cells.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 8 Then
If Target = "COMPLETED" Then
Set Tbl = Sheets("PMI ARCHIVE").ListObjects("Table3")
Tbl.ListRows.Add
nxtTblRow = Tbl.ListColumns(9).Range.Rows.Count
Target.EntireRow.Copy _
Destination:=Tbl.Range(nxtTblRow, 1)
Application.EnableEvents = False
Target.Row.ClearContents
Application.EnableEvents = True
Range("A1", Range("A1").End(xlDown)).Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes
End If
End If
End Sub
Try the next code, please. It assumes that you need to copy all existing values of the Target row and then clear contents of the cells not having a formula:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim arrCopy As Variant, lastColT As Long
If Target.Column = 8 Then
If Target = "COMPLETED" Then
Set tbl = Sheets("PMI ARCHIVE").ListObjects("Table3")
tbl.ListRows.aDD
nxtTblRow = tbl.ListColumns(9).Range.Rows.Count
lastColT = Cells(Target.row, Columns.Count).End(xlToLeft).Column
arrCopy = Range(Target.row, lastColT).Value
tbl.Range(nxtTblRow, 1).Resize(, UBound(arrCopy, 2)).Value = arrCopy
Application.EnableEvents = False
Range(Target.row, lastColT).SpecialCells(xlCellTypeConstants).ClearContents
Application.EnableEvents = True
'If in column A:A, an empty cell will exist (because of the above code), the range will be set up to that empty cell.
'The next way, goes to the last cell:
Range("A1", Range("A" & Rows.Count).End(xlUp)).Sort Key1:=Range("A1"), _
Order1:=xlAscending, Header:=xlYes
End If
End If
End Sub
I am not sure you need to sort only A:A column, but I kept the code as it was, from this point of view...
I need to copy a few cells from a form on sheet Form, then paste them into a new row on sheet Activities.
Breaking it down:
When the button is clicked:
The cells "B2,B3,B4,B5,B6,A10,A16,A21,A24,E10,E17,E20,E23,E26,I10,I12,I14,I16,M10,M12,M14,M16,M19,M22" will be selected on the active sheet (Form) and copied.
The copied cells are pasted on another sheet (Activities) and pasted on a new row (something like A + 1)
This is what I have so far:
Private Sub CommandButton1_Click()
Sheets("Form").Select
Range("B2,B3,B4,B5,B6,A10,A16,A21,A24,E10,E17,E20,E23,E26,I10,I12,I14,I16,M10,M12,M14,M16,M19,M22").Select
Selection.Copy
Sheets("Activities").Select
If Sheets("Activities").Range("A9") = "" Then
Sheets("Activities").Range("A9").PasteSpecial Paste:=xlPasteValues
Else
Sheets("Activities").Range("A10").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues
End If
End Sub
But it's not working properly, and I haven't managed to figure out the A+1 yet.
First of all, You should avoid Select statement if it's not necessary (it raises events unnecessarily, etc.).
My approach would be:
Dim rng As Range
Set rng = Sheets("Novo Pedido").Range("B2,B3,B4,B5,B6,A10,A16,A21,A24,E10,E17,E20,E23,E26,I10,I12,I14,I16,M10,M12,M14,M16,M19,M22")
For Each cell In rng
'here you copy to another sheet, one row lower
Sheets("Geral").Cells(cell.Row + 1, cell.Column).Value = cell.Value
Next cell
i want a macro to paste values from clipboard ( values that i have copied from another source)
in transpose to last non empty cell in row E in worksheet "sheet1"
any help?
Try something like this:
Sub WriteFromClipboard()
Dim refRow As Integer
Dim Data As DataObject
Dim DataText As String
On Error Resume Next
'Get row to write data:
refRow = 1 + ThisWorkbook.Sheets("Sheet1").Columns(5).Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
If Err.Number > 0 Then
refRow = 1
End If
Err.Clear
'Get data from clipboard:
Set Data = New DataObject
Data.GetFromClipboard
DataText = Data.GetText
'Write data from clipboard on the spreadsheet:
If Err.Number > 0 Then
MsgBox "Clipboard doesn't contain valid data."
Else
ThisWorkbook.Sheets("Sheet1").Cells(refRow, 5).Select
ThisWorkbook.Sheets("Sheet1").Paste
End If
End Sub
function findLastRowInCol(wks as excel.worksheet, col as long)
dim rng as range
with wks
set rng = .cells(.rows.count, col).end(xlup)
findLastRowInCol = rng.row
end with
end function
To test from the Immediate pane:
debug.print findLastRowInCol(thisworkbook.sheets("sheet1"), 5)
This will give you the row of the last non-empty cell in the specified column. Note that this may not be the last used row of the worksheet; selecting a different column may give you a different result depending which is that column's last non-empty cell.
Edit: replaced activesheet with "sheet1"
Edit: sample code to use in another macro to paste in from some other source,
thisworkbook.sheets("sheet1").cells(findLastRowInCol(thisworkbook.sheets("sheet1"), 5), 5).pastespecial paste:=xlAll
If pasting from another Excel sheet or to strip formatting, you can use e.g. "xlPasteSpecialValues"
Edited to add: to paste a row from another worksheet and transpose, use the above as far as ".pastespecial" then use some variation on:
.pastespecial paste:=xlpastevalues, transpose:=True
The simplest implementation is,
sub PasteToE()
thisworkbook.sheets("sheet1").cells(findLastRowInCol(thisworkbook.sheets("sheet1"), 5),5).pastespecial paste:=xlpastevalues, transpose:=True
end sub
Add a commandbutton to the other worksheet and assign this macro to it. Select your range and copy it, then click the button.
I have a workbook where i have the following formula in cell D3.
"=VLOOKUP($C3,'[NIGHT ROTA.xlsx]15'!$A$5:$I$32,D$1,0)"
The source workbook has 52 sheets and when i want to search a certain sheet i have to change the sheetname manually and then run the code below to copy it across the cells in my workbook using worksheet change.
Is there a way to have the sheetname as a variable that i would have in a cell in my workbook or that would be chosen from a drop down? I know i could use INDIRECT but don't want to have to open the source workbook.
Sub WeekChange()
Range("D3").Select
Selection.Copy
Range("D3:I26").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A1").Select
End Sub
You can put the sheet names in a drop-down cell, say E1 (or any of your choice) and capture the change in this cell:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("E1").Address Then
Range("D3:I26").Formula = "=VLOOKUP($C3,'[NIGHT ROTA.xlsx]" & Target.Value & "'!$A$5:$I$32,D$1,0)"
End If
End Sub
To populate the drop-down box in E1, you can eventually set it upon the opening of the workbook, but since you dont want to open it (I guess you have some reasons), you might have to populate the validation of E1 manually.
First thing I did was create a button that would copy certain cells using this code:
Worksheets("Sheet1").Range("A:A,B:B,D:D").Copy _
and it worked fine.
Second, I found the code that would copy all details in a row based on the criteria of one, in this case if there was an "A" in the "Location" column.
Private Sub ENTIREROW_Click()
'Sub copyrows()
Dim i As Range, Cell As Object
Set i = Range("D:D") 'Substitute with the range which includes your True/False values
For Each Cell In i
If IsEmpty(Cell) Then
Exit Sub
End If
If Cell.Value = "A" Then
Cell.ENTIREROW.Copy
Sheet2.Select 'Substitute with your sheet
ActiveSheet.Range("A65536").End(xlUp).Select
Selection.Offset(1, 0).Select
ActiveSheet.Paste
End If
Next
End Sub
My question is, how do I copy all information in the specified columns (A,B,D) where there is an "A" in "Location" in one button.
Furthermore, this is my example data, the sheet I will actually use this on has 34 columns to copy. Is there a more efficient way of setting a range when you don't want an entire sequence, everything but the data in column C?
Thanks in advance and apologies for my explanation skills.
One way maybe to:
filter your source
hide column C
copy the result using .PasteSpecial xlPasteValues into the destination
Unhide column C on the source sheet
remove the autofilter
Using xlPasteValues only pastes the visible cells from the source - so no column C
The code then looks like this: .
Sub CopyRows()
With Sheets(1).Range([A2], [A2].SpecialCells(xlLastCell))
[A1].AutoFilter
.AutoFilter Field:=4, Criteria1:="A"
[C:C].EntireColumn.Hidden = True
.Copy
[C:C].EntireColumn.Hidden = False
End With
With Sheets(2)
If .Cells(Sheets(2).Rows.Count, 1).End(xlUp) = "" Then 'it's a clean sheet
.Cells(Sheets(2).Rows.Count, 1).End(xlUp).PasteSpecial Paste:=xlPasteValues
Else
.Cells(Sheets(2).Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
End If
End With
Application.CutCopyMode = False
Sheet1.[A1].AutoFilter
End Sub