+ Reply to Thread
Results 1 to 15 of 15

Convert Array set elements to Excel sheet and allow to user to add new data

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    696

    Convert Array set elements to Excel sheet and allow to user to add new data

    Hi,

    I have "array" set which representing branches name. For the time being, there are 4 branches and it expected to be grown in future. Using "array" set code in VBA would not help user if there are new branches to be added.

    Thus, my idea to convert "array" set data to excel sheet and allow user to add new data using Userform command button. I am familiar with that approach but my problem is how to convert array set elements to excel sheet and the VBA code can also be reflected with new data added by user

    VBA code will perform the following function,
    Opening Workbook
    Filtering Criteria
    Deleting Worksheet

    as described below.


    e.g.
    Array set elements to be stored in Worksheets("Data") starting from A2.

    A2 Battambang
    A3 Chbar Ampov
    A4 Chroy Changvar
    A5 Kampong Cham
    A6 .......
    A7 .......
    A?? ........ new data to be added by user


    Sub Test()
    
    strPath = ThisWorkbook.Path & "\"
        ASC = Array("Battambang.xlsx", _
                    "Chbar Ampov.xlsx", _
                    "Chroy Changvar.xlsx", _
                    "Kampong Cham.xlsx")
                      
                      
    For i = LBound(ASC) To UBound(ASC)
        Workbooks.Open Filename:=strPath & ASC(i)
        ActiveWindow.WindowState = xlMinimized
    Next i
                      
                                      
        If ws.Name = "Battambang" Then
                With rng
                     .AutoFilter
                     .AutoFilter Field:=4, Criteria1:="Battambang"
                     .AutoFilter Field:=28, Criteria1:=">0"
                End With
        
        ElseIf ws.Name = "Chbar Ampov" Then
                With rng
                     .AutoFilter
                     .AutoFilter Field:=4, Criteria1:="Chbar Ampov"
                     .AutoFilter Field:=28, Criteria1:=">0"
                End With
         
        ElseIf ws.Name = "Chroy Changvar" Then
                With rng
                     .AutoFilter
                     .AutoFilter Field:=4, Criteria1:="Chroy Changvar"
                     .AutoFilter Field:=28, Criteria1:=">0"
                End With
                        
        ElseIf ws.Name = "Kampong Cham" Then
                With rng
                     .AutoFilter
                     .AutoFilter Field:=4, Criteria1:="Kampong Cham"
                     .AutoFilter Field:=28, Criteria1:=">0"
                End With
        
    Application.DisplayAlerts = False
    Worksheets(Array("Battambang", "Chbar Ampov", "Chroy Changvar", "Kampong Cham")).Delete
    Application.DisplayAlerts = True
    End Sub
    Last edited by Faridwahidi; 10-13-2014 at 10:50 PM.

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Convert Array set elements to Excel sheet and allow to user to add new data

    Hi Farid,

    I'm not sure what you're attempting but maybe:

    Sub FaridW(): Dim strPath As String, Asc, i As Integer, ws As Worksheet, rng As Range
    
                    strPath = ThisWorkbook.Path & "\"
    Asc = Array("Battambang.xlsx", "Chbar Ampov.xlsx", "Chroy Changvar.xlsx", "Kampong Cham.xlsx")
                      
    For i = LBound(Asc) To UBound(Asc)
        Workbooks.Open FileName:=strPath & Asc(i)
        Set ws = ActiveSheet: Set rng = ws.Range("A1").CurrentRegion
        ActiveWindow.WindowState = xlMinimized
        
                With rng
                     .AutoFilter Field:=4, Criteria1:=Asc(i)
                     .AutoFilter Field:=28, Criteria1:=">0"
                End With
                
                Next i
    
    End Sub
    *do you want to leave the books open? What do you intend to do with the filtered data?
    Last edited by xladept; 10-14-2014 at 04:36 PM.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: Convert Array set elements to Excel sheet and allow to user to add new data

    Hi Xladept,

    Sorry for my poor explanation,

    What I attempted to do is firstly to place array set elements to excel sheets.
    Asc = Array("Battambang.xlsx", "Chbar Ampov.xlsx", "Chroy Changvar.xlsx", "Kampong Cham.xlsx")
    This will be allow user to add new elements using Userfom Textbox.


    e.g. I have placed all elements starting from M2 and tried this code
    ASC = Array(ws.Range("M2"), ws.Range("M" & Rows.count).End(xlUp))
    but it only applicable for the 1st and last element. How to make array set is readable to all elements?

  4. #4
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Convert Array set elements to Excel sheet and allow to user to add new data

    Okay from the last posted reply..
    I have concluded this..( I hope this will help )
    ASC= Range(Cells(ws.Range("M2").address),ws.Range("M" & Rows.count).End(xlUp).address)).value

    Now ASC has been an array .. which you can refer like this..
    ASC(R,1) where R is the row variable starting from 1 and 1 is the column Constant...


    Regards,
    Regards,
    Vikas Gautam
    Excel-buzz.blogspot.com

    Excel is not a matter of Experience, its a matter of Application.

    Say Thanks, Click * Add Reputation

  5. #5
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: Convert Array set elements to Excel sheet and allow to user to add new data

    Hi Vikas,

    It could be the syntax error, the programme stop running at

    ASC = Range(Cells(ws.Range("M2").Address), ws.Range("M" & Rows.count).End(xlUp).Address).Value

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Convert Array set elements to Excel sheet and allow to user to add new data

    Hi, Faridwahidi,

    using Array for the area will just cover the first and the last elements as they are mentioned.

    Maybe try:
    ASC = Ws.Range("M2", ws.Range("M" & Rows.Count).End(xlUp)).Value
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Convert Array set elements to Excel sheet and allow to user to add new data

    Maybe:

    Asc = Array(ws.Range("M2:M" & ws.Range("M" & Rows.Count).End(xlUp).Row))

  8. #8
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Convert Array set elements to Excel sheet and allow to user to add new data

    Sorry..
    Its mistake..
    Formula: copy to clipboard
    ASC = Range("M2:" & ws.Range("M" & Rows.count).End(xlUp).Address).Value

    rest explanations are same..


    Edit :- Tried to work upon Adept's Suggestion....
    Last edited by Vikas_Gautam; 10-15-2014 at 02:33 AM.

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Convert Array set elements to Excel sheet and allow to user to add new data

    @ Vikas - Why do you keep putting Address in the code? - it's just redundant and, probably a breach of syntax.

    @ Holger - I didn't realize that you had posted when I made my post.

  10. #10
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: Convert Array set elements to Excel sheet and allow to user to add new data

    Hi xladept,

    I have tried to replace both of your array set code and also the one suggested by Holger, but not working. The file unable to be opened.
    All elements have been placed to Worksheets("Data") starting from M2 to M5

    M2 Battambang - BTM.xlsx
    M3 Chbar Ampov - CBA.xlsx
    M4 Chroy Changvar - CCV.xlsx
    M5 Kampong Cham - KCM.xlsx
    ASC = Array(ws.Range("M2:M" & ws.Range("M" & Rows.count).End(xlUp).Row))
    ASC = ws.Range("M2", ws.Range("M" & Rows.count).End(xlUp)).Value
    But the original Array set working fine.
    Sub Test()
    Dim strPath As String, ASC
    Dim i As Integer
    Dim ws As Worksheet
    
    strPath = ThisWorkbook.Path & "\"
    Set ws = Worksheets("Data")
    ASC = Array("Battambang - BTM.xlsx", "Chbar Ampov - CBA.xlsx", "Chroy Changvar - CCV.xlsx", "Kampong Cham - KCM.xlsx") 
                    
        For i = LBound(ASC) To UBound(ASC)
                Workbooks.Open Filename:=strPath & ASC(i)
                ActiveWindow.WindowState = xlMinimized
        Next i
    End Sub
    Last edited by Faridwahidi; 10-15-2014 at 02:40 AM.

  11. #11
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Convert Array set elements to Excel sheet and allow to user to add new data

    Hi, Faridwahidi,

    the whole code you posted is worth a severe workover as it´s lacking lots of different things. Any reason for storing the values in a range on a worksheet and adding an Array for getting the values from these cells? None I´d say.

    For Each rngCell In Range("M2", Range("M" & Rows.Count).End(xlUp))
      Workbooks.Open Filename:=strPath & rngCell
      ActiveWindow.WindowState = xlMinimized
    Next rngCell
    What is ws in your sample? You opened a certain number of worksheets but how do the worksheets are being looped as that part is lacking?
    Why should the worksheets be deleted? In which workbook would we find the worksheets?

    Ciao,
    Holger

  12. #12
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Convert Array set elements to Excel sheet and allow to user to add new data

    Hi Farid,

    I still don't know what you are attempting - but for a worksheet array for column M from row 2 to the end this code works:

    Asc = ws.Range("M2:M" & ws.Range("M" & Rows.Count).End(xlUp).Row)

  13. #13
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: Convert Array set elements to Excel sheet and allow to user to add new data

    Hi Holger / Xladept,

    Thanks a lot for your help.

    The opening code at post #11 is a part of what I attempting to do. The reason I store the values in a range on a worksheet is allow to user to add new data using userform Textbox . If we use array set in VBA code, user are not able to add new elements.

    What I attempting to do is actually to create a simple customize data.

  14. #14
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Convert Array set elements to Excel sheet and allow to user to add new data

    You're welcome and thanks for the rep!

  15. #15
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Convert Array set elements to Excel sheet and allow to user to add new data

    ASC =
    The above construction used in several of the previous posts in this thread should probably be avoided since 'Asc' is a KEYWORD that represents a built in VBA function to convert the first letter in a string to an integer.

    Option Explicit
    
    Sub UseBuiltInFunctionAsc()
    
      Dim i As Integer
      
      i = Asc("C")   'The value of i is 67
      MsgBox "Asc(" & Chr(34) & "C" & Chr(34) & ") = " & i
    
    End Sub
    Lewis
    Last edited by LJMetzger; 10-16-2014 at 06:32 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Removing specific elements from an Array based on matching elements in a different Array
    By jamesdeuce in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-24-2014, 10:35 PM
  2. Convert Array Formula into VBA User Defined Function
    By hhhhwong in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-05-2013, 12:38 AM
  3. [SOLVED] Excel VBA Last Empty Row VBA paste Array elements
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-06-2006, 12:45 PM
  4. [SOLVED] convert excel data to 2-D array using macro
    By David in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-13-2005, 10:10 AM
  5. Replies: 2
    Last Post: 04-07-2005, 02:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1