I need check if any cell in Range is empty, using VBA in Excel. If so, then write a messing box.
I have data similar to this
ID ColumnB
1 a
2 aa
3
4 ab
I tried many codes, but always it write the messing box, even when the columnB has no empty cell.
One of the code is
For Each cell In Range(Range("A4").End(xlDown).Offset, Range("A4").End(xlDown).End(xlUp)).Offset(0, 1)
If IsEmpty(cell) = True Then
GoTo Done1
End If
Done1:
MsgBox "In column B are empty cells.", vbInformation
Next cell
I have data from row 4 and Column A doesn't have empty cell (that is why I use Offset())
I always see messing box 5 times (because the range has 5 rows), even when I modify the data, so ColumnB doesn't have any empty cells.
I also tried
If cell.Value = "" Then
Or
If WorksheetFunction.CountA(Range(Range("A4").End(xlDown).Offset, Range("A4").End(xlDown).End(xlUp)).Offset(0, 1)) = 0 Then
' GoTo Done3
And many others, but nothing work.
Any idea?
Thanks
Related
I have a file with a bunch of rows that contains data for certain part numbers from different configurations. Some of these part numbers are repeated throughout the file, and in those duplicated part numbers may contain certain data and some may not. I am trying to find the best way to determine the commonalities in the file for certain data. So for the commonalities, if one row has a value and another row is blank, the value for the nonblank row would be put into the blank row. And if the data on those two rows were different it would change the font color on the cell indicating that this part number two different unique values and should be checked.
Dim i, j, n As Long
Dim lr As Long
Dim moaf As Workbook
Dim sht As Worksheet
Application.ScreenUpdating = False
Set moaf = Workbooks("MOAF3.xlsb")
Set sht = moaf.Worksheets("Wire Data")
n = InputBox("What column # are you trying to fill in?: ")
lr = Cells(Rows.count, 2).End(xlUp).Row
For i = 2 To lr
lkup = Cells(i, 2).Value 'sets first lookup value
Fill = Cells(i, n).Value 'sets the first data value to compare
If Len(Fill) > 0 Then
For j = 2 To lr
lkup2 = Cells(j, 2).Value 'sets the second lookup value
Fill2 = Cells(j, n).Value 'sets the second value to compare
If lkup2 = lkup Then 'checks to see if p/ns are same
If Len(Fill2) = 0 Then 'checks to see if second value is blank
Cells(j, n).Value = Fill 'if value is blank the cell takes value of non blank cell
ElseIf Fill <> Fill2 Then 'checks to see if the values are non matching and non zero
Cells(i, n).Font.ColorIndex = 3 'changes font color of two cells
Cells(j, n).Font.ColorIndex = 3 'changes font color of two cells
End If
End If
Next j
End If
Next i
Application.ScreenUpdating = True
End Sub
Doing this generally freezes my excel, where my computer has 32GB of RAM and is Windows10. Is there a better approach for my problem, or is it something that can be done without using a vba? I've done some research on a method without using vba, but with like sumifs, countifs but haven't really done any deep dives.
So, if I understand your question correctly, you start with following data:
ID Column_header
2 a
3 _BLANK_
4 _BLANK_
5 b
6 _BLANK_
And you want to turn this into:
ID Column_header
2 a
3 a
4 a
5 b
6 b
I know a very simple trick for that (I have put everything in column 'A' for explanation):
Select every cell inside that column
Goto (Ctrl+G) Special, Blanks
In the formula bar, type =A2 (you are currently located in 'A3', and you want to copy there the value of the cell just above it)
Press Ctrl+ENTER
You'll see that 'A2' gets copied into 'A3', 'A3' into 'A4' and 'A5' into 'A6' (the fact that this is done for all blank cells, is due to the Ctrl+ENTER).
Record this into a macro, and it will go much faster.
I already see a question popping up : "Ok, but what about the font colour I want to change?". Well, the newly filled cells are based on a formula, so the length of =FORMULATEXT() won't be zero. You use this as a basis for conditional formatting.
Good luck
The inner for loop just needs to start at i, that is:
for j = i to lr
This should roughly half the runtime.
Further performance enhencements:
Use .Value2 instead of .Value property.
Or even better, read in the entire columns into an array, work on that in VBA, then write the result back.
I copy a lot of information (1000 rows and at least 24 columns) from one sheet to another. A lot of the cells contains "". This makes my other formulas(for example: A1-B1) to show an value error if either of these cells contains "".
I believe I can solve the problem by never pasting "" but a "0" instead. But I would like to delete these "0" afterwards.
There could be values in the first 3 rows but the other 997 rows have "".
I would think I need to tell my macro to (Cell A1 in the "sheet1" sheet displays "G5:H12". the cells I need to delete):
Rowstodelete = Sheets("sheet1").Range("A1").Value
Sheets("sheet1").Range("rowstodelete").clearcontent
This does not work. anyone know how to do this?
Summary(new example)
If cell A1 = "B1:B2" I want to clear the content of B1 and B2, but if A1 now = B4:B6, that is the cells that should be cleared.
Try this one:
With Worksheets(1).Range( _'PLACE YOUR RANGE
)
Set c = .Find(0, lookin:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Value = ""
Set c = .FindNext(c)
Loop While Not c Is Nothing
End If
End With
Hope it helps
Anyhow, I think that will be simpler to place a condition in your operation formula: =IF(OR(A1="",B1=""),Make when there is an unexpected value,A1-B1)
I am trying to Create a Formula that checks 4 Cells next to each other if they have the same number once positive and once negative see in the example:
If the formula sees there is a Plus 50 and a Minus 50 its has to colour the cell on the right side or the cells with the numbers blue.
The list is a inventory of multiple stores if one store sells alot of that product and may run out they ask another store to restock the product. Sometimes they forget to send a note. This List is supposed to make the control as easy as possible.
I expect the formula to color the cell on the right side of the list to be colored blue if 2 of the cells have the same value in plus and minus.
I tried to use cell formatting rules but its not possible to do it with that.
Another Example since people seem to have trouble understanding what the formula should do:
I marked every cell blue like the formula should and yellow colored value is the reason.
You can do this, using conditional formatting, using this formula (just for the first row):
=OR(A1+B1=0;B1+C1=0;C1+D1=0)
This formula checks if the sum of two adjacent cells equals zero, which is another way of saying that they should have the same value, but opposite signs.
Obviously, you might consider changing this formula, e.g.:
Instead of:
A1+B1=0
you put:
AND(A1+B1=0;A1<>0)
When the sum of two values equals zero and at least one of them is not zero, then both are not zero.
All this together in one formula yields the following:
=OR(AND(A1+B1=0;A1<>0);AND(B1+C1=0;B1<>0);AND(C1+D1=0;C1<>0))
Use such a formula in the conditional formatting of cell E1, and apply this for all cells in E column.
Try:
Option Explicit
Sub test()
Dim Row As Long, Column As Long
Dim rng As Range
'Let us assume that we use Sheet1 & columns A to F
With ThisWorkbook.Worksheets("Sheet1")
For Row = 2 To 100 ' <- Let us assume that data starts in row 2 and ends in row 100
Set rng = .Range("B" & Row & ":E" & Row)
For Column = 2 To 6
If .Cells(Row, Column).Value <> 0 Then
If Application.WorksheetFunction.CountIf(rng, (-1 * .Cells(Row, Column).Value)) > 0 Then
.Range("F" & Row).Interior.Color = vbBlue
Exit For
End If
End If
Next Column
Next Row
End With
End Sub
I would like to write a macro which goes through all cells in column and if it finds any formula in cell then it simply ignores this cells and simply 'DO NOTHING'.
I know about HasFormula property in VBA but not really sure it works for cells where I sum two cells simply (for instance, =C4+C5). Here is an example:
'loop (if the cell has formula then loop ignores that cell
i = 1
For i = 5 To 100
If InStr(1, Sheets("Corporate Detailed").Cells(i, "F"), "+") > 0 Then
could be also used---'If Sheets("Corporate Detailed").Cells(i, "G").HasFormula = True Then
Else
'Do some operations
End If
Next i
End Sub
I've imported the results of a broken link search in to Excel and I now need to sort the results based on the error code. I can't get my head around how to do this because the error code is in the row below the URL and not in a column next to it. Also, some URLs take up more than one row.
Here is a screenshot of part of the spreadsheet:
How would I go about grouping all results with error 404 together?
Below you'll find a VBA code that do what you need. As I don't have the original sheet I create an excel and put some random data. Works fine for me.
Sub test()
Dim row, rowDest, rowAux As Integer
Dim source As Worksheet
Dim dest As Worksheet
'Replace here by the name of the worksheet that contains the data
Set source = Worksheets("Sheet1")
'This is the sheet where the modified data will be placed
Set dest = Worksheets("Sheet2")
'Start row (source sheet)
row = 1
'Start row (dest sheet)
rowDest = 1
'This is an auxiliary variable used to fill the error code column
rowAux = 0
'Go to the last line (column 1) of your source sheet and write the string !q
'This will be used as a flag to know where your data finish
While (source.Cells(row, 1).Value <> "!q")
If (InStr(source.Cells(row, 1).Value, "http") > 0) Then
dest.Cells(rowDest, 1).Value = source.Cells(row, 1).Value
If (rowAux = 0) Then rowAux = rowDest
rowDest = rowDest + 1
ElseIf (InStr(source.Cells(row, 1).Value, "error code") > 0) Then
While (dest.Cells(rowAux, 1).Value <> "")
dest.Cells(rowAux, 2).Value = source.Cells(row, 1).Value
rowAux = rowAux + 1
Wend
rowAux = 0
End If
row = row + 1
Wend
End Sub
My dataset and results:
Source sheet:
Dest sheet:
Insert a column to the left of A and fill in with a sequence of numbers (1, 2, 3, ...). Now sort by column B. Select all the error code entries and drag them to column c (or some other empty column). Resort the sheet by the sequence of numbers in column A. Now with everything ordered and the error codes is a separate column (C), you can right-click on C1, and select shift cells up. Column A can be deleted, and you can sort by the URL's (although it looks like you clean up the text a bit).
This is going to be difficult to do unless you can get the error code in the same row as the URL. However, you can still do it by using the SEARCH function on the error code. This will find the 404 error, but it won't give you the URL in the cell beneath it. Therefore, you need a function that checks to see if the cell beneath it's cell has found a 404 code. Then you can filter on the "true" values and get two rows.
Create a new column in A, and use the function below.
=IF(ISNUMBER(SEARCH("404",B1)),1,IF(A2=1,2,0))
Filter down on 1 and 2 values.
Solution based on http://office.microsoft.com/en-us/excel-help/check-if-a-cell-contains-text-HP003056106.aspx and the function:
=IF(ISNUMBER(SEARCH("v",A2)),"OK", "Not OK")
Subject to some constraints (but they are not in your Q!) this formula in an inserted ColumnA may serve:
=INDEX(C1:C3,MATCH("*error code:*",C1:C3,0))
along with =ROW() in an inserted ColumnB (though could be elsewhere) and both copied down at the same time.
The formulae should be converted to values (copy ColumnsA:B, Paste Special..., Values over the top) and sorting be based on ColumnA then ColumnB. The rows blank in ColumnC may be deleted, as also those with error codes in that column.