Reputation: 49
I have lots of data (numbers) with heading in several worksheets that I am trying to zero. This is done on each column as follows: Taking the value of the first row in the column and subtracting this value from all rows in the column.
I have put together this code (may not be the best way, but Im new to VBA so :))
Dim ws As Worksheet
Dim Header As Range, ColData As Range
Dim firstrow As Long
Dim cell As Range, cell2 As Range
Set ws = ActiveSheet
Set Header = ws.Range("B5").End(xlToRight)
For Each cell In Header
If IsEmpty(cell) Then
Else
firstrow = cell.Offset(2).Value
If Not IsEmpty(cell.Offset(2)) Then
Set ColData = ws.Range(cell.Offset(2), cell.Offset(2).End(xlDown))
For Each cell2 In ColData
cell2.Value = cell2.Value - firstrow
Next cell2
Next cell
I am getting the error of Next without For? Am I missing something? forgive me if its something stupid I have checked several times and cant seem to spot it!
Thanks!
EDIT: Fixed IsEmpty function and it ran.. not giving the correct output yet but I'll work on it!
Upvotes: 1
Views: 64
Reputation: 35990
As mentioned in a comment to your question, you are missing two End If
statements. Here is a simple way how you can avoid this kind of problem when typing out your code:
Every time you start an IF _condition_ Then
statement, immediately hit Enter twice and type the End If
line. Then go up one line and type what goes inside the IF statement.
The same technique can be applied to all languages and statements that have an opening and a closing marker (For/Next, etc.), be it a keyword, a pair of brackets {} [] ()
, or comment marks /* [...] */
.
Upvotes: 1