H
Harimau
Hi,
I was trying to run some VBA code when the error message above came up...
Try as I might, i could not find what was wrong with it. Could someone please
help me?
There are 154 companies in Sheet2, which are then divided into 6 groups.
Each of these groups had a seperate sheet for their raw data. I had named the
six raw data ranges "Data1", "Data2", etc. I also have one seperate Dividend
sheet, which has the raw data of the dividends in it. This is what I had
wanted the code to do:
For each of the companies, go to the dividend page. For each dividend, find
out what the market price was on the payment date of the dividend. Then find
out what 1+dividend/price is, and then paste it into a seperate sheet,
"Sheet4".
For some reason, I just don't know why it doesn't work. My other macros on
the worksheet works fine, so it doesn't seem to be anything wrong with my
excel, so its probably my code. Could someone please help?
Code is below:
Option Explicit
Sub dividend()
Dim i As Integer
Dim j As Integer
Dim asx As String
Dim code As Integer
Dim position As Integer
Dim number As Integer
Dim reference As String
Dim paydate As Integer
Dim closingrow As Integer
Dim closingcolumn As Integer
Dim closingprice As Double
Dim dividend As Double
Dim divunit As Double
Dim divcolumn As Integer
For i = 1 To 154
asx = Sheet2.Cells(4 + i, 1).Value
code = Sheet2.Cells(4 + i, 2).Value
position = Sheet2.Cells(4 + i, 10).Value
number = Sheet2.Cells(4 + i, 11).Value
reference = "Data" & code
closingcolumn = Application.WorksheetFunction.Match(asx,
Application.WorksheetFunction.Index(Application.WorksheetFunction.indirect(reference), 1, 0), 0)
If i <= 76 Then
divcolumn = Application.WorksheetFunction.Match(asx,
Sheet4.Range("A1:EU1"))
Else
divcolumn = Application.WorksheetFunction.Match(asx,
Sheet4.Range("A20:EY20"))
End If
If number = 0 Then
Else
For j = 1 To number
dividend = Sheet3.Cells(position + 1 + j, 3).Value / 100
paydate = Sheet3.Cells(position + 1 + j, 7).Value
closingrow = Application.WorksheetFunction.Match(paydate,
Application.WorksheetFunction.Index(Application.WorksheetFunction.indirect(reference), 0, closingcolumn), 0)
closingprice =
Application.WorksheetFunction.Index(Application.WorksheetFunction.indirect(reference), closingrow, closingcolumn)
divunit = 1 + dividend / closingprice
If i <= 76 Then
Sheet4.Cells(1 + j, divcolumn).Value = paydate
Sheet4.Cells(1 + j, divcolumn + 1).Value = divunit
Else
Sheet4.Cells(20 + j, divcolumn).Value = paydate
Sheet4.Cells(20 + j, divcolumn + 1).Value = divunit
End If
Next j
End If
Next i
'
End Sub
I was trying to run some VBA code when the error message above came up...
Try as I might, i could not find what was wrong with it. Could someone please
help me?
There are 154 companies in Sheet2, which are then divided into 6 groups.
Each of these groups had a seperate sheet for their raw data. I had named the
six raw data ranges "Data1", "Data2", etc. I also have one seperate Dividend
sheet, which has the raw data of the dividends in it. This is what I had
wanted the code to do:
For each of the companies, go to the dividend page. For each dividend, find
out what the market price was on the payment date of the dividend. Then find
out what 1+dividend/price is, and then paste it into a seperate sheet,
"Sheet4".
For some reason, I just don't know why it doesn't work. My other macros on
the worksheet works fine, so it doesn't seem to be anything wrong with my
excel, so its probably my code. Could someone please help?
Code is below:
Option Explicit
Sub dividend()
Dim i As Integer
Dim j As Integer
Dim asx As String
Dim code As Integer
Dim position As Integer
Dim number As Integer
Dim reference As String
Dim paydate As Integer
Dim closingrow As Integer
Dim closingcolumn As Integer
Dim closingprice As Double
Dim dividend As Double
Dim divunit As Double
Dim divcolumn As Integer
For i = 1 To 154
asx = Sheet2.Cells(4 + i, 1).Value
code = Sheet2.Cells(4 + i, 2).Value
position = Sheet2.Cells(4 + i, 10).Value
number = Sheet2.Cells(4 + i, 11).Value
reference = "Data" & code
closingcolumn = Application.WorksheetFunction.Match(asx,
Application.WorksheetFunction.Index(Application.WorksheetFunction.indirect(reference), 1, 0), 0)
If i <= 76 Then
divcolumn = Application.WorksheetFunction.Match(asx,
Sheet4.Range("A1:EU1"))
Else
divcolumn = Application.WorksheetFunction.Match(asx,
Sheet4.Range("A20:EY20"))
End If
If number = 0 Then
Else
For j = 1 To number
dividend = Sheet3.Cells(position + 1 + j, 3).Value / 100
paydate = Sheet3.Cells(position + 1 + j, 7).Value
closingrow = Application.WorksheetFunction.Match(paydate,
Application.WorksheetFunction.Index(Application.WorksheetFunction.indirect(reference), 0, closingcolumn), 0)
closingprice =
Application.WorksheetFunction.Index(Application.WorksheetFunction.indirect(reference), closingrow, closingcolumn)
divunit = 1 + dividend / closingprice
If i <= 76 Then
Sheet4.Cells(1 + j, divcolumn).Value = paydate
Sheet4.Cells(1 + j, divcolumn + 1).Value = divunit
Else
Sheet4.Cells(20 + j, divcolumn).Value = paydate
Sheet4.Cells(20 + j, divcolumn + 1).Value = divunit
End If
Next j
End If
Next i
'
End Sub