I am trying to interpolate a value in the table. However since my column cells are merged together, my code won't read the values. So far whenever i unmerge those columns, it works completely fine and gives me the value i want. How do i integrate merged cells as a matrices?
Sub brent()
Dim i As Integer, j As Integer
Dim P As Single, P1 As Single, P2 As Single
Dim M As Single, M1 As Single, M2 As Single
Dim inputmat()
nrow = 29
ncol = 2
P = Range("Axial").Value
ReDim inputmat(nrow, ncol)
For i = 1 To nrow
For j = 1 To ncol
inputmat(i, j) = Cells(5 + i, 6 + j)
Next j
Next i
If (P > inputmat(1, 1)) Or (P < inputmat(nrow, 1)) Then Range("PM").Value =
"NG"
Else
For i = 1 To nrow - 1
If (P <= inputmat(i, 1)) And (P >= inputmat(i + 1, 1)) Then
P1 = inputmat(i, 1)
P2 = inputmat(i + 1, 1)
M1 = inputmat(i, 2)
M2 = inputmat(i + 1, 2)
End If
Next i
For i = 1 To nrow
M = M1 + (P - P1) * (M2 - M1) / (P2 - P1)
Next i
Range("PM").Value = M
End If
End Sub
I know that there is problem under the "input (i,j)= cells(5+i,6+j)"
Is there any way to read that black column between the merged cells?
Sub FindMergedCells()
Dim tbl As Range, cll As Range
Dim i As Integer
i = 1
Set tbl = Range("A1:E4")
For Each cll In tbl
If cll.MergeCells Then
Cells(i, 7) = "Cell " & cll.Address & " is merged"
i = i + 1
End If
Next
End Sub
Related
I am supposed to make two market portfolios from the reversal strategy from the data given of value weighted market returns. However, I am stuck at how to proceed.
Sub REV1()
Dim c As Integer, r As Integer, g As Integer, x As Integer
Application.ScreenUpdating = False
lr = Sheets("VWMR").Cells(Rows.Count, 1).End(xlUp).Row
lc = Sheets("MRM").Cells(1, Columns.Count).End(xlToLeft).Column
Sheets.Add after:=Sheets(Sheets.Count)
Sheets(ActiveSheet.Name).Name = "REV1"
ReDim r1(lr - 2) As Variant
ReDim r2(lr - 2) As Variant
ReDim r3(lr - 2) As Variant
ReDim r4(lr - 2) As Variant
ReDim r5(lr - 2) As Variant
ReDim r6(lr - 2) As Variant
Columns("A:C").ColumnWidth = 20
For h = 1 To 2
B = 2
x = 2
For r = 2 To lr - 2 - h
Set n = Range(Sheets("VWMR").Cells(x, 2), Sheets("VWMR").Cells(x, lc))
Set m = Range(Sheets("VWMR").Cells(x + h, 2), Sheets("VWMR").Cells(x + h, lc)) _
cn = Application.WorksheetFunction.Count(n)
cm = Application.WorksheetFunction.Count(m)
If cn > 10 And cm > 10 Then
D2 = Application.WorksheetFunction.Percentile(n, 0.1)
D3 = Application.WorksheetFunction.Percentile(n, 0.9)
r2(r) = Application.WorksheetFunction.AverageIfs(m, n, "<=" & D2)
r3(r) = Application.WorksheetFunction.AverageIfs(m, n, ">=" & D3)
Sheets("REV1").Cells(B + h - 1, h + 1).Value = r2(r) - r3(r)
Sheets("REV1").Cells(B, 1).Value = Sheets("VVMR").Cells(B + 1, 1).Value
End If
B = B + 1
x = x + 1
Next
Sheets("REV1").Cells(1, h + 1).Value = "MOM" & h
Next
Sheets("REV1").Cells(1, 1).Value = "Dates"
Application.ScreenUpdating = True
Set a1 = Range(Sheets("REV1").Cells(2, 2), Sheets("REV1").Cells(lr, 2))
D = Application.WorksheetFunction.Average(a1)
MsgBox "The annual reversal returns are " & Format(Exp(D) - 1, "") & "."
End Sub
This is the code I tried to take out one portfolio first but this is not working.
In Excel 365, I have data in this format:
Or, in text:
1,2,3,7 A
4 B
5 C
6, 8 D
And I'm trying to split the data so it becomes this:
Or, in text
1 A
2 A
3 A
4 B
5 C
6 D
7 A
8 D
The leftmost row is always composed by numbers separated by comma or a single number. The right row can be any data.
The following VBA code will do most of what you want:
Sub ExpandRows()
Dim R As Range
Dim Rw As Range
Dim I As Integer
Dim J As Integer
Dim K As Integer
Dim S As String
Dim Tokens(1 To 1000) As String
Dim NTokens As Integer
Const Delim As String = ","
Dim StartSize As Integer
Dim TopCell As Range
Dim BotCell As Range
Set R = Selection
Set TopCell = R.Cells(1, 1)
Set BotCell = R.Cells(R.Rows.Count, 1)
StartSize = R.Rows.Count
For I = StartSize To 1 Step -1
S = R(I, 1)
If (S <> "") Then
J = 0
NTokens = 0
Do
K = InStr(J + 1, S, Delim)
If (K = 0) Then
NTokens = NTokens + 1
Tokens(NTokens) = Mid$(S, J + 1, Len(S) - J)
Else
NTokens = NTokens + 1
Tokens(NTokens) = Mid$(S, J + 1, (K - J - 1))
J = K
End If
Loop Until (K = 0)
End If
If (NTokens > 1) Then
For J = NTokens To 2 Step -1
If (Tokens(J) <> "") Then
Set Rw = R.Cells(I, 1).EntireRow
Call Rw.Select
Call Rw.Copy
Call R.Cells(I + 1, 1).EntireRow.Select
Call Rw.Insert(xlDown)
If (I = 1) Then
Set TopCell = TopCell.Cells(0, 1)
Set R = Range(TopCell, BotCell)
End If
Call R.Select
Call R.Cells(I + 1, 1).Select
R(I + 1, 1) = Tokens(J)
End If
Next J
R(I, 1) = Tokens(1)
End If
Next I
End Sub
This code will split the cells and create new rows with a single entry.
To use it, select the first column and execute the method.
After that, all you have to do is sort on the first column.
I have a MatMul function for multiplying 2 matrices and returning the resulting Array. Gomb2_Click is triggered via a button and for each trigger it applies the transformation described by the matrix to the initial point p0.
(Both the matrix and the initial p0 point are hardcoded) \
In the Gomb2_Click subroutine the p1 = MatMul(mat, p0) line gives me a ByRef argument type mismatch error and I don't know why.
Thanks!
Function MatMul(a As Range, b As Range) As Range
If a.Columns.Count <> b.Rows.Count Then
MatMul = 0
End If
ReDim res(a.Rows.Count, b.Columns.Count)
For i = 1 To a.Rows.Count
For j = 1 To b.Columns.Count
tmp = 0
For k = 1 To a.Columns.Count
tmp = tmp + a(i, k) * b(k, j)
Next k
res(i - 1, j - 1) = tmp
Next j
Next i
MatMul = res
End Function
Dim colnum As Integer
Set colnum = 0
Dim p0 As Range
Set p0 = Range("B6:B7")
Dim mat As Range
Set mat = Range("B2:C3")
Sub Gomb2_Click()
Dim p1 as Variant
Set p1 = MatMul(mat, p0)
Range(Cells(6, 3 + colnum), Cells(7, 3 + colnum)).Value = p1
colnum = colnum + 1
p0.Value = p1
End Sub
I was wondering it is possible to transpose a specific number of columns in a single column and display it in a row. For example, if there was a column that extended from A1 to A1000000, is it is possible to select the first 272 data points and then transpose it into a single row starting at A1 and then select the next 272 rows and display it on B1 etc. until it reaches the last row.
Thanks,
Select A1:A272. Press Copy (or Ctl+C).
Select B1. Press Paste in the top left corner of the ribbon's Home tab.
Select Paste Special and Transpose in the dialog box that opens.
Sub CopyToRange()
Dim vDB, vR()
Dim rngDB As Range
Dim Cnt As Long, i As Long, j As Integer
Dim n As Long
Set rngDB = Range("a1", Range("a" & Rows.Count).End(xlUp))
vDB = rngDB
Cnt = 272
For i = 1 To UBound(vDB, 1) Step Cnt
n = n + 1
ReDim Preserve vR(1 To Cnt, 1 To n)
For j = 1 To Cnt
If i + j - 1 > UBound(vDB, 1) Then GoTo p
vR(j, n) = vDB(i + j - 1, 1)
Next j
Next i
p:
Sheets.Add
Range("a1").Resize(n, Cnt) = WorksheetFunction.Transpose(vR)
End Sub
Sub Transp_mod2()
Dim P1 As Range, T2()
Set P1 = Sheets(3).UsedRange 'Adapt to your source column range
T1 = P1
Rws = P1.Count
Rmd = Rws
Spl = 272 'Adapt to your required steps
Cnt = 1
If Rws Mod Spl = 0 Then Rnds = Rws / Spl Else Rnds = Int(Rws / Spl) + 1
For i = 1 To Rnds
ReDim Preserve T2(1 To Spl, 1 To i)
If Rmd = Rws Mod Spl Then t = Rmd Else t = Spl
For j = 1 To t
T2(j, i) = T1(Cnt, 1)
Cnt = Cnt + 1
Rmd = Rmd - 1
Next j
Next i
Sheets(4).Range("A1").Resize(UBound(T2, 2), UBound(T2, 1)) = Application.Transpose(T2) 'Adapt "Sheets(4).range("A1")" to your destination range
End Sub
I want this code to search the column name called((attribute value1,attribute value2..N)
If that column contains fraction values, it should convert it to decimal. I'm using this macros(VBA).
The code is working but it is converting only one column(attribute value1).
It will take more time because I have multiple columns(attribute value2...N) that have fraction values.
Please help me out I am struck here.
Sub deci()
Dim LR As Long
Dim Dash As Long
Dim Whole As Double
Dim lngDataColumn As Long
Dim pi
lngDataColumn = 4
Sheets("Sheet3").Select
LR = Cells(Rows.Count, lngDataColumn).End(xlUp).row
For r = 2 To LR
s = Cells(r, lngDataColumn)
arr = Split(s, ",")
For i = LBound(arr) To UBound(arr)
Whole = 0
P = InStr(arr(i), " IN")
If P > 0 Then
Worksheet = (Left((arr(i)), P - 1))
Else
Worksheet = arr(i)
End If
Dash = InStr(Worksheet, "-")
If Dash > 0 Then
Whole = Frac(Left(Worksheet, Dash - 1))
Worksheet = Mid(Worksheet, Dash + 1)
End If
af = Right(arr(i), Len(arr(i)) - P + 1)
evfrac = Whole + Left(CStr(Evaluate(Worksheet)), 5)
' evfrac = Whole + Format(Evaluate(frac), "0.###")
ss = ss & evfrac & af & ", "
Next i
Cells(r, lngDataColumn) = Left(ss, Len(ss) - 2)
ss = ""
Next r
End Sub
Function Frac(ByVal X As String) As Double
Dim P As Integer, N As Double, Num As Double, Den As Double
X = Trim$(X)
P = InStr(X, "/")
If P = 0 Then
N = Val(X)
Else
Den = Val(Mid$(X, P + 1))
If Den = 0 Then Err.Raise 11 ' Divide by zero
X = Trim$(Left$(X, P - 1))
P = InStr(X, " ")
If P = 0 Then
Num = Val(X)
Else
Num = Val(Mid$(X, P + 1))
N = Val(Left$(X, P - 1))
End If
End If
If Den <> 0 Then
N = N + Num / Den
End If
Frac = N
End Function
The reason it's only doing one column is because that's exactly what your telling it to do with this section of the code:
lngDataColumn = 4
Sheets("Sheet3").Select
LR = Cells(Rows.Count, lngDataColumn).End(xlUp).row
Because your setting lngDataColumn as a fixed figure, your code is only executed on column 4. If you want to do more columns as a loop, you need to increment this value in the same maner you are incrementing r in your for loop.
For example:
lngDataColumn = 10
Sheets("Sheet3").Select
For 4 To lngDataColumn
LR = Cells(Rows.Count, lngDataColumn).End(xlUp).row
'Rest of code
Next lngDataColumn