Figuring out proper looping

jrwrangler

New Member
Joined
Mar 22, 2013
Messages
16
Hello

I am trying to go down row f and if there is a value in that cell, I want it to divide row f by row c and put it in row g. I dont know much about vba really so if you could walk me through each line i would really appreciate it. thanks a lot

Dim Mycell
r1 As Range
Set r1 = ActiveSheet.Range("f2:f1000")
For Each Mycell In r1
If Mycell <> "" Then
Selection.Value = Cell.Offset(-1, 0) / Cell.Offset(-4, 0)
End If
Next Cell
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Welcome to the Board.

Something like this?

Code:
Sub test()
Dim Last_Row As Long
Dim i As Long
Application.ScreenUpdating = False
With ActiveSheet
    Last_Row = .Range("F" & Rows.Count).End(xlUp).Row
    
    For i = 2 To Last_Row
        If Len(.Cells(i, 6)) > 0 Then
            With .Cells(i, 6)
                .Value = .Offset(0,-1) / .Offset(0,-4)
            End With
        End If
    Next i
End With
Application.ScreenUpdating = True
End Sub

EDIT: Just realised that you mention rows f, c and g in your post. I've amended the offset function as I assume you mean columns f,c and g?
 
Last edited:
Upvote 0
I did, thank you. I am getting a mismatch error on this line:

.Value = .Offset(0,-1) / .Offset(0,-4)</pre>
 
Upvote 0
I did, thank you. I am getting a mismatch error on this line:

.Value = .Offset(0,-1) / .Offset(0,-4)
</PRE>

To clarify, if column F contains a value, what do you want to happen?
Your original post suggests you want to calculate F/C and write this to column G. Is this correct?
 
Upvote 0
I am trying to go down row f and if there is a value in that cell, I want it to divide row f by row c and put it in row g. I dont know much about vba really so if you could walk me through each line i would really appreciate it. thanks a lot

Dim Mycell
r1 As Range
Set r1 = ActiveSheet.Range("f2:f1000")
For Each Mycell In r1
If Mycell <> "" Then
Selection.Value = Cell.Offset(-1, 0) / Cell.Offset(-4, 0)
End If
Next Cell
You can do this without using any looping...
Code:
Sub DivideFbyC()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "F").End(xlUp).Row
  Range("G2:G" & LastRow) = Evaluate("IF(LEN(F2:F" & LastRow & "),F2:F" & _
                            LastRow & "/C2:C" & LastRow & ","""")")
End Sub
 
Upvote 0
Rick, thanks a lot.
Mack, I was tryin to get row f divided by row c and insert calculation in row g. thanks for your help
 
Upvote 0

Forum statistics

Threads
1,203,606
Messages
6,056,279
Members
444,854
Latest member
goethe168

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top