run macro after open workbook

Forestq

Active Member
Joined
May 9, 2010
Messages
482
hi,

In module called "total_measures" I have simply code:
Code:
Sub count_total_measures()Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("row_data")


Dim x_row_data, i, count_measure As Long
x_row_data = ws.Cells(Rows.Count, "A").End(xlUp).Row
count_measure = 0

    For i = 2 To x_row_data
            If Range("D" & i).Value <> "" Then
                count_measure = count_measure + 1
            End If
            If Range("E" & i).Value <> "" Then
                count_measure = count_measure + 1
            End If
            If Range("F" & i).Value <> "" Then
                count_measure = count_measure + 1
            End If
            If Range("G" & i).Value <> "" Then
                count_measure = count_measure + 1
            End If
            If Range("H" & i).Value <> "" Then
                count_measure = count_measure + 1
            End If
            ws.Range("C" & i).Value = count_measure
            count_measure = 0
    Next i
End Sub

The code is working OK.

Now I want to run this code, after I open workbook. So, in "ThisWorkbook" I add code as below:
Code:
Private Sub Workbook_Open()
Call total_measures.count_total_measures
End Sub

But something is wrong, because I`m getting wrong numbers.
Please assist.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I found mistake:
Code:
If ws.Range("D" & i).Value <> "" Then               
count_measure = count_measure + 1
End If
after I added "ws" before Range it's working ok.
 
Upvote 0

Forum statistics

Threads
1,215,744
Messages
6,126,629
Members
449,323
Latest member
Smarti1

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