Renumber rows after move - excel-formula

I have a spreadsheet with 25 rows of tasks. If a task becomes a hot topic I need to move or renumber the row to bring it to the top and I would like excel to renumber the rows below in ascending order. (ie. if I change 19 to number 1 the everything else would move down and renumber in ascending order).

So, simple example, just to demonstrate:
So, you can sort and include column A or not as you wish.

Say our tasklist is like:
place this code in the worksheet code area:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column <> 1 Then Exit Sub
Cancel = True
Rows("1:1").Insert Shift:=xlDown
Target.EntireRow.Copy Range("A1")
Target.EntireRow.Delete
End Sub
When wife tell you she has gone into labor, double-click cell A4 and the task list becomes:

Related

VBA Excel: Write timestamp to cell on change of another cell

I want to insert a timestamp (E3) when the status (B3) changes. This should happen for at least 30 more such examples in the worksheet. The code currently works only for one example (Country1). Do you have an idea how this can be implemented?
I already tried different types but it just worked for example "Country 1" not for "Country 1", "Country 2", "Country 3" etc.
When I adjust the code for the range "B3:I3" then I received an adjustment in every 3rd column, example: I add a comment in D3 then a timestamp will be created in H3. That is not what I want. :(
Is there a way to adjust the code so that as soon as a change is made in the Status column (B3;F3;J3etc.), the Timestamp column (E3;I3 etc.) will reflect the time stamp?
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("B3:B5"))
Is Nothing Then Exit Sub
Application.EnableEvents = False
Target.Offset(0,3).Value = Now
Application.EnableEvents = True
Please, try the next adapted event. It will calculate how many groups of four columns exists and set a range of their first column intersected with rows 3 to 5. Only for this range the event will be triggered:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lastCol As Long, rngCols As Range
lastCol = Me.cells(2, Me.Columns.count).End(xlToLeft).column 'last column on the second row
Set rngCols = Me.Range(trigData(Me.Range("B2", Me.cells(2, lastCol)))) 'create the range of the columns for what the event to be triggered
Set rngCols = Intersect(Me.rows("3:5"), rngCols) 'create the range inside which the change to trigger the event
If Not Intersect(rngCols, Target) Is Nothing Then
Application.EnableEvents = False
Target.Offset(0, 3).Value = Now
Application.EnableEvents = True
End If
End Sub
Function trigData(rngCols As Range) As String
Dim i As Long, strCols As String
For i = 1 To rngCols.Columns.count Step 4 'iterate from four to four and create the necessary columns string address
strCols = strCols & "," & rngCols.cells(i).EntireColumn.address
Next i
trigData = Mid(strCols, 2) 'Mid eliminates the first (unnecessary) comma...
End Function
The code will be confused if you place on the second row data after the necessary groups of four columns. If necessary, one or two such columns, the code can be adapted to work for a fix number extracting the divided integer (without decimals).
The code assumes that you need to be triggered for the mentioned rows (3 to 5). If you need something different in terms of rows to be affected, you should change Me.rows("3:5") according to your need.
Please, send some feedback after testing it.
Your request is a little unclear, and your table format may not have come across correctly in your post. Your code is written to add the current time to a cell 3 columns away from the target cell. It is dynamic, so if you set
If Intersect(Target, Range("B2:I3"))
You are going to get the value in cell 3 columns offset from the changed cell. If you always want it to update column E, then you can use the target.row property...
Cells(Target.Row,5).Value = Now
...to make the row dynamic, and the column static. Clarify your question if this is not what you're looking for. If country2 is in cell F2, where do you want to write the timestamp?
You can use this simple function:
Public Function TimeStamp(Status As Range) As Double
TimeStamp = Now
End Function
So, in Cell E3 will be the formula =TimeStamp(B3). (Format cell E3 appropriately as Time Format)

How can I simplify or loop excel vba code for data exchange in different sheets?

I'm making calculation form where you can enter element quantity and select materials from drop down list on sheet1 (all info will go to the sheet2) and in sheet2 you can change same materials that were set in sheet1 to other from drop down list and it automatically update info in sheet1. I have the code that works perfectly(see code below) and it works both ways.
Question is that the code for checking and changing the values is ok with me but it has to to be repeated for about 10 different positions(ranges) that are defined next to 51 different element calculations and it need to be entered 1000+ times. If there is possibility to somehow group that 10 position with one code similar to "loop" then it would be repeated just for 100+ times :)
the idea is to be able to adjust "materials"in both sheets see picture
enter image description here
Main code is in sheet and yes it is _change
calculation sheet (sheet1)
Private Sub Worksheet_Change(ByVal Target As Range)
Call FloorElementExchange1
End Sub
main sheet(sheet2)
Private Sub Worksheet_Change(ByVal Target As Range)
Call FloorElementExchange2
End Sub
and all another code that actually do the check and replacement of value is in module (sub FloorElementExchange1()) that goes like this:
sub FloorElementExchange1()
If Worksheets("CALCULATION").Range("N67") <> Worksheets("main sheet").Range("F44") Then
Worksheets("main sheet").Range("F44").Value = Worksheets("CALCULATION").Range("N67").Value
End If
sub FloorElementExchange2()
If Worksheets("main sheet").Range("F44") <> Worksheets("CALCULATION").Range("N67") Then
Worksheets("CALCULATION").Range("N67").Value = Worksheets("main sheet").Range("F44").Value
End If
give this a try , your If condition isn't needed here
Sheet1 :
Worksheets("main sheet").Range("F15").Value = Worksheets("CALCULATION").Range("N17").Value
Sheet2 :
Worksheets("CALCULATION").Range("N17").Value = Worksheets("main sheet").Range("F15").Value
Note : Your check is useless and waste time .. Try without this will be quite faster

My question is how can i set stop data entry when i exceed the limit of 3 countifs of same participant?

My question is if I entered "JOHN" 4 times then show me a error or stop message
"You exceed more than 3, Enter a new name"
"max of participants list is 3"
This should work, or at the very least, get you started. The macro will only be called upon when a cell is changed in Column B. When fired, the macro will display a message and clear the entire row of the changed cell IF the Participant name exists more than 3 times in the column.
You do not need to keep Column D for this macro as it calculates the count independently. There is no harm in leaving it there either though if you need to see the remaining possible counts (1, 2, 3)
To implement, paste this code on the worksheet in VB Editor where your code is stored.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 Then
If Application.WorksheetFunction.CountIf(Range("B:B"), Target) > 3 Then
Application.EnableEvents = False
MsgBox "Participant Already Has 3 Entries!"
Target.EntireRow.Clear
Application.EnableEvents = True
End If
End If
End Sub

VBA-Excel. How can I handle hide/unhide rows based on Private Sub Worksheet_Change(ByVal Target As Range)?

Good morning to all,
I am a newbie in macros & VBA Excel. I wish to handle hide/unhide rows based on Private Sub Worksheet_Change(ByVal Target As Range). I have the following code event
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Unprotect Password:="avalon"
If Target.Column = 2 Then
Application.EnableEvents = False
Cells(Target.Row, 5).Value = Date + Time
Application.EnableEvents = True
End If
ActiveSheet.Protect Password:="avalon"
End Sub
The idea is to aggregate this event (hide/unhide rows) when I entry data from 'B4'to 'B21' (Sometimes they will be filled totally and sometimes partially. It does not matter). Maybe, clicking in 'B21'or another event using double click in certain cell to activate unhide rows from 22 to 36 for follow up fill those rows. I hope to be clear, if not let me know to try to clarify my need to discover the proper code based on events.
Thanks in advance. I promise to learn quickly.
It seems that what you are trying to do is to automatically hide or show rows based on the selection.
You can show or hide rows by using the .hidden on a range object (using EntireRow). You just need some way of determining when to hide these rows. By using the selectionChange event, you can show or hide rows based on what rows are currently selected.
The following code, when put on a worksheet object, will help you.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Select Case Target.Row
Case 21
ActiveSheet.Range("B22:B36").EntireRow.Hidden = False
Case 22 To 36
ActiveSheet.UsedRange.EntireRow.Hidden = False
Case Else
ActiveSheet.Range("B22:B36").EntireRow.Hidden = True
End Select
End Sub
You can create more cases and give the row numbers that, when selected, will hide or show other ranges.
I hope I understood your question correctly.

excel clear dependent dropdownlist vba

After all, I already have the dropdownlists, the dependencies etc. and it works well, ALSO I have a vba code that, when the user change one value from the dropdownlist parent, the dependence clear their contents. BUT
That only works with that cell...
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("C2")) Is Nothing Then
Range("D2").ClearContents
End If
End Sub
Obviusly because I'm tellin vba that only C2 & D2, but What I want it's that somebody would help to figure it out how to make it the entire column, not just that specific cell, like (column - 1 ) or something... because if I copy paste those dropdownlists only works in the first one cause that is specified...
Anybody? Any ideas? please.
Here there are some pics
In the pictures above, the dropdownlists work only in that specific cell, I tried what Hol told me with the function Cells(row index,columnIndex) but I need a for or something like that isn't it ? This is the first thing I'm doing in vba so I dont have a clue and I'm looking for examples and then trying, it takes too long hahaha,
I've already tried instead of "C2" , Column(3) and in D2 Column(4) but I have an error in the conditional If Not Intersect(Target, Range(Column(3))) Is Nothing Then
As far as I understand you want your macro to works in pairs: any change in C column (as of 2nd row bottom direction) will clear cell in D column in the same row. If so the following code does the trick.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Target.Column = 3 Then
Target.Offset(0, 1).ClearContents
End If
End Sub

Resources