I have the following list, I want to sort the dates and display the recent ones and stock them in ranges 21 and 22 like this:
I have successfully written this code that helped to do the sorting and display the recent dates and stock them on range 21, now I am stack and I dont know how to do to display the end date associated to each recent date.
Loop Until Not ech
' conversion of numbers into text for dico and listbox)
' and the false dates (written in text) to real dates
On Error GoTo PasDate
For i = 2 To UBound(t)
t(i, 1) = CStr(t(i, 1))
If TypeName(t(i, 2)) = "String" Then t(i, 2) = 1 * DateSerial(Right(t(i, 2), 4), Mid(t(i, 2), 4, 2), Left(t(i, 2), 2))
Next i
On Error Resume Next
'Fill dico
Set dico = CreateObject("scripting.dictionary")
dico.CompareMode = TextCompare
For i = 2 To UBound(t)
If t(i, 1) <> "" Then
If Not dico.Exists(t(i, 1)) Then
dico.Add t(i, 1), t(i, 2)
Else
If t(i, 2) > dico(t(i, 1)) Then dico(t(i, 1)) = t(i, 2)
End If
End If
Next i
'Transfert dico to the table r for the list
ReDim r(1 To dico.Count, 1 To 2): i = 0
For Each x In dico.Keys: i = i + 1: r(i, 1) = x: r(i, 2) = dico(x): Next
'fill ranges 20 and 21
.Range("b20:b21").Resize(, Columns.Count - 1).Clear
.Range("b20").Resize(1, UBound(r)).HorizontalAlignment = xlCenter
.Range("b21").Resize(1, UBound(r)).NumberFormat = "dd/mm/yyyy"
.Range("b20").Resize(2, UBound(r)).Borders.LineStyle = xlContinuous
.Range("b20:b21").Resize(2, UBound(r)) = Application.Transpose(r)
End With
'poplate the listbox
For i = 1 To UBound(r): r(i, 1) = Format(r(i, 2), "dd/mm/yyyy"): Next
'For i = 1 To UBound(r): r(i, 1) = Format(r(i, 1), "000"): r(i, 2) = Format(r(i, 2), "dd/mm/yyyy"): Next
With ListBox1
.ColumnCount = 2
.ColumnHeads = False
.ColumnWidths = .Width * 0.7 '& ";" & .Width * (1 - 0.6 + 0.1)
.List = r
End With
Exit Sub
'
PasDate:
Exit Sub
End
End Sub
Related
Sub FormatAndIncompleteOrders()
Dim a, Q&, i&, b(1 To 2), R, j%
Application.ScreenUpdating = False
Rem -----------------------------------\
a = Range("'Original Data'!A3").CurrentRegion: Q = UBound(a)
ReDim R(1 To Q, 1 To 4): b(1) = R: b(2) = R
ReDim R(1 To 2) As Long
Rem -----------------------------------\
For i = 2 To Q
Select Case True
Case a(i, 2) <> "" And a(i, 3) <> "" And a(i, 4) <> ""
R(1) = 1 + R(1): b(1) = fillArray(b(1), R(1), a, i)
Case a(i, 2) <> ""
R(2) = 1 + R(2): b(2) = fillArray(b(2), R(2), a, i)
End Select
Next
Rem -----------------------------------\
With Sheets("New Orders")
.Select
.Range("A3").CurrentRegion.Offset(1).Delete xlShiftUp
.Range("A4").Resize(R(1), 4) = b(1)
End With
Rem -----------------------------------\
With Sheets("Incomplete Orders")
.Range("A1").CurrentRegion.Offset(1).Delete xlShiftUp
.Range("A2").Resize(R(2), 4) = b(2)
End With
End Sub
*I am trying to use the code below to format and clean data it keeps giving me an error message "Sub or function not defined"
I have a problem with displaying multiple columns in a ListBox in my UserForm.
Everything is working until my numbers of column is max 10.
My code:
Private Sub FindButton_Click()
ListBoxResult.Clear
ListBoxResult.ColumnCount = 14
Dim RowNum As Long
RowNum = 1
Do Until Sheets("db").Cells(RowNum, 1).Value = ""
If InStr(1, Sheets("db").Cells(RowNum, 2).Value, FindDMC.Value, vbTextCompare) > 0 Then
On Error GoTo next1
ListBoxResult.AddItem Sheets("db").Cells(RowNum, 1).Value
ListBoxResult.List(ListBoxResult.ListCount - 1, 2) = Sheets("db").Cells(RowNum, 2).Value
ListBoxResult.List(ListBoxResult.ListCount - 1, 3) = Sheets("db").Cells(RowNum, 3).Value
ListBoxResult.List(ListBoxResult.ListCount - 1, 4) = Sheets("db").Cells(RowNum, 4).Value
ListBoxResult.List(ListBoxResult.ListCount - 1, 5) = Sheets("db").Cells(RowNum, 5).Value
ListBoxResult.List(ListBoxResult.ListCount - 1, 6) = Sheets("db").Cells(RowNum, 6).Value
ListBoxResult.List(ListBoxResult.ListCount - 1, 7) = Sheets("db").Cells(RowNum, 7).Value
ListBoxResult.List(ListBoxResult.ListCount - 1, 8) = Sheets("db").Cells(RowNum, 8).Value
ListBoxResult.List(ListBoxResult.ListCount - 1, 9) = Sheets("db").Cells(RowNum, 9).Value
ListBoxResult.List(ListBoxResult.ListCount - 1, 10) = Sheets("db").Cells(RowNum, 10).Value
ListBoxResult.List(ListBoxResult.ListCount - 1, 11) = Sheets("db").Cells(RowNum, 11).Value
ListBoxResult.List(ListBoxResult.ListCount - 1, 12) = Sheets("db").Cells(RowNum, 12).Value
ListBoxResult.List(ListBoxResult.ListCount - 1, 13) = Sheets("db").Cells(RowNum, 13).Value
ListBoxResult.List(ListBoxResult.ListCount - 1, 14) = Sheets("db").Cells(RowNum, 14).Value
ListBoxResult.List(ListBoxResult.ListCount - 1, 15) = Sheets("db").Cells(RowNum, 15).Value
End If
next1:
RowNum = RowNum + 1
Loop
End Sub
ListBoxResult.ColumnCount and properties is 14, also Column widths is ok.
After runing my code the failure code is Run-time error '380': Could not set the List property. Invalid property value. At first, I was thinking that maybe ListBoxes have limits for columns, but I found ListBoxes with 60 columns on the Internet.
I am trying also this, but still doesn't work:
Private Sub Browser_RMA_Initialize()
ListBoxResult.RowSource = "db!a1:z1"
ListBoxResult.ColumnCount = 14
ListBoxResult.ColumnWidths = "50;50;50;50;50;50;50;50;50;50;50;50;50;50;"
ListBoxResult.ColumnHeads = True
End Sub
Could you support me, please?
Assigning to .Columnproperty avoids transposing
As late addition to #Dy.Lee 's valid and already accepted array approach (see my comment), I demonstrate a way how to avoid both repeated redimming [4] and transposing [5]:
Option Explicit ' declaration head of UserForm code module
Private Sub FindButton_Click()
'[0] where to search
Const SearchCol As Long = 2 ' get search items from 2nd column
'[1] define data set
Dim data As Variant
data = Tabelle1.Range("A1").CurrentRegion ' << change to your project's sheet Code(Name)
Dim ii As Long: ii = UBound(data, 1) ' row count
Dim jj As Long: jj = UBound(data, 2) ' column count
'[2] provide for sufficient result rows (array with converted row : columns order)
Dim results() As Variant
ReDim Preserve results(1 To jj, 1 To ii) ' redim up to maximum row count ii
'[3] assign filtered data
Dim i As Long, j As Integer, n As Long
For i = 1 To ii
If InStr(1, data(i, SearchCol), FindDMC.Value, vbTextCompare) > 0 Then
'' If data(i, SearchCol) = FindDMC.Value Then ' exact findings
n = n + 1
For j = 1 To jj
results(j, n) = data(i, j)
Next
End If
Next i
'[4] fill listbox with results
With ListBoxResult
.Clear
.ColumnCount = 14
.ColumnWidths = "50;50;50;50;50;50;50;50;50;50;50;50;50;50;"
If n Then
'[4] redimension only a 2nd time (& last time)
ReDim Preserve results(1 To jj, 1 To n)
'[5] assign results to listbox'es .Column property
.Column = results ' << .Column property avoids unnecessary transposing
End If
End With
End Sub
The column index of the listbox also starts at 0. The index number of additem should be 0, and you specified 15 at the end, then the number of columns becomes 16, so an error occurs because column 14 is exceeded.
It would be convenient to use an array.
Private Sub FindButton_Click()
Dim Ws As Worksheet
Dim vDB As Variant, vResult()
Dim i As Long, j As Integer, n As Long
Set Ws = Sheets("db")
vDB = Ws.Range("a1").CurrentRegion
For i = 1 To UBound(vDB, 1)
If InStr(1, vDB(i, 2), FindDMC.Value, vbTextCompare) > 0 Then
n = n + 1
ReDim Preserve vResult(1 To 14, 1 To n)
For j = 1 To 14
vResult(j, n) = vDB(i, j)
Next
End If
Next i
With ListBoxResult
.Clear
.ColumnCount = 14
.ColumnWidths = "50;50;50;50;50;50;50;50;50;50;50;50;50;50;"
If n Then
If n = 1 Then
.Column = vResult
Else
.List = WorksheetFunction.Transpose(vResult)
End If
End If
End With
End Sub
I am having an issue with the "Subscript out of Range" error message. I got some help writing a code that loops a long list of stocks. The code basically makes all of the vectors even so i can use it in a panel data setting.
The loop stops after 4 stocks and gives me a "Subscript out of Range" error.
I can run the code over the first 95 "i" i.e. if i transform the first part:
For i = 4 To 95
If Cells(i, 1) <> Cells(i - 1, 1) Then Clean_Stock_2 (i)
Code:
**Sub Outer_Loop()
For i = 4 To Cells(Rows.Count, 1).End(xlUp).Row**
If Cells(i, 1) <> Cells(i - 1, 1) Then Clean_Stock_2 (i)
Next i
End Sub
Sub Clean_Stock_2(ByVal r As Long)
Dim Stock(31, 5)
Dim Quarter(31)
Dim Bo As Boolean
Charge = 0
'Frame
For i = 0 To 31
Stock(i, 0) = Cells(r, 1)
Stock(i, 1) = Cells(r, 2)
Stock(i, 2) = Cells(r, 3)
Stock(i, 5) = "Q" & Format(DateAdd("q", i, #1/1/2011#), "q-YYYY")
Quarter(i) = Stock(i, 5)
Next i
'Data
Do While Cells(r, 1) = Stock(0, 0)
Qu = "Q" & Format(Cells(r, 4), "q-YYYY")
rr = Application.Match(Qu, Quarter, 0)
If Not IsError(rr) Then
Stock(rr, 3) = Cells(r, 4)
Stock(rr, 4) = Cells(r, 5)
If Not Bo Then Charge = Stock(rr, 4): Bo = True
End If
r = r + 1
Loop
'fill
For i = 0 To 31
If Stock(i, 4) = 0 Then
Stock(i, 4) = Charge
Else
Charge = Stock(i, 4)
End If
Next i
'Output
lr = Cells(Rows.Count, "I").End(xlUp).Row + 1
lr = IIf(lr < 3, 3, lr)
Cells(lr, "I").Resize(32, 6) = Stock
End Sub
This question already has an answer here:
Why does Range work, but not Cells?
(1 answer)
Closed 5 years ago.
In this sub, I'm simply doing a few calculations. In a differnt sheet I've some temporary data stored and I'm trying and failing to define the following as a range (at the very bottom at the code):
MeanVector = Worksheets("TempSheet").Range(Cells(2, 2), Cells(Stocks + 1, 2))
I get the following error: 1004 application-defined or object-defined error
The thing though is, this is a line of code that I copied in from a different module, so I know the method itself works.
I assume the problem is with the activation of the sheets, I'm tried to activate both at the beginning but with no luck. I do specify the worksheet in evry line of code, so I'm not sure why it would still be a problem?
I've tried to select the range as well:
Set MeanVector = Worksheets("TempSheet").Range(Cells(2, 2), Cells(Stocks + 1, 2))
All code:
Sub CalculateOwnPortfolio()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.StatusBar = "Calculating..."
Dim MeanVector As Range
Dim WeightsVector As Range
Worksheets("TempSheet").Activate
Worksheets("Own Portfolio").Activate
'Count the amount of stock
Stocks = 0
For i = 1 To 20
If Worksheets("MainSheet").Cells(i + 2, 2) <> 0 Then
Stocks = Stocks + 1
Else
Stocks = Stocks
End If
Next
'Amount of each stock
EmptyAmount = 0
For i = 1 To Stocks
If Worksheets("Own Portfolio").Cells(1 + i, 7) = Value Then
Else
EmptyAmount = EmptyAmount + 1
End If
Next
If EmptyAmount = 0 Then
MsgBox ("Error: Enter stock amounts")
Exit Sub
End If
'Calcualte amount of observations
Observations = 0
For j = 2 To 15000
If Worksheets("Own Portfolio").Cells(j, 1) <> 0 Then
Observations = Observations + 1
Else
Observations = Observations
End If
Next
Worksheets("Own Portfolio").Range(Cells(2, 2), Cells(Observations, 2)) _
.ClearContents
Worksheets("Own Portfolio").Range(Cells(2, 5), Cells(3 + Stocks, 5)) _
.ClearContents
'Total Value
For i = 2 To Observations + 1
Value = 0
For j = 1 To Stocks
Symbol = Worksheets("Own Portfolio").Cells(1 + j, 4)
Amount = Worksheets("Own Portfolio").Cells(1 + j, 7)
AdjClose = Worksheets(Symbol).Cells(i, 7)
Value = Value + (Amount * AdjClose)
Worksheets("Own Portfolio").Cells(i, 2) = Value
Next
Next
'Weights
TotalValue = 0
For j = 1 To Stocks
Symbol = Worksheets("Own Portfolio").Cells(1 + j, 4)
Amount = Worksheets("Own Portfolio").Cells(1 + j, 7)
AdjClose = Worksheets(Symbol).Cells(2, 7)
TotalValue = TotalValue + (Amount * AdjClose)
Next
For j = 1 To Stocks
StockValue = 0
Symbol = Worksheets("Own Portfolio").Cells(1 + j, 4)
Amount = Worksheets("Own Portfolio").Cells(1 + j, 7)
AdjClose = Worksheets(Symbol).Cells(2, 7)
StockValue = Amount * AdjClose
Worksheets("Own Portfolio").Cells(1 + j, 5) = StockValue / TotalValue
Next
'Mean,Variance,Std Dev and Sharp Ratio
'-------------------------------------------------------------------------
'----------------This is where I get the error message--------------------
MeanVector = Worksheets("TempSheet").Range(Cells(2, 2), Cells(Stocks + 1, 2))
'-----------------------------------------------------------------------
WeightsVector = Worksheets("Own Portfolio").Range(Cells(2, 5), Cells(Stocks + 1, 2))
Mean = Application.WorksheetFunction.SumProduct(MeanVector, WeightsVector)
Call OwnPortfolioGraph(Symbol)
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.StatusBar = False
End Sub
Picture of "TempSheet"
change this:
MeanVector = Worksheets("TempSheet").Range(Cells(2, 2), Cells(Stocks + 1, 2))
to this:
set MeanVector = Worksheets("TempSheet").Range(Cells(2, 2), Cells(Stocks + 1, 2))
and see if it works
then do the same for
WeightsVector = Worksheets("Own Portfolio").Range(Cells(2, 5), Cells(Stocks + 1, 2))
Edit:
I'm pretty sure that in your formula
Worksheets("TempSheet").Range(Cells(2, 2), Cells(Stocks + 1, 2))
Unless the current worksheet is tempsheet then Cells(2, 2) will not be the value your expecting, neither will Cells(Stocks + 1, 2).
you could try:
With Worksheets("TempSheet")
WeightsVector = .Range(.Cells(2, 5), .Cells(Stocks + 1, 2))
end with
although looking at the screenshot you have added it doesnt appear cells(2,5) is from tempsheet
I have 2 column table with columns as bunker index and bunker name.. i have to index the bunker using VBA and then generate another table with columns as the Bunker names and 'first column as the date...
On changing the number of bunkers( like from 10 to 8).. the color of the cells(last 2 columns previosly filled) in second table remains the same (blue), which i require as default (white)
I have used many variables from my workbook. Any suggestion in this regard. As to how should the formatting be done?
This is the Code :
Sub Bunker_index_Click()
Sheet3.Range(Sheet3.Cells(4, 6), Sheet3.Cells(500, 100)).ClearContents
Dim N As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim LocalIYM As Date
LocalIYM = ThisWorkbook.IYM
N = Application.CountA(Range("B:B")) - 1
ThisWorkbook.NumBunker = N
MsgBox (N)
Cells(5, 1).Value = "Bunker Index"
For i = 1 To 100
If i <= N Then
Cells(5 + i, 1).Value = i
Cells(5 + i, 1).Interior.Color = RGB(253, 233, 217)
Cells(5 + i, 2).Interior.Color = RGB(219, 238, 243)
Else
Cells(5 + i, 1).ClearContents
Cells(5 + i, 2).ClearContents
End If
Next i
Range("F5").Value = "Time (LOCKED)"
Range("F5").Interior.Color = RGB(253, 233, 217)
For i = 1 To 100
If i <= N Then
Cells(5, 6 + i).Value = Cells(5 + i, 2)
Cells(5, 6 + i).Interior.Color = RGB(253, 233, 217)
Else
Cells(5, 6 + i).ClearContents ' unable to bring back th original
' color of the cells
End If
Next i
For k = 1 To 12 * 1
If k <= ThisWorkbook.N Then
Cells(k + 5, 6).Value = LocalIYM
LocalIYM = DateAdd("m", 1, LocalIYM)
Cells(5 + k, 6).Interior.Color = RGB(253, 233, 217) ' problem handling borders
' Range("B2").Borders(xlEdgeLeft).LineStyle = XlLineStyle.xlDashDot
' 1edgeleft).LineStyle = x1linestyle.x1countinous
For j = 1 To N
Cells(5 + k, 6 + j).Interior.Color = RGB(219, 238, 243)
Next j
Else
Sheet2.Cells(i + 4, 6).Clear
End If
Next k
End Sub
Instead of .clear or .clearcontents Try:
.Interior.Pattern = xlNone