Copy cell to anoter worksheet with variable row number - excel

How do I copy the content of a cell to a cell in another worksheet with a variable row number?
I've searched this site and came up with the following code but nothing appears in the destination worksheet.
Worksheet "Koersen" is automatically updated.
Cell A19 in "Koersen" should be copied to cell A3 in "ASML" and when cell C7 in "Koersen" changes it should be copied to A4 in "ASML" and then to A5, A6 and so on.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim x As Integer
For x = 3 To 1500
If Target.Address = "$C$7" Then
Sheets("Koersen").Cells(19, 1).Copy
Sheets("ASML").Cells(x, 1).Paste
End If
Next x
End Sub
As you may have guessed, I'm a complete newbie to VBA
Thank in advance.

Try the following, although I can't clearly see what you want to achieve..
Private Sub Worksheet_Change(ByVal Target As Range) 'if something change in worksheet...
Dim x As Integer
Worksheets("ASML").Cells(3, 1) = Worksheets("Koersen").Cells(19, 1) 'make value of ASML A3 equal to Koersen A19
If Target.Address = "$C$7" Then 'if what have changed is cell C7...
For x = 3 To 1500
Worksheets("ASML").Cells(x, 1) = Target.Value '...copy value of Koersen C7 to ASML column A, from row 3 to 1500
Next x
End If
End Sub

I believe what you are trying to do is copy Koersen!A19 to a new row in ASML every time cell C7 changes. If so, the following code should work:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim x As Long
If Target.Address = "$C$7" Then
With Sheets("ASML") ' use a With block to save typing
x = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
Sheets("Koersen").Cells(19, "A").Copy .Cells(x, "A")
End With
End If
End Sub

Related

Excel VBA - range offset

I'm trying to make a code which after insert/update value in one cell, will go to the next cell like on the picture. Now this code works only one way:
ActiveCell.Offset(1,0).Select ex. from A2 to B2.
What to do to return offset to the cell on the left from B2 to A3 - need loop like on the picture.
I am not quite sure if I understood what you are looking!
I set a range on column A and B until row 10, just for testing.
This code moves to the next cell on the picture whenever you edit
the cell and then you press Enter.
Put this code on the sheet module
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngColA As Range: Set rngColA = Range("A2:A10")
Dim rngColB As Range: Set rngColB = Range("B2:B10")
If Not Intersect(Target, rngColA) Is Nothing Then
Target.Offset(0, 1).Select
exit sub
End If
If Not Intersect(Target, rngColB) Is Nothing Then
Target.Offset(1, -1).Select
End If
End Sub

VBA, deleting row/column values as increment function

Whenever I change a value (choose some value from data validation list) in column G, it should clear the cell in the next column H.
So when I choose value in G4, value in H4 will be deleted. When I choose value in G5, tha same would happen with H5.
I tried this, but not working:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 7 Then
For i = 4 To 154 Step 1
If Target.Address = "$G$i" Then
Range("Hi").Select
Selection.ClearContents
End If
Next i
End If
End Sub
No need of iteration for such a task. Since, the cell value is changed by selecting from a drop down validation list, multiple changes are not possible. For such a case, the code simple exists:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 7 Then
If Target.cells.count > 1 Then Exit Sub
Target.Offset(0, 1).ClearContents
End If
End Sub
This can be done like this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim oCell As Range
For Each oCell In Target.Cells ' You can change many cells at once (i.e. copy-paste or select-delete)
If oCell.Column = 7 Then ' Is it cell in G:G or any other?
If oCell.Text <> vbNullString Then ' Has cell any value?
oCell.Offset(0, 1).ClearContents ' Clear cell in the next column in this row
End If
End If
Next oCell
End Sub

Clear contents of cells in that row when a cell in the same row changes to apply to multiple rows

I have this code
Private Sub Worksheet-Change(ByVal Target As Range)
If Not Intersect(Target, Range(“M4”)) Is Nothing Then
Range("N4:T4).ClearContents
End If
End Sub
Which works for the 4th row When I change M4 it clears N4 to T4
I need a way to adapt this code so that if I change the value of M5 it deletes N5 to T5 and so on for all the rows.
Can you help
You need to intersect the range you want to observe for changes Me.Range("M4:M10") with Target and then use the Range.Offset property and Range.Resize property to move from the changed cell 1 right and resize it to 7 columns to clear contents.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim AffectedRange As Range
Set AffectedRange = Intersect(Target, Me.Range("M4:M10")) 'range to observe
If Not AffectedRange Is Nothing Then
Dim Cell As Range
For Each Cell In AffectedRange.Cells
Cell.Offset(ColumnOffset:=1).Resize(ColumnSize:=7).ClearContents
'or
'Me.Range("N" & Cell.Row & ":T" & Cell.Row).ClearContents
Next Cell
End If
End Sub
Test Target for if it contains a cell in M, and use its row to clear the relevant cells
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cl as Range
For Each cl In Intersect(Target, Me.Columns(13))
If cl.Row >= 4 Then
cl.EntireRow.Cells(1, 14).Resize(1, 7).ClearContents
End If
Next
End Sub

Show value of clicked cell and another value from the same row in VBA Excel

I am using following macro to show value of clicked cell (range A5:A200) in A1 cell:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("A5:A200")) Is Nothing Then
Range("A1").Value = Selection.Value
End If
End If
End Sub
...and it works great. Additionally in A2 I'd like to show related value from C column.
Example:
I click on A10 -> and see the value in A1, then in A2 i'd like to see the value from C10 cell
You can utilize the .row property of Selection. But, you already have Target being used from the arugument within the Worksheet_Selection() event as your "Selection" range, and using that range is preferred over using selection.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count = 1 Then
If Not Intersect(Target, Range("A5:A200")) Is Nothing Then
Range("A1").Value = Target.Value
Range("A2").Value = Cells(Target.row, "C")
End If
End If
End Sub
So replace your selection ranges with Target, and use the row property within Cell() to get your value from the C column.

show numbers starting from 1 based on a cell value using vba

show numbers starting from 1 based on a cell value using vba.
For example, if you type 4 in cell A1, then range D1,D2,D3,D4 should display 1,2,3,4 respectively. If type 5 in cell A2, then range D1,D2,D3,D4,D5 should display 1,2,3,4,5.
Could anyone please help?
The below macro uses a worksheet change event to detect changes to the cell A1,
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" And IsNumeric(Range("A1")) Then
Dim i As Long
Columns(4).ClearContents
For i = 1 To Cells(1, 1)
Cells(i, 4) = i
Next i
End If
End Sub

Resources