I need to compare a cell to the one below it in a loop. i know for most languages you can say something like "if cells(i,1).value = cells(i+1,1).value then..."
is there a way to do this in vba for some reason it isn't working for me. thanks
For i = 7 To ltrw
If (Cells(i, 1).Value = 0 And Cells(i + 1, 1).Value = 0 Then
Cells(i, 1).EntireRow.Hidden = True
End If
Next i
like this ?
For i = 7 To ltrw
If Cells(i).Value = Cells(i + 1).Value Then ' you can skip the ", 1" as its optional
Cells(i).EntireRow.Hidden = True
End If
Next i
Related
I have a piece of code in which i would like the for loop to continue to the next value of i if the value in a cell is "nil" and not execute the commands in the loop. I am unable to figure it out.
For i = 2 To n
If .Cells(i, "G").Value = "nil" Then
next i
Else
mon = month(.Cells(i, "G").Value)
acctyp = .Cells(i, "P").Value
end if
next i
Thanks in advance.
The comparison operator <> means "not equal to":
For i = 2 To n
If .Cells(i, "G") <> "nil" Then
mon = Month(.Cells(i, "G"))
acctyp = .Cells(i, "P")
End If
Next i
Got any Way to Arrange The data like the picture attached?
*the requirement is when the Column A Cell is empty then Column B Value will Move to previous not Empty Column A Row at Column B Cell.
Example Photo
Try this code
Sub Macro1()
Dim i As Long
i = 1
Do Until Cells(i, 2) = ""
If Cells(i, 1) = "" Then
On Error Resume Next
Cells(i - 1, 2) = Cells(i - 1, 2) & "," & Cells(i, 2)
Cells(i, 2).EntireRow.Delete
i = i - 1
End If
i = i + 1
Loop
End Sub
The value of the cell is always "error" no matter if the condition is true or not. I've tried using else but it doesn't work either.
Sub ejer_4()
Cells(3, 1).Value = "hola"
For i = 2 To 21:
If Int(Cells(i, 3).Value) <> Int(Cells(i + 1, 3).Value) - 1 Then
Cells(3, 1).Value = "Error"
End If
Next
End Sub
I think that should solve your problem:
Sub ejer_4()
For i = 2 To 21:
If Int(Cells(i, 3).Value) <> Int(Cells(i + 1, 3).Value) - 1 Then
Cells(i, 1).Value = "Error"
Else
Cells(i, 1).Value = "hola"
End If
Next
End Sub
You problem is that you loop thorugh range of cells, but after checking condition you write into one cell. So, this cell will contain oonly the result of the last check. Previous values will be simply overwritten.
I am trying to set up a user form to do a loop and look up information in my table which is in a separate worksheet within the same workbook.
I want my user form to look up information in my table as I type and then auto fill in the other textboxes so that I can limit keystrokes and duplicates.
I found some code that worked with another user form as desired. However, when I try to use the same code for my table it goes through the loop like it’s looking but it does not populate the user form. I have tried changing with the user forms textbox names and making sure the names match... but to no avail. I also have to skip over a combo box on my user form, can this effect my code?
Option Explicit
Dim id As String, i As String, j As Integer, flag As Boolean
Sub GetData()
If Not IsNumeric(UserForm1.TextBox1.Value) Then
flag = False
i = 0
id = UserForm1.TextBox1.Value
Do While Cells(i + 1, 1).Value <> ""
If Cells(i + 1, 1).Value = id Then
flag = True
For j = 4 To 7
UserForm1.Controls("TextBox" & j).Value = Cells(i + 1, j).Value
Next j
End If
i = i + 1
Loop
If flag = False Then
For j = 2 To 4
' UserForm1.Controls("TextBox" & j).Value = ""
Next j
End If
Else
End If
End Sub
try qualifying your ranges up to worksheet reference:
Option Explicit
Sub GetDataA()
Dim id As String, i As String, j As Integer, flag As Boolean
If Not IsNumeric(UserForm1.TextBox1.Value) Then
flag = False
i = 0
id = UserForm1.TextBox1.Value
With Worksheets("myTableWorksheetName") '<--| change "myTableWorksheetName" to your actual worksheet with table name
Do While .Cells(i + 1, 1).Value <> ""
If .Cells(i + 1, 1).Value = id Then
flag = True
For j = 2 To 7
UserForm1.Controls("TextBox" & j).Value = .Cells(i + 1, j).Value
Next j
End If
i = i + 1
Loop
End With
If flag = False Then
For j = 5 To 10
UserForm1.Controls("TextBox" & j).Value = ""
Next j
End If
End If
End Sub
i have been having problems with getting my code to run through its conditional loop and stopping. Here is what I have:
Do While True
Dim i As Integer
i = 2
If Cells(i, 1).Value <> "" And Not IsError(Cells(i, 2).Value) Then
Range(Cells(i, 1), Cells(i, 22)).Copy
Range(Cells(i, 1), Cells(i, 22)).PasteSpecial
i = i + 1
Else
Exit Do
End If
Loop
What I'm trying to do is to get the program to check if one cells isn't empty and if another doesn't have an error in it, if that condition is met, then the program would copy a certain row and re-paste it as just it's values since some of the cells in the row is a formula. For some reason the loop doesn't exit and Excel crashes, am I missing something?
the i = 2 should be outside
Dim i As Integer
i = 2
Do While True
If Cells(i, 1).Value <> "" And Not IsError(Cells(i, 2).Value) Then
Range(Cells(i, 1), Cells(i, 22)).Copy
Range(Cells(i, 1), Cells(i, 22)).PasteSpecial
i = i + 1
Else
Exit Do
End If
Loop
Two points :
The i = 2 must be outside the while loop.
Don't use Copy and PasteSpecial. Using the clipboard will give lots of problems later on. Additionally PasteSpecial likes you to be specific with "what" PasteSpecial action you're using. Rather assign values directly.
Dim i As Integer, Dataset As Variant
i = 2
Do While True
If Cells(i, 1).Value <> "" And Not IsError(Cells(i, 2).Value) Then
'This seems silly, but it overwrites the cell with its value.
Dataset = Range(Cells(i, 1), Cells(i, 22)).Value
Range(Cells(i, 1), Cells(i, 22)).Value = Dataset
i = i + 1
Else
Exit Do
End If
Loop