comparing two cells in certain pattern VBA - vba

I'm trying to compare two excel's cells in certain pattern using VBA. The table is separate by empty rows. And the patter is:
I'm looking at column A. If cell A1 is empty I'm moving to next one. If cell A2 is not empty then I'm compering $A2$ to B2, B3, B4, .. etc until I hit the empty cell. If so, then it moves to the next one (not empty), i.e. $A6$ and I'm continue to compering to B6, B7, ...
For defining the scope (range) of the loop I have the following:
LastRow1 = This.Sheets("List1").Range("A1048576").End(xlUp).Row
Which loop will fit this kind of pattern?
Many thanks in advance.

Correct me if I misunderstood you:
You want to loop through each line in column A till the last row.
Every time you have a value in column A, you want to compare this with the data in column B.
Maybe something like this:
LastRow1 = Range("A" & Rows.Count).End(xlUp).Row 'Determining the last row in column A
LastRow2 = Range("B" & Rows.Count).End(xlUp).Row 'Determining the last row in column B
For i = 2 to LastRow1 'Looping through every row
If Cells(i, "A") <> "" Then
For j = i to LastRow2 'looping through each row in column B from the same line as i in column A at that moment, column A6 f.e. will never compare with a value in B2:B5 for example, but only B6 till lastrow in column B
If Cells(i, "A") = Cells(i, "B") Then
<<do something if the value is found>>
End If
Next j
End If
Next i

Related

How to loop through rows starting in row 2 and find out if row is empty from column E to column AE

Basically I need to figure out how to loop through rows starting with row 2 column E to column AE and to the end of rows, find which ones are empty from column E over to column AE then place a letter code in that rows cell column d and place a different letter code if info is found in any cell in that selection (E thru AE) then of course move down one row and do the same until know info left in rows with column A as a reference on when to stop looping
If anyone can help I'd appreciate it.
Everything I have tried keeps blowing up, can't find a way to assign a value to multiple cell selections, at least that is how I have tried to attack this.
Hope someone can help
This will loop through Column E and if all cells in the active row to Column AE are empty it will put "E" in Column D of the active row, or a "F" in any cell has a value.
For Each cell In Range("E2:E" & Cells(Rows.Count, "E").End(xlUp).Row)
Dim rng As Range
Set rng = Range("E" & cell.Row & ":" & "AE" & cell.Row)
If Application.WorksheetFunction.CountA(rng) = 0 Then
Range("D" & cell.Row).Value = "E"
Else
Range("D" & cell.Row).Value = "F"
End If
Next cell

Return Column Header of Colored Cells

This process is being used for QC purposes. I have a spreadsheet that highlights certain cells that are wrong based off of their values and the validation rules we have in place. I was wonder if there was a way to return the column names of each cell that is colored into column A for each row? So for example if D2, F2, and G2 are wrong it would put all of those column headers in A2 to specify what exactly is wrong. I know it gets a bit more complicated trying to automate stuff with cell colors and I am not experienced in VBA which I'm assuming this will need. Is this possible to do, if so what would be the proper route to take? The data runs from column A to column BS, and the row numbers may differ, so if it could run up to row 1,000 that would be great. Attached is what the data looks like that I am working with.
The red means something is wrong in that row, and the orange cell is the color indicating that it is a wrong value
Yes, it is possible to do. Here is some snippets of code I pulled together to help get you started.
Lastrow = Cells(Rows.count, "A").End(xlUp).Row 'Get last row
With ActiveSheet
Lastcol = .Cells(1, .Columns.count).End(xlToLeft).Column 'Get last col
End With
For x = 1 To Lastcol 'Iterate Col
For i = 1 To Lastrow 'Iterate Row
'if red....
If Cells(i, x).Selection.Interior.Color = 255 then
'Move name to Cell A and append off of old name(s).
Cells(i, "A") = Cells(i, "A") & ", " & Cells(i, x)
End If
Next i 'next row
Next x 'next col

VBA - IF loop improvements

I'm currently running a macro which identifies duplicates in a workbook, however it identifies the first set off the index and doesn't tag the first set then which has led to me setting up a if statement to by pass this, which adds duplicate to the first instance too. This is taking a long time to do however and would like to improve this, if possible. Any suggestions would be greatly appreciated, I am new to VBA but have been learning bits as I've encountered new problems!
'Declaring the lastRow variable as Long to store the last row value in the Column1
Dim lastRow As Long
'matchFoundIndex is to store the match index values of the given value
Dim matchFoundIndex As Long
'iCntr is to loop through all the records in the column 1 using For loop
Dim iCntr As Long
Dim first_dup As Long
Dim tagging As Long
Dim item_code As String
'Finding the last row in the Column 1
lastRow = Range("B1000000").End(xlUp).Row
'
'looping through the column1
For iCntr = 2 To lastRow
'checking if the cell is having any item, skipping if it is blank.
If Cells(iCntr, 1) <> "" Then
'getting match index number for the value of the cell
matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range("A1:A" & lastRow), 0)
'if the match index is not equals to current row number, then it is a duplicate value
If iCntr <> matchFoundIndex Then
'Printing the label in the column B
Cells(iCntr, 4) = "Duplicate"
End If
End If
Next
For first_dup = 2 To lastRow
If Cells(first_dup, 5) = "Duplicate" Then
item_code = Cells(first_dup, 1)
For tagging = 2 To lastRow
If Cells(tagging, 1) = item_code Then
Cells(tagging, 5) = "Duplicate"
End If
Next
End If
Next
Example data:
item code
1
2
3
4
1 duplicate
2 duplicate
3 duplicate
4 duplicate
1 duplicate
2 duplicate
3 duplicate
4 duplicate
My first suggestion is not to over-complicate things, try using duplicate values conditional formatting to see if this helps:
Failing that, if you are desperate to find ONLY the duplicates, and not the first occurrence, you can use a formula like this: (In Cell B2 if your Data starts in A2, it will require a header row that doesn't match, or your first row will always match)
=IF(COUNTIF($A1:A$1,A2)>=1,"Duplicate","")
Which when pasted down your row of data could look something like this:
There are also VBA solutions if you are desperate for a VBA solution, but I thought I'd give you the simple ones first. Let me know how you get on in the comments.
Edit: you can just insert the above formula using VBA, with R1C1 notation, e.g.:
Sub test()
Range("B2:B" & Range("A1").End(xlDown).Row).FormulaR1C1 = "=IF(COUNTIF(R1C1:R[-1]C1,RC1)>=1,""Duplicate"","""")"
End Sub
I'll break this down so you know what is happening.
Range("B2:B" & Range("A1").End(xlDown).Row) selects the cells in column B between B2 and the last filled row in column A i.e. Range("A1").End(xlDown).Row (so this won't work if you expect blanks in column A as part of your data)
Then, it sets the R1C1 ref formula to "=IF(COUNTIF(R1C1:R[-1]C1,RC1)>=1,""Duplicate"","""")", where R1C1 means first row, first column, (i.e. $A$1)
R[-1]C1 means previous row, first column. For example,
If you are in B5, this would select A4.
If you are in A2, this would select A1.
If you are in A1, this would error out because you cant be in a row earlier than 1.
And RC1 means current row, first column.
Hope this helps!
The answer was the same as the initial code I presented, it's taking roughly 5 minutes for 30000 items so it isn't too bad at what it does.

macro to insert blank rows based on column value

I'm unsuccessfully trying to create a macro to insert blank row based on cell value.
I have a bulk data, in which one column has differing numbers. Based on the column value, I need to insert a blank row below it.
If I understand you properly this should do what you want.
Just change "A:A" to the range your working with and the If cell.Value = 1 Then to the condition you need to find the cell you want to add a blank row under.
Dim i As Range
Dim cell As Range
Set i = Range("A:A")
For Each cell In i.Cells
If cell.Value = 1 Then
cell.Offset(1).EntireRow.Insert
End If
Next
The below is an example for if you are looking to insert a blank row based on a sudden change of value in a column (in this case column "C"):
Dim lRow As Long
For lRow = Cells(Cells.Rows.Count, "C").End(xlUp).Row To 3 Step -1
If Cells(lRow, "C") <> Cells(lRow - 1, "C") Then Rows(lRow).EntireRow.Insert
Next lRow
You can change Cells(lRow - 1, "C") to whatever value you want to trigger your row insert and, of course, what column this is being applied to.

update the formula in the cell if column value is stored in a variable

I have these formula activesheet.cells(3,"C").Formula = "=F2"
but i want the formula to be like these activesheet.cells(3,"C").Formula = "=lastcolumn2"
So I found the last column of the 6th row
last_col = ActiveSheet.Cells(6, Columns.Count).End(xlToLeft).Column
last_col has 10 value in it.
I have inserted the formula like these
ActiveSheet.Cells(3,"C").Formula = "=" & last_col & "1"
but these inserts a value "=101" but not as the formula "=J1"(which is what i expected)
How do i make that. Any help is greatly appreciated.
You can also use .Address when you grab the last_col (slight spin on JMax's answer - sorry I just realized it's similar enough that I should have just added a comment under his). If you don't want the absolute path (like J1 instead of $J$1), you can use:
last_col = Cells(6, Columns.Count).End(xlToLeft).Address(False, False)
Cells(3, 3).Formula = "=" & last_col
Your solution will work provided you work with R1C1 notation instead of LC1.
Here is another way to do this:
ActiveSheet.Cells(3, 1).Formula = "=" & Cells(1, last_col).Address
I solved my own question, sorry but i just found the way to do it when i accidentally opened my pivot table report and tried these way and it works.
ActiveSheet.Cells(3, 1).Formula = "=R" & "1" & "C" & last_col
Hope these helps someone else but please let me know if there are any others way to do it.
Thanks
I do not understand your question or your answer. This does not really matter if you are happy but I suspect you got the effect you wanted by luck.
The original formula was activesheet.cells(3,"C").Formula = "=F2". This has the effect of setting cell C3 to the value of cell F2. If cell F2 is changed, cell C3 changes to match.
You want to set cell C3 to the value of the last used cell of row 2. You first used:
last_col = ActiveSheet.Cells(6, Columns.Count).End(xlToLeft).Column
This sets last_col to the number of last used column in row 6. You do not explain why you are looking for the last used cell of row 6 and not the last used cell of row 2 which is what you said you wanted.
last_col has a value of 10. The expression "=" & last_col & "1" has a value of =101 because Excel does not know you want 10 converted to J.
You say you are happy with:
ActiveSheet.Cells(3, 1).Formula = "=R" & "1" & "C" & last_col
But this sets cell A3 to the value of the cell with row = 1 and column = the last used column of row 6.
Nothing I can find in your question or your answer suggests this is what you want.
If you want cell A3 set to the value of the last used cell in row 2 as it is at the time the macro is run use:
Dim last_col As Integer
With ActiveSheet
' Find the column number of the last used cell in row 2
last_col = .Cells(2, Columns.Count).End(xlToLeft).Column
' Set cell C3 = Now value of last of row 2
.Cells(3, "C").Value = .Cells(2, last_col).Value
End With
If you want the value of cell C3 to change each time the value of the last used cell of row 2 changes use:
Dim LastUsedCellAddress As String
With ActiveSheet
' Find address of last used cell in row 2
LastUsedCellAddress = .Cells(2, Columns.Count).End(xlToLeft).Address
' Set cell C3 = Current value of last of row 2
.Cells(3, "C").Formula = "=" & LastUsedCellAddress
End With
I hope the above gives enough of an explanation for you to understand the effects of the different statements so you can get what you want today and in the future.