Replacing IF with loops [duplicate] - vba

This question already has answers here:
VBa conditional delete loop not working
(4 answers)
Closed 8 years ago.
I have a spreadsheet with a load of values in and a button.In the code button I have the
following VBA code
If Range("N2").Value = "100" Then
Rows ("2:2").Select
Selection.Delete
End if
The idea is that if 100 is placed in that row then that row will be deleted.But obviously there will be problems if I have loads of rows,this will mean loads of "IFs"
My question is this,is there any way this could be done with a loop.So I could jsut loop through all the rows and if the conditions are met then that Row will be deleted.In the If statement above I have to specify the rows selected,so how would a loop do this??
Any help greatly appreciated.
kind regards
j

Set a range and do the following loops
For Each row In rng.Rows
For Each cell in row.Cells
'Do Something
Next cell
Next row

Related

How to delete rows based on empty cell value in VBA [duplicate]

This question already has answers here:
Code in VBA loops and never ends. How to fix this?
(2 answers)
Closed 5 years ago.
I am trying to delete entire rows based on whether the cell value in the D column is NULL or not. My entire code so far is:
Sub DeleteNULL()
Dim i As Long
For i = 2 To 119713
If IsEmpty(Range("Di")) = True Then
Rows([i]).EntireRow.Delete
Else
If IsEmpty(Range("Di")) = False Then
Next i
End If
End Sub
I keep getting compile errors, either If without Else or Next without For, how should I fix this?
Thanks in advance.
A few things:
Placement of a lot of syntax is off.
When adding or deleting rows, you need to loop backwards based on how Excel handles these events.
See code below:
Sub DeleteNULL()
Dim i As Long
For i = 119713 To 2 Step -1
If IsEmpty(Range("D" & i)) Then Rows([i]).EntireRow.Delete
Next i
End Sub

How to get last column used in excel using vba [duplicate]

This question already has answers here:
Find last used cell in Excel VBA
(14 answers)
Closed 5 years ago.
Hi I am having issues getting to the last column used in my sheet.
I do know that as it stands my last column is 43 and i want it to enter a value at column 44, so then when i search again it will go to column 44 as last column used and then enter a value at 45 and so on and so on. I am using a ref num to find the correct row.
The code I am using is as follows to try get the last column
'find last empty Col in database
LastCol = sou.Cells(2, Columns.count).End(xlToLeft).Column + 1
MsgBox LastCol
'For Loop to cycle through the columns
For x = 2 To LastCol
'If the row value at column 1 = the ref number then continue
If sou.Cells(x, 1).Value = refNum Then
sou.Cells(x, LastCol).Value = Me.commentBox.Text
Exit For
End If
Next
The issue is that for some reason it only goes to column 22. I have tried to use the other ways to get the last column but they have just given me the last column in the whole entire sheet which again is not what I want.
If someone can lend me a hand as I'm a newbie to this it would be greatly appreciated!
To find the last row used, have you tried
lastRow = ActiveSheet.UsedRange.Rows.Count
If that didn't work, please elaborate what went wrong and I can submit more code.

Can't get excel macro to return after a for loop [duplicate]

This question already has answers here:
VBA macro to delete rows quickly
(4 answers)
Closed 5 years ago.
I'm trying to make a little code that deletes an entire row when certain text is written in a cell.
Sub Delete_Rows()
For Each c In Range("B1:B20").Cells
If c.Value = "text" Then
c.EntireRow.Delete
End If
Next c
End Sub
This is what I wrote in my excel sheet
The problem is when I run it and the condition to delete a cell is met, excel automatically scrolls to the next row without evaluating the current cell, it skips like this
I run it again and only "text" from numbers 4 and 9 remain
After I run it a third time it finally deletes every cell with "text" written in it.
I tried to use a while loop instead, offset the selection, use an integer to subtract the loop iteration and several other ways and have had no luck.
Any ideas?
Two options are:
delete from the bottom up, or
delete all relevant rows at once (shown below):
Sub Delete_Rows()
Dim deleteRange As Range
For Each c In Range("B1:B20")
If c.Value = "text" Then
If deleteRange Is Nothing Then
Set deleteRange = c
Else
Set deleteRange = Union(c, deleteRange)
End If
End If
Next c
deleteRange.EntireRow.Delete
End Sub

Excel check if same value exist across all sheets [duplicate]

This question already has answers here:
Excel - Using COUNTIF/COUNTIFS across multiple sheets/same column
(4 answers)
Closed 7 years ago.
I have an excel file which has 82 sheets in it, all sheets have the same title in column A but on Column B has different status, think of it as a task name on column A and its status on Column B.
I want to check if the status of task on Column A5 is same across all sheets or how many times the status is critical and how many times status is ok
If I was using a single sheet I know the following formula would give how many tasks have critical status on the same sheet
=COUNTIF(B5:B24,"critical")
But I want to check how many times B5 has status critical across all sheets, how can i do this?
Any assistance will be really appreciated.
This code iterates through all the sheets, and creates a formula that sums up all the countifs
Then, it stores the formula in A1
s = ""
For i = 1 To Sheets.Count
s = s & "+COUNTIF(" & Sheets(i).Name & "!B5:B24,""critical"")"
Next
range("a1")=s

1 single row of cells, i want to copy it into multiple rows of 10 cells each [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
in excel i have a very long row of values that were imported into excel.
For simplicity sake, lets say i have 1 row with 100 cells with values of 1 to 100.
I was wondering if there was an easy way to copy it so that it would result in a table 10x10 with row 1 being 1 to 10, row 2: 11 to 20, etc
I tried using the offset function but was unsuccesful.
You could use vba (assuming the row of 100 numbers is row 20)
for i = 1 to 10
for j = 1 to 10
cells(i, j) = cells(20, j + 10 * (i - 1))
next j
next i
If you want accurate help, please provide a sample of what you've done so we can assist, rather than expecting us to code this for you. Still, this is a fairly simple snippet based on what you are asking for:
Dim i As Integer
Sheets(1).Activate
For i = 1 To 10
Range(Cells(1, (i * 10) - 9), Cells(1, (i * 10))).Copy
Sheets(2).Cells(i, 1).PasteSpecial
Next i
This copies from the top-most row of the first sheet and makes it a 10x10 matrix on the second sheet.
One simple way to deal with this problem without macros:
Create an offset value in your column and row heading
Combine the offset value of the row and the column to get you total offset.
Use this value to retrieve the proper cell using =OFFSET(...
Alternatively, refer to this post to do it with a VBA Macro.
https://superuser.com/questions/700104/transpose-data-in-columns-to-rows-downwards
Without VBA:
If you have data in the first row, then pick a cell, say cell B3 and enter this formula:
=INDEX($1:$1,1,(10*ROWS($1:1)-10)+(COLUMNS($A:A)))
Then copy B3 from B4 to B11
Then copy B3 thru B11 downwards
Here is an example::
Both VBA and non-VBA solutions will work as shown in other answers. A simpler non-VBA formula (doesn't need the values in row 1) is:
=(ROW()-1)*10+COLUMN()
That assumes your data starts in A1. If your starting cell is say D4, you could also do something like:
=(ROW()-ROW($D$4))*10+COLUMN()-COLUMN($D$4)+1