Deleting While Looping VBA Code

Aug 15, 2012

I have a worksheet and the products starts from the first row has product name and 2 following rows are other information which i do not need. i was trying to figure out a VBA code that can leave the first rwo and delete every 2 rows below the first one and then to complete this until the end of the data.

For example: in the following rows, i want to keep rows 1, 4, 7 and all others to be deleted and this process to continue until the last rows having data.

Column A
1 Product name
2 serial number
3 manufucture date
4 Product name
5 Serial number
6 manufucture date
7 Product name
8 serial number
9 manufucture date

Sub Deletesecondtworows()
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row

View 4 Replies


Looping Through And Deleting Rows If Cells Contain A Value

Apr 8, 2014

I have a normally easy task that is causing me some grief. In column f of my data I either have a number or this "____________". My goal is to delete the entire row if that line is present but my code is not finding that value in my range.

I am wondering is this some format value or something, but it shows up in the formula line as a line. Doesn't appear to be an underline, but I could be wrong. Below is my code.

[Code] ........

View 9 Replies View Related

Looping Code

Apr 28, 2007

I am using the following code in a project and would like some help to loop it until either TRUE or #N/A is found in the active cell.

View 9 Replies View Related

Looping VBA Code Until End Of Data?

Jul 2, 2014

trying to get macro to loop until there's no data to process.I have data in two columns A&B which it creates a chart from I then need it to skip C (which is blank) and do the same chart for columns D&E and so on indefinitely until there is no data to process..

I have attached a very simplified version of the data, the data is made up so the charts don't make sense but if you run the macro you'll see what I'm trying to achieve.

View 4 Replies View Related

Code Keeps Looping 3 Times

Jan 21, 2010

Im having a bit of an issue getting my head around why I am getting the result I am. I have data in ws1, a search list in ws2 and the results of the search in ws3.
It works though for some reason it loops 3 times... not sure why.

Additionaly I want to be able to run the same sub against 2 work sheets, ie, ws1 and another ws titled "otherWS" for this example. So run the exact sub again and return the results to ws2 as well.

View 4 Replies View Related

Looping Code Not Ending Correctly

Dec 29, 2013

I have the following code that I use to create individual client files from one master spreadsheet. My problem is that it always creates and saves a blank file with only the 'rn' value in the file name. It seems that it is looping one extra time when the 'rng.value' list ends.

Sub MakeFiles()
Dim wb As Workbook
Dim ws As Worksheet
Dim criteriaRng As Range, usedRng As Range, rng As Range
Dim lh As String, ch As String, rh As String
Dim rn As String


View 1 Replies View Related

Code To Find Range Without Looping

May 4, 2009

As a part of a much larger routine, I need a code to find five consequetive cells in a column with identical value "XYZ" and select the first one of them. Say,

I have a column:

I need to select the twelfth cell in this column, highlighted red.
I guess there could be some complicated if-then loop to go through the whole column, checking each cell and comparing it to the next four, but it would take forever to excecute... Is there some other way around? To find the first occurence of five xyz's one directly under another?

View 9 Replies View Related

Looping Code & AutoFilter Criteria

Aug 12, 2006

I'm looking to use Autofilter and current region to select each persons data and paste to a new sheet.

The trouble is i'm not sure how to change the criteria name in the loop

e.g first name in list is Jack, next is John

Selection.AutoFilter Field:=1, Criteria1:="Jack"

View 9 Replies View Related

ComboBox Change Code Looping

Jan 10, 2007

I have a piece of code attached to a combo box. When selected the code loops through 3, even 4 times for no apparent reason. Has anyone come across this before? The loop can start either after the final "end sub" or part way through (.clearcontents mainly) Have included the code just in case it is something obvious.

Private Sub cmbDI_GrowthBasis_Change()
If UCase(Worksheets("Tables"). Range("GrowthNo")) = "NIL" Then
Worksheets("Detail Inputs").Range("DI_GrowthRateTitle") = ""
With Worksheets("Detail Inputs").Range("DI_GrowthRate")
.Borders(xlEdgeLeft).LineStyle = xlNone
.Borders(xlEdgeTop).LineStyle = xlNone
.Borders(xlEdgeBottom).LineStyle = xlNone
.Borders(xlEdgeRight).LineStyle = xlNone
.Interior.ColorIndex = 15
.Locked = False
End With
Worksheets("Detail Inputs").Range("DI_GrowthRateTitle") = Worksheets("Tables").Range("GrowthNo") & " :"
End If
End Sub

View 2 Replies View Related

Looping Code Error: Repeatedly On One Sheet

Apr 22, 2008

I have a peice of code (Below) and instead of going from work sheet 4 to the one in the workbook 2 from the end sheet by sheet running the same bit of code it just loops repeatedly on one sheet??

Sub a()

Dim n As Integer
For n = 4 To ThisWorkbook.Sheets.Count - 2
Selection.Insert Shift:=xlToRight
Selection.Delete Shift:=xlToLeft
Selection.AutoFill Destination:=Range("L8:W9"), Type:=xlFillMonths
Application.CutCopyMode = False
Next n
End Sub

View 9 Replies View Related

Deleting Event Code With Code

Sep 2, 2006

Is is possible to use VBA to remove/delete a macro and also remove code like this on worksheets:

Private Sub Worksheet_Change
End Sub

Private Sub Worksheet_Activate()
End Sub

View 4 Replies View Related

Macro Code For Deleting Blank Row?

Jan 23, 2013

I've a file with lots of data. In that file I've some blank rows, I want a code which search complete blank row and delete.

View 2 Replies View Related

VBA Code Keeps Deleting Leading Zeros

Jul 23, 2013

My following code keeps deleting leading zeros. The purpose of this code is to delete leading spaces but leave the zeros.

lr = Cells(Rows.Count, 1).End(xlUp).row For Each c In ActiveSheet.Range("A2:A" & lr)
If Left(c.Value, 1) = " " Then c.Value = Right(c.Value, Len(c.Value) - 1)
c.Value = c.Value
Next c

View 2 Replies View Related

Deleting Code On The Saved File

Jan 11, 2008

I have a template that when it is opened it will automatically save the file to a specific location with the date in the file name.

Currently my code is

Private Sub Workbook_Open()
ActiveWorkbook.SaveAs "G:Shared"IT_New " & Format(Date, "mm-dd-yy") & ".xls", , , , , , xlShared
End Sub
Does anyone know a way to have this code disabled (either by deleting or making it all a comment) on the saved file (xls) so the speadsheet does not try and overwrite itself when I go back and open a previous days spreadsheet?

View 9 Replies View Related

Filtering And Deleting Rows With VBA Code

Aug 19, 2008

I've got a row of data with a column showing some sales including amounts with 0. Any row with 0 I want gone, so I want the macro to filter the list, delete all the rows with 0 in that column, and then display what's left, all the rows with some number other than 0 in that column.

The code below should filter the 0 rows, delete those rows, and then display what's left. I don't want the heading row deleted, but everything below it that is filtered. What my code below is doing now is not deleting everything it should. How can I fix it.

Sub prepmetrics()
' prepmetrics Macro
' for use with monthly report - don't forget the new coversheet
Application.ScreenUpdating = False

Selection.AutoFilter Field:=14, Criteria1:="0"

' this is the challenge here. How do I tell Excel to select, relatively, whatever
' rows are now showing as a result of the filter?

Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp

' there may be a number of different criteria in different columns, so
' we rinse and repeat as many times as necessary but, again, each filter/delete
' task should delete all rows filtered

Selection.AutoFilter Field:=16, Criteria1:=">7500"
Selection.Delete Shift:=xlUp

Application.ScreenUpdating = True

End Sub

View 9 Replies View Related

Getting #VALUE Error When Deleting Blank Rows With VBA Code

Jul 13, 2009

I have this code that I have had for a while.. it works okay on my computer and does as intended... it deleted all empty rows in the selected range after checking to see if any cells contain anything that makes it look blank but isn't (it cleans those cells).

So on my computer it works... on a coworkers computer it converts every used cell in the usedrange to #VALUE...

View 14 Replies View Related

Security Prompt Even After Deleting Macros Using VBA Code

Jun 27, 2007

I have used the following Macro that deletes the Macro code. It then saves the file and quits excel.

Sub removeAllCode()
Dim awi
Dim awcl As Integer
Dim count As Integer
Dim i As Integer 'loop counter
On Error Resume Next
count = ActiveWorkbook.VBProject.VBComponents.count
For i = 1 To count
Set awi = ActiveWorkbook.VBProject.VBComponents.Item(i)
awcl = awi.CodeModule.CountOfLines
awi.CodeModule.DeleteLines 1, awcl
Next i
Set awi = Nothing
End Sub

However when I open the Excel File, I still get the Macro Prompt. I have checked in Macro list as well as in VB Editor and no Macro code exists

If I have the file manually (or do a save as) and then reopen the file, I no longer get the Macro prompt.

I have tried various examples listed in different forums/sites but nothing works.

View 9 Replies View Related

Adding And Deleting Columns, Ruins Code

Jul 21, 2008

I have a spreadsheet with 20 or so columns but that number can change at any time. I need my code to by dynamic so if a column is deleted (or added) my code will still work.

Each column has a name in row 2 and I'm trying to use that in order to make everything dynamic. For example, my code counts the number of blue cells in colums D4:G68, but now if someone adds a column before column D, everything gets messed up and it will still count the cells in D4:G68 but I want to use E4:H64 now. Does that make sense? I have made code that searches the column names and returns the column number to correspond to the name. How would I change this?

Range ("C8") = "= CountBlue(E4:G68)"

the code doesn't look exactly like this, I believe it is actually in R1C1 format

View 9 Replies View Related

VBA Code For Deleting Columns With 0 Value In Specific Cells

Oct 24, 2008

I am trying to write a VBA code in excel to delete any columns when a cell in that particular column = 0 (zero). That cell would start at B19 and subsequently follow to C19, D19 and so on.

View 9 Replies View Related

Code Deleting Non-Existent Module Causes Error

Apr 11, 2007

I have used the code that was previously suggested in this forum for deleting a module. I have module 2 looping into module 1.At the end of module 1

I tell it to delete itself,likewise for module 2.

I placed in the "this workbook" on open event, the code

Application.Run" CALENDAR.xls!TEST

In the "this workbook" I have also put code in the "before close" event:

If Me. Saved=False Then Me.Save

All is great until the end user reopens the workbook and it locks up on the workbook open event because there is no modules to run now.

View 6 Replies View Related

VBA Code For Deleting Rows Where Cell In Column A States

Mar 20, 2009

I'm trying to get a VBA macro put together that would delete all the rows, throughout every sheet, and shift up wherever the cell in column A = "Delete Row" in a workbook I've made. In the below copy of the workbook you can see that a lot of the information from all the tables is linked to the topmost table on the 'Mix Detail Sheet'.

If the the Cell in column A is blank on that first table, its corresponding cells in tables throughout the workbook displays the words "Delete Row". I need the macro to delete the rows and shift everything up where this occurs.

Ideally I'd also like the macro to then delete the rows in that first table where the column A cells are blank.

View 5 Replies View Related

Delete The Contents Of Every Cell Without Deleting Any Code From My Module

Apr 16, 2009

I want to delete the contents of every cell without deleting any code from my module. The reason I ask is, I'm reading from a text file into excel.

I want to run my code to read text into excel one time through. Then i want to delete all the text, make some alterations to my code, and run the code again. That way each time the worksheet is fresh and clear before i run the code.

View 4 Replies View Related

Code To Stop A User Adding Or Deleting Sheets

Aug 31, 2007

I need to stop users from adding and deleting sheets in a workbook

My idea was obviously to disable the command bars to add or delete sheets in open event and then put back in before close.

But then i thought...whats stopping them from right clicking the sheet tab and inserting a sheet, can i remove that menu to...?

I also wasnt sure if there were short cut keys to add or delete sheets.?

Some of my users use excel alot so i want to account for an tips they know that i might not.

View 9 Replies View Related

Shortening VB Code That Deals With Deleting Row If Type Of Word Is Found?

Sep 17, 2012

I have a file with over 20,000 rows that contain a date (Mon~Sun), What I need to do is remove every day that contains Mon~Sat and only keep Sun, this is the code that I have come up with and is working.

Dim c As Range
Dim SrchRng
Set SrchRng = ActiveSheet.Range("A1", ActiveSheet.Range("A" & Lastrow).End(xlUp))3


View 1 Replies View Related

Deleting Multiple Worksheets Which Ends With The Word Data Through Vba Code

Jun 14, 2007

I have many worksheets which ends with the word " data". Is there a way to delete all the data sheets at one go through VBA code.

View 3 Replies View Related

Code For Deleting Rows Verses Hiding Rows

May 25, 2013

The first code hides everything just fine based on the date in A1. When I change it to the second code to delete instead of hide it is leaving a bunch of rows that the 1st code hides. Both codes have the same search criteria.

For Each cell In Range("B8:B5000") If cell.Value Range("A1").Value Then cell.EntireRow.Hidden = True
Next cell

For Each cell In Range("B8:B5000") If cell.Value Range("A1").Value Then cell.EntireRow.Delete
Next cell

View 4 Replies View Related

Code Only Deleting One Empty Row Instead Of All Empty Rows

Jan 22, 2012

I am using the following block of code, which cycles through the data and first deletes any cell with "Legal:" in it, and then cycles through again and deletes any row where the cell is blank.

The problem is that within the data, there are some locations where there are two blank rows in a row. When the code runs through, it deletes the FIRST blank row only, not the second. I Need ALL blank rows within the data set to be deleted.

Sub ModifyNewData()
Dim r As Range, rAll As Range
Dim WS As Worksheet
Dim iLast As Integer

[Code] ........

Also, if there is a way to write looking for blank rows into the first block of code looking for "Legal:" that would be cool too.

If not, just deleting all the blank rows is good. Right now, I have to have the second block of code run twice to get rid of the remaining blank rows.

View 4 Replies View Related

Looping For 01 To 10

Aug 8, 2014

I know how to loop for 1 to 10.

But is it possible to loop for 01 to 10?


[Code] ......

Because I am pulling data from external aplication, so the 0 is fix in a row/column.. I will need to get the row/column for the whole "01" and not only "1".

View 2 Replies View Related

For Next Looping

Aug 18, 2009

I am not sure what I am doing wrong with this expression. I know the code is a little messy, sorry.

View 2 Replies View Related

Looping Without Using VBA

Aug 8, 2009

Is it possible to write a cell formula (not a macro) to loop through a range of cells and test for a value? Something tells me that I need to use the "Offset" function.

View 9 Replies View Related

Copyrights 2005-15, All rights reserved