July 29, 2018
Dim vNames As Range
Dim vMyArray As Variant
'---Collects data from the other sheet
vMyArray = Sheets("J2-Trading Journal").Range("T19:T5000").Value
'---Then have it pasted to my report
'---I do not want to use these following lines to print the data into another sheet:
'Sheets("TS-Traded Stocks").Range("C8:C5000").Value = vMyArray
'Sheets("TS-Traded Stocks").Range("C8:C5000").RemoveDuplicates Columns:=1, Header:=xlNo
'Sheets("TS-Traded Stocks").Range("C8:C5000").Sort Key1:=Range("C8"), Order1:=xlAscending, Header:=xlNo
'---Instead, I wish to remove and sort the data in an array (vMyArray) itself
'Worksheet / Source: J2-Trading Journal
'Data from T19 to T22 (T22 or more...)
' T19 (dep.) - not to be included in an array
' T20 SMPH
' T21 HOUSE
' T22 HOUSE - duplicate, need to remove from array
'Worksheet / Result: J2-Traded Stocks
'New data: vMyArray (the word enclosed in parenthesis been removed, no duplicate, and sorted)
' C8 HOUSE
' C9 SMPH
'Then it's the time am going to use this line to print the content of an array:
'Sheets("TS-Traded Stocks").Range("C8:C5000").Value = vMyArray
'Is it possible?
Dim vCell As Range
For Each vCell In vNames
MsgBox Mid(vCell.Value, 1, 2)
If Mid(vCell.Value, 1, 1) = "(" Then
'remove content enclosed in parentheses from array: vMyArray
End If
MsgBox vCell.Value '= cell.Value & " " & cell.Offset(0, 1).Value
Next vCell
Trusted Members
December 20, 2019
July 29, 2018
I can't figure out how to manipulate an array. I attached the excel file in my main post which contains the data for reference.
1. Worksheet 1: J2-Trading Journal
Content / Data from column T19 to T22 (Security Code, this will be the content of an array):
(dep.)
SMPH
HOUSE
HOUSE
ABC
etc. ...
2. Worksheet 2: TS-Traded Stocks
(Output data from J2-Trading Journal, to appear from C8, C9, C10, and so on...)
Data based on Worksheet #1:
ABC
HOUSE
SMPH
3. Program specification
a. Collect the data from J2-Trading Journal worksheet under the "Security Code" table.
Here's the code I used to get them:
Dim vMyArrary() As Variant
vMyArray = Sheets("J2-Trading Journal").Range("T19:T5000").Value
b. Array manipulation requirements:
- Remove non-security code data (enclosed in parenthesis, e.g., (deposit), (subscribe), (etc.))
- Remove the duplicate (data in array)
- Sort the data within an array in ascending order.
- The content of an array (vMyArray) should be manipulated first before updating the J2-Traded Stocks (is it possible?).
- Then I will apply this code:
Sheets("TS-Traded Stocks").Range("C8:C5000").Value = vMyArray
4. Program coding
Option Explicit
Sub RefreshTSData()
Dim vMyArray As Variant
Dim vNames As Range
Dim vCellData As Variant
Dim iCtr As LongSet vNames = Sheets("J2-Trading Journal").Range("T19:T5000")
For Each vCellData In vNames
If Mid(vCellData.Value, 1, 1) = "(" Then
'do not add to array
Else
'validate: if duplicate value, do not add
'validate: if not duplicate, add to array
vMyArray(iCtr) = vCellDataEnd If
Next vCellData
Next iCtr
Sheets("TS-Traded Stocks").Range("C8:C5000").Value = vMyArray
'Debug.Print "No.", "Stock Code"
'For iCtr = 1 To 6 ''6= last row of non-empty cells
'Debug.Print iCtr, vMyArray(iCtr)
'Next iCtrEnd Sub
5. Notes:
- I am a newbie in programming and I would like to learn more, now on how to use and manipulate data in arrays
- I would like to apply coding directly to my personal and actual database
Trusted Members
December 20, 2019
The first thing I would say is don’t use Merged cells - they are the antichrist of excel and will only cause problems later on, if not now. You can use 'Centre Across Selection' instead which achieves the same visual results but doesn’t cause cell problems.
Personally I wouldn’t use arrays for this, I would copy column T and paste into a temporary worksheet, do the actions you want then copy it to where it is needed. Much simpler in my opinion and you can see the results as you step thru the code.
July 29, 2018
Got your point.
But my excel file is for distribution (once completed), the reason why I would like to automate things.
Plus, as I pointed out, to learn and practice coding purposes. At the same time applying those codes to my live data. I will start using this by January 2021.
Trusted Members
December 20, 2019
Sorry my post wasnt clear - I agree that repeatable actions should be automated, i just dont use arrays like you have suggested.
It would be automated, use VBA to do all the steps mentioned but on a temp sheet. Then either hide or delete the sheet.
Something like this
Sub Temp()
Worksheets.Add.Name = "Temp"
Worksheets("J2-Trading Journal").Range("T19:t5000").Copy Worksheets("Temp").Range("a1")
Worksheets("Temp").Activate
Range("1:1").Rows.Insert
Range("a1") = "Stock"
Range("b1") = "Check"
Range("b2:b5000") = "=IF(A2="""","""",ISNUMBER(SEARCH(""("",A2)))"
Range("a:a").Sort key1:=Columns(1), order1:=xlAscending, Header:=xlYes
Range("1:1").AutoFilter field:=2, Criteria1:="false"
Range("a2:a5000").Copy Worksheets("TS-Traded Stocks").Range("c8")
Worksheets("TS-Traded Stocks").Activate
Application.DisplayAlerts = False
Worksheets("Temp").Delete
Application.DisplayAlerts = False
End Sub
1 Guest(s)