I asked a similar question before and someone helped me with another problem I had with this macro but now I got stuck again (1 week now). I wanted to make the macro variable in terms of range (row and column).
For a better understanding:
Every week I'll the latest added column and insert it between Q:R (only in this example, it's variable). Here is the macro I use to insert the column:
Sub insertColumn()
Dim lastrow As Long
Dim LastCol As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'Copies the third last column and inserts it between the column [last date] and Overall'
With Sheets("getDATA")
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
lastrow = .Cells(.Rows.Count, "G").End(xlUp).Offset(7, 0).Row
.Columns(LastCol - 2).Copy
.Columns(LastCol - 1).Insert Shift:=xlToRight
End With
With Sheets("getDATA")
.Range("G7").End(xlToRight).Offset(0, -2).Value = Date
.Range(Cells(8, LastCol).Address(), Cells(lastrow, LastCol).Address()).Offset(0, -1).Formula = "=IFNA(INDEX($D:$D,MATCH($L8,$E:$E,0)),"""")"
End With
With Sheets("getDATA")
.Columns(LastCol - 2).PasteSpecial Paste:=xlPasteValues
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
After inserting a column, I'll check the DATA. If something new gets added, I'll delete everything that was in the weeks before.
If the DATA has the same value one, two, three or more weeks, the macro should only keep the first one. If the cell is blank, the macro should add "Other" in the column Overall.
The overall column should also display the value, we kept. You can see this in the example table - everything that grey should be deleted.
Here is the Macro that checks the criteria:
Public Sub CheckDATA()
Dim myRow As Range
Dim myCell As Range
Dim inputRange As Range
Dim previousCell As Range
Dim flagValue As Boolean
Dim lastCell As Range
Dim lastrow As Long
Dim LastCol As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With Sheets("getDATA")
lastrow = .Cells(.Rows.Count, "G").End(xlUp).Row
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
Set inputRange = Worksheets(1).Range(Cells(8, 13).Address(), Cells(lastrow, LastCol - 2).Address())
For Each myRow In inputRange.Rows
Set previousCell = Nothing
flagValue = False
For Each myCell In myRow.Cells
If Len(myCell) Then flagValue = True
If Not previousCell Is Nothing Then
If previousCell <> myCell Then
previousCell.clear
Set previousCell = myCell
Else
myCell.clear
End If
Else
Set previousCell = myCell
End If
Set lastCell = myCell
Next myCell
If Not flagValue Then
lastCell.Offset(0, 1) = "Other"
Else
lastCell.Offset(0, 1) = previousCell
End If
Next myRow
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
I think the main problem appears in the line If previousCell <> myCell Then previousCell.clear. The macro works perfectly till I'll add a third week. The main mistake is that it ONLY checks the cell next to it instead of the entire row-range. This will cause the macro to delete every value that was there before, no matter the value was 3 weeks in a row the same. Because of this, I can't tell when the value first got inserted - hopefully this does make sense. Every "Value" is a sales order block. I want to know in which week the block first appeared

F8. This will execute your code line by line. Now before you pressF8again, open theLocals Window(you can find it underView). This will show you all the variables and its current value. Continuously pressF8to find out what line of code doesn't do what you expect. This way, you will be able to find out what went wrong and why you're not getting the desired result. Once you've found your specific issue, post it again here if you are unable to solve it.Locals Windowand stepping through each line of code will improve your coding skills more than you'll ever know.If previousCell <> myCell Then previousCell.clear. The macro works perfectly till I'll add a third week. The main mistake is that it ONLY checks the cell next to it instead of the entire row-range. This will cause the macro to delete every value that was there before, no matter the value was 3 weeks in a row the same. Because of this, I can't tell when the value first got inserted - hopefully this does make sense. Every "Value" is a sales order block. I want to know in which week the block first appeared.