I am trying to create a loop in Excel to insert an entire row when there is a value change in column A. It goes through the loop once and works perfectly. It inserts the row like it is suppose to but when it loops and the value changes again it jumps straight to end sub rather than inserting a new row. I have tried to do this multiple ways.
Below is the simple loop I made to see if it works. This code works from the bottom up.
Do Until ActiveCell = ""
If ActiveCell.Value <> ActiveCell.Offset(-1, 0).Value Then
ActiveCell.EntireRow.Insert
ActiveCell.Offset(-1, 0).Select
Else
ActiveCell.Offset(-1, 0).Select
End If
Loop
It reads to me like you are attempting to circumvent the Change event. I suggest reading the documentation particularly the Worksheet_Changedocumentation. If you try and implement the Change event and are still having issues, I suggest you edit your original question to provide a bit more clarity and offer more information.
I'm assuming that this loop is part of a larger piece of code. If so, make sure that the code is returning to the bottom of the sheet (or whatever location is desired). Since you are telling the loop to stop at an empty cell, running the code again without resetting the position of activecell will automatically find and empty cell and exit the loop.
As you said, the actual code for adding the blank rows itself works well, so it makes sense to look at the code around it.
#Bradon As I can see your code,it might be going into infinite loop [assuming you are using WorkSheet_Change event to catch any change in sheet]
Any change in worksheet trigger WorkSheet_Change event; in your case inserting row into worksheet is triggering the Worksheet_Change event
I will suggest you to post your entire code if possible
or
change you code like below [still assuming you are using WorkSheet_Change event]
add new sheet say "Config" and only once manually set A1 value to "True"
In worksheet change event use if condition
if Sheets("Config").Range("A1").Value = "True" then
call MyProcedure
end if
In Procedure
sub MyProcedure()
Sheets("Config").Range("A1").Value = "False"
your Do while loop here
Sheets("Config").Range("A1").Value = "True"
Related
I have created a spreadsheet which uses the Worksheet_Change feature and the code associated with that works very well. I can stop it firing unnecessarily when inside the module by using Application.EnableEvents = False.
However, while I've created a form to enter data directly into the next available row (again, that works fine in terms of entry) it doesn't cause the formulae in the sheet to calculate (even though auto calculation is on and re-enabled within the module). If I manually place my cursor in the row, hit F2 and simply press enter, everything then fires up.
I have tried to enter data directly into the cells, but of course the Worksheet_Change feature then kicks in again and the cursor isn't simply moving to the next adjacent cell ....
I've tried to check firs for any direct entry with the code below and if it looks like the user isn't entering directly into the cell, the Worksheet_Change is disabled:
Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo eventhandler
Sheets(1).Range("a1").Select
LastCell2 = Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row
Dim intersection As Range
Set intersection = Intersect(Target, Range("A3:F" & LastCell2))
If intersection.Row = LastCell + 1 Then
Exit Sub
End If
Application.EnableEvents = False
The code above is simply checking to see if data is being entered into the next empty cell and if that's the case I want it to just exit there but it isn't working.
So I actually have 2 problems :
the first is why this formula isn't triggering after entry via a vba form - I've used INDIRECT since there are other macros that delete rows by moving the remaining cells up and that was causing the count in the $A$3:$A$500 to reduce to $A$499 and then 498 etc - the addition is done depending on the system date and the transaction date so I get a current value and a future value using a standard sum statement:
=AD1-(SUMIF(INDIRECT("$A$3:$A$500"),"<="&TODAY(),INDIRECT("$E$3:$E$500")))+(SUMIF(INDIRECT("$A$3:$A$500"),"<="&TODAY(),INDIRECT("$F$3:$F$500")))
The second is why I can't enter data directly into the spreadsheet and trap the fact that I don't want it to do anything and simply allow the user to hit enter and move to the next adjacent cell to the one they just entered data into.
Is this a lost cause and am I trying to do too much here? I'm relatively new to coding and teaching myself so apologies if the standard and style isn't to everyone's taste.
Thanks in advance for any replies.
I'm very new to this. However, I've created a small VBA script in Excel. This script highlights the row the button is on, asks for a name and then inserts today's date and the name entered on to some cells in the row. It's basically used to book out media that is recorded on this spreadsheet.
The issue I have is that I'll need a CommandButton for every row, and I have no idea how to get the command button to change the the row values within the script to reflect the row that the button is on. Is this possible?
I think you should specify your question more clearly, but if I understand your question right, you need to write data in the next empty cell. If that is the case, you can use this:
ActiveSheet.Range("B10").Select
If ActiveCell.Offset(1, 0) <> "" Then
Selection.End(xlDown).Select
End If
ActiveCell.Offset(1, 0).Select
This will search for and select the next empty cell in column B, starting from B10. Hope this helps!
I have recorded a macro in Excel that takes data from certain cells in one page of a spreadsheet and copies them to page in a different order. it does each cell individually, and is quite long.
However it only does it for one row of data in the first page.
How do I make this single macro into a loop that copies the same data but from each row into the new page?
I haven't included the macro code since it is very long but can do so if necessary.
Thanks
You could omit your code to only show the relevant part, or code a mockup macro to address only and only your problem.
Lets imagine your problem as a sub, which in this case is highly omitted:
Sub OmittedSub()
' Do stuff
End Sub
You can create new sub to call it many times, this new sub would be the one you would be calling instead:
Sub LoopOmittedSubs()
Dim i As Integer
' Loop to call your macro routine multiple times
For i = 1 To 100
OmittedSub
Next
End Sub
In case you would need to pass a value, for example your macro does not know which row to affect and you would need to tell it, you can pass the loop variable like this:
Sub OmittedSub(iRow As Integer)
' Do stuff for row number iRow
End Sub
Sub LoopOmittedSubs()
Dim i As Integer
' Loop to call your macro routine multiple times
For i = 1 To 100
OmittedSub i
Next
End Sub
This answer is the very basics of VBA. I dont know how to answer your question better without knowing what you already tried, how you tried, etc...
Raybarg provided you basic solution to that problem. I am not sure what do you mean by "certain cell", does it mean they have certain value or there are in regular order like A5, A10, A15?
In both cases you probably will use Raybarg's code and conditional statements.
You should include your code, it gives some hints on what you actually want to achieve and where is error.
Is there a way we can trigger a macro function on column filter in excel??
Please help
Thanks.
I was just thinking if I can post this answer. I guess some of you will not like it as it is not direct answer by presentation of bypass solution. However I think I can show that idea as we don't have all project assumptions in the question.
Let's agree- we all know that there is no event which fires after we change filtering. However, I see one option.
Changing filter could fire Worksheet_Calculate event (not Worksheet_Change). If there is any single formula within your sheet than we will fire that event each time we change filtering criteria using our mouse.
Step 1. put any single formula in the sheet, like in cell ZZ1 where =ZZ2
Step 2. I assume that our data range starts in Range(A1) and we have titles in first row (see the picture). I assume also there is nothing below that area.
Step 3. Put that following solution in Sheet1 module.
Private Sub Worksheet_Calculate()
If ActiveSheet.Name = "Sheet1" Then
If Cells(Rows.Count, 1).End(xlUp).Row = 1 Then
MsgBox "No data available"
Else
MsgBox "There are filtering results"
End If
End If
End Sub
Step 4. Using filter would fire that event and result with following situations:
I hope someone will like it and can use that. Even if it's only a bypass idea.
can we execute a formula without moving cursor from the cell? After giving data in to Input cell either we press enter or we move from that cell and then any formula related to that gets executed. Is there any way that while entering input itself we can see the result in output in excel?Please help me in finding this option.
I don't believe it's possible for code to execute while the "editor" of a cell has focus and control. For example, try doing something else in Excel while you are editing a formula. I believe that the cell has to finish saving its value or formula before code can execute.
You can have a formula "listen" for when a cell is updated, but this is a little bit complicated. It could then force you back into the original cell once it is updated. Here's how you can write some code to "listen" for when a value of a cell changes. In the following example:
I record the value of Cell I1 to strValue.
Then I run a loop that stays running as long as the value of cell I1 has not changed.
The DoEvents command releases control back to the processor, which is what allows you to work in Excel while this is running.
Once the value of cell I1 changes, the While statement will be false and the loop will exit.
After the loop exits, a message box pops up that tells you the value changed and then the original cell is selected again.
'
Sub testChange()
Dim strValue As String
strValue = Range("I1").Value
Do While strValue = Range("I1").Value
DoEvents
Loop
MsgBox "The value changed!"
Range("I1").Activate
End Sub
Perhaps if you simply wanted to return control back to the original cell every time the value changed, you could use the line above without the message box and return to the loop once your method executes. Keep in mind that you might have to figure out another way to exit your code if you do that.
Why do you want to have it execute from within the cell? What is the end purpose of your task?
Press F9 to do a re-calc without moving from the input cell. This works in Excel 2007. I'm not sure about earlier versions.