Is there a way to combine range, cells, value and offset? - excel

I have done a loop and I was wondering if I do a cell reference by combining it with offset such as
If (range1.Cells(i, j).Offset(1,0).Value <> range2.Cells(i, j).Value) Then
But I run into an error. Is there anyway of going about it without defining other integers?

The line of code can work..............you need to see what is going on........use OnError to get to:
k = vbCrLf
MsgBox i & k & j & k & range1.Cells(i, j).Offset(1, 0).Address & k & range1.Cells(i, j).Offset(1, 0).Value & k & range2.Cells(i, j).Address & k & range2.Cells(i, j).Value
to see why the code is failing.

Related

Excel VBA Using Sum in a For Next Loop

I have a for next loop where I have dimmed the row number as "i". I would like to sum up the following range - within row "i", from column C to M in a For Next Loop. I have the following codes:
Range("N" & i).Value = Application.WorksheetFunction.Sum(Range("C" & i & ":" & "M" & i))
I have also tried the following:
Range("N" & i).Value = Application.WorksheetFunction.Sum(Range("C" & i), Range("M" & i))
Range("N" & i).Value = Application.WorksheetFunction.Sum("C" & i & ":" & "M" & i)
I guess my main problem is I cannot figure out the correct way to reference the dynamic row number i in the code, hence the error. Would appreciate your help.

Types Mismatch error on range value, can't figure out why

So Every time I run my code I get the Types Mismatch error and I can't find what to change into what in order to run it properly. Even though this might still not entirely Solve the greater problem which is how to formulate my question into the vba code, but first things first. How to get this line of code without errors:
Range("I" & r & ":" & "I" & B).Value = Range("I" & r & ":" & "I" & B).Value + 1
Where r = the current row the code is checking ( 5 to 44)
and B is the last row the code can check ( 44)
All I want this line to do is to add one to the already existing value of the cell (which is 0 if nothing is done in that row or a formula if conditions are met, this formula will make a value from 1 to 40)
You cannot do this the way you are doing -> you cannot add a number to a range of cells at once. You must add the number to one cell at a time.
Try this:
Dim i As Integer
For i = r To B
Range("I" & i).Value = Range("I" & i).Value + 1
Next
To handle formulas:
Dim i As Integer
For i = r To B
Dim numberToAdd As Integer
numberToAdd = 1
If Range("I" & i).HasFormula Then
Range("I" & i).Value = Range("I" & i).Formula & "+" & Trim(Str(numberToAdd))
Else
Range("I" & i).Value = Range("I" & i).Value + 1
End If
Next

Execute String Formulas from Array

I have two columns which have to be filled with formulas (with 5k to 10k values). Those are simple formulas and they work.
But writing directly into the cells slows the program down too much, so I'm writing the formulas into an array and then pasting the array into the workbook. This works fine too, it just doesn't evaluate the strings. I tried using EVALUATE without success.
If I select any of the copied cells, press F2 and Enter, they work perfectly.
Code sample:
ReDim Schreibblock(FirstRow To LastRowPos, 0 To 1)
For j = FirstRow To LastRowPos
Schreibblock(j, 0) = "=" & SteigungPos.Address & "*$B$" & j & "+" & NullwertPosTren.Address & ""
Schreibblock(j, 1) = "=ABS($A$" & j & "-$C$" & j & ")"
Next j
Range("C" & FirstRow & ":D" & LastRowPos).Formula = [Schreibblock]
and here a "light version" for easier reading:
ReDim array(14 To 5000, 0 To 1)
For j = 14 To 5000
Array(j, 0) = "=$B$10*$B$14+$B$9" <- This is what I see, but w/o ""
Array(j, 1) = "=ABS($A$14-$C$14)" <- This is what I see, but w/o ""
Next j
Range("C14:D5000").Formula = [Array]
There are no errors and it's lightening fast compared to wirting every loop into a cell. I would appreciate any help.
Carlos
Use R1C1 notation and skip the array and loop:
Range("C" & FirstRow & ":C" & LastRowPos).FormulaR1C1 = "=" & SteigungPos.Address(0,0,xlR1C1) & "*RC2 +" & NullwertPosTren.Address(0,0,xlR1C1) & ""
Range("D" & FirstRow & ":D" & LastRowPos).FormulaR1C1 = "=ABS(RC1 - RC3)"

How do I concatenate rows between cells of specified value?

I am trying to use VBA to concatenate everything between two specified rows. What's the best way to go about this?
Basically I want to leave the lines where the third cell is "U" intact, and make the sixth cell of that row the concatenation of the rows below, until we run into another row that contains a "U" in the third cell. Then the process would repeat. The number of rows between the cells containing "U" is varied.
Pic is below
Ok, this should work (haven't tested it though):
Sub My_Amazing_Skills()
Dim l As Long, i As Long
l = 1
i = 1
Do Until i > Range("A1048576").End(xlUp).Row
If Range("C" & l).Value = "U" Then
i = i + 1
Do Until Range("C" & i).Value = "U"
Range("F" & l).Value = Range("F" & l).Value & " " & Range("C" & i).Value
i = i + 1
Loop
Range("F" & l).Value = Trim(Range("F" & l).Value)
End If
l = i
Loop
MsgBox "Bow down to the great Jeremy!", vbInformation, "Your concatenating is done"
End Sub
I presume you know know where to copy this to?

Find both duplicates in the same ROW VBA

Somehow after few days of googleing I didn't find any satisfying answer.
I have to find duplicates in the same column and copy them both (or more) to the new sheet to show where are the issues.
The only way I managed to do that was
For i = 2 To lastCell
If dataArray(i, 3) <> "" Then
For j = i + 1 To lastCell
If dataArray(i, 3) = dataArray(j, 3) Then
results.Range("A" & k & ":" & lastCol & k).Value = checkbook.Range("A" & i & ":" & lastCol & i).Value '
results.Range(commentAddress & k).Value = "Duplicate ID"
k = k + 1
results.Range("A" & k & ":" & lastCol & k).Value = checkbook.Range("A" & j & ":" & lastCol & j).Value
results.Range(commentAddress & k).Value = "Duplicate ID"
k = k + 1
End If
Next j
End If
Next I
But this is taking too long! I found that dictionary could be very helpful but don't really know how to use this - and it only shows the SECOND value (I need both)
So are there any other solutions to find duplicates? I need the fastest one as the file I am working on has 100K+ rows (loop in a loop is killing me)
You could try something similar to this to recreate the column with blanks replacing the duplicate values. Then you can just filter on blank values in the column to find which values or IDs are duplicates. Or write a formula that loops through column and collects addresses of cells that are empty.
=IF(A1="";"";IF(COUNTIF(A1:A100;A1)=1;A1;""))
Replace A1:A100 with the range your data occupy, or the whole column if you prefere.

Resources