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

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

Related

Comparing selected cell value to a cell range values inside parentheses

I fairly new in excel so don't know much about it or VBA.
I have this dataset shown below, In which first column has some values in it.
I wanted a conditional formatting or VBA formatting(don't know if this exists also!)
to color the data from the 3rd to 5th column based on the selected cell in the first column.
For eg. if I select dbo.project in the 1st column, all the cells having the dbo.project string
in it should be colored/highlighted.
Given Below is the image of my dataset.
The below code will activate whenever a single cell in column 1 is selected, and will color all cells containing the selection's text in columns C:E to vbGreen color.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Target.Column = 1 Or Not Target.Cells.Count = 1 Then Exit Sub
With ThisWorksheet
Dim ColorColumns As Range, Cell As Range, strMatch As String
Set ColorColumns = Intersect(.Range("C:E"), .UsedRange)
strMatch = "*" & Target.Text & "*"
For Each Cell in ColorColumns
If Cell.Text Like strMatch Then Cell.Interior.Color = vbGreen
Next
End With
End Sub

How to display value based on highlighted cell?

Is there a way for a cell in Excel to show a value based on just selecting another cell? Example:
Column A has a list of car manufacturers. Column B has a list of vehicle models. When the user puts the cursor in cell A4 then cell D2 would display the content of cell B4. If the user clicks cell A3 then cell D2 now displays the content of cell B3. Is there a way to put a formula in cell D2 for this purpose without any macros? It would change value based on selecting a different cell in column A.
The code given below should work. It will be triggered everytime you select a value in Column "Make".
Paste the below code in the VB editor :
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim currentRow As Long
If Selection.Count = 1 Then
If Not Intersect(Target, Range("A:A")) Is Nothing Then
currentRow = Target.Row
Range("D2").Value = Cells(currentRow, 2).Value
End If
End If
End Sub
Go to Excel Developer Mode (Alt + F11)
Doube Click The Sheet1(Sheet1) in Left Side
Paste The code And Save
Sub test()
Worksheets("Sheet1").Activate
Set selectedCell = Application.ActiveCell
Range("D2").Value = selectedCell.Row
End Sub
D2 is a your highlighted Cell
And Goto Excel Sheet and Open Macro
Select Cell & use ctrl+q

How to capture a cell value in Excel when a cell updated

I have a cell in Excel that gets updated on basis of RAND() function.
Each time I update the cell, I want to capture the previous values in a separate column. So that I can compute mean later on.
How to achieve this??
Copy below into Worksheet module and change "A1" cell:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Intersect(Target, Range("A1")) Is Nothing) Then
i = i + 1
Worksheets("Sheet1").Cells(i, 2).Value = Worksheets("Sheet1").Cells(1, 1).Value
End If
End Sub

Copy cell to anoter worksheet with variable row number

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

How to have the exact input time of row B1 in A1

is there is any formula to have the input time of B1 in A1..
or in row A1 how to get the exact time of input of B1..
.is it possible by excel formula
Dont know any way to do it with formulas, but you can use the worksheet change event like this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 Then ' did a value in column B change?
Cells(Target.Row, 1) = Now ' write date and time in column A
End If
End Sub

Resources