• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Array Manipulation | Remove Duplicates and Sorts Data |VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Array Manipulation | Remove Duplicates and Sorts Data |VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosArray Manipulation | Remove Duplica…
sp_PrintTopic sp_TopicIcon
Array Manipulation | Remove Duplicates and Sorts Data
Avatar
SoniboiTM
Member
Members
Level 0
Forum Posts: 10
Member Since:
July 29, 2018
sp_UserOfflineSmall Offline
1
July 30, 2020 - 10:41 pm
sp_Permalink sp_Print

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

Avatar
Purfleet
England
Member
Members


Trusted Members
Level 4
Forum Posts: 412
Member Since:
December 20, 2019
sp_UserOfflineSmall Offline
2
July 31, 2020 - 4:40 am
sp_Permalink sp_Print

I am not sure what part is question and what part is code - can you clarify your question?

Avatar
SoniboiTM
Member
Members
Level 0
Forum Posts: 10
Member Since:
July 29, 2018
sp_UserOfflineSmall Offline
3
July 31, 2020 - 10:08 am
sp_Permalink sp_Print sp_EditHistory

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 Long

Set 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) = vCellData

End 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 iCtr

End 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

Avatar
Purfleet
England
Member
Members


Trusted Members
Level 4
Forum Posts: 412
Member Since:
December 20, 2019
sp_UserOfflineSmall Offline
4
July 31, 2020 - 2:57 pm
sp_Permalink sp_Print

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.

Avatar
SoniboiTM
Member
Members
Level 0
Forum Posts: 10
Member Since:
July 29, 2018
sp_UserOfflineSmall Offline
5
July 31, 2020 - 5:10 pm
sp_Permalink sp_Print

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.

Avatar
Purfleet
England
Member
Members


Trusted Members
Level 4
Forum Posts: 412
Member Since:
December 20, 2019
sp_UserOfflineSmall Offline
6
July 31, 2020 - 6:13 pm
sp_Permalink sp_Print

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

Avatar
SoniboiTM
Member
Members
Level 0
Forum Posts: 10
Member Since:
July 29, 2018
sp_UserOfflineSmall Offline
7
July 31, 2020 - 9:29 pm
sp_Permalink sp_Print

Thanks a lot.

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: s Ramchandran, Kristine Storti
Guest(s) 9
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
John Chisholm
vexokeb sdfg
John Jack
Malcolm Toy
Ray-Yu Yang
George Shihadeh
Naomi Rumble
Uwe von Gostomski
Jonathan Jones
drsven
Forum Stats:
Groups: 3
Forums: 24
Topics: 6212
Posts: 27237

 

Member Stats:
Guest Posters: 49
Members: 31889
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.