Consulting

Results 1 to 5 of 5

Thread: elements of array

  1. #1

    elements of array

    Sub ta()
    Dim mar
    Dim i As Integer, n As Integer
    Dim rg As Range
    Dim sh
    Worksheets.Add.Name = "sheet2"
    Worksheets("sheet1").Range("a1:s2").Copy
    Worksheets("sheet2").Rows(1).Select
    Worksheets("sheet2").Paste
    With Worksheets("sheet1")
    Set rg = .Range("a1").CurrentRegion
    mar = rg
    n = 3
        For i = 3 To UBound(mar)
            If (mar(i, 3) > mar(i, 6) And mar(i, 7) * 0.8 > mar(i, 5)) Then
                .Rows(i).Copy
                Worksheets("sheet2").Select
                Rows(n).Select
                Worksheets("sheet2").Paste
                n = n + 1
            End If
        Next i
    End With
    End Sub
    Why some elements of array format is string, some double?
    Attached Images Attached Images
    Last edited by idnoidno; 05-23-2017 at 12:30 AM.

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Please do not post unreadable pictures. Post a sample file instead.

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If you feed in mixed formats you will get mixed formats
    Sub test()
    Dim mar, mar1
    mar = Array(1, "two", 3.05, 3.11111111111111)
    Cells(1, 1).Resize(, 4) = mar
    mar1 = Cells(1, 1).Resize(, 4)
    Cells(3, 1).Resize(, 4) = mar1
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    mar is Dim-ed as a Variant (as is sh)

    rg is a 2D range of Variant (since the cells can hold anything, strings, doubles, etc.)

    So

    mar = rg


    makes mar a 2D array of Variant


        Dim mar 
        Dim rg As Range 
        Dim sh 
        Worksheets.Add.Name = "sheet2" 
        Worksheets("sheet1").Range("a1:s2").Copy 
        Worksheets("sheet2").Rows(1).Select 
        Worksheets("sheet2").Paste 
        With Worksheets("sheet1") 
            Set rg = .Range("a1").CurrentRegion
            mar = rg
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Sub M_snb()
      Sheets.Add.Name = "sheet2"
      Sheets("sheet1").Range("a1:s2").Copy Sheets("sheet2").Cells(1)
    
      With Sheets("sheet1")
        Set rg = .Cells(1).CurrentRegion
        mar = .Cells(1).CurrentRegion
      End With
    
      MsgBox TypeName(rg)
      MsgBox TypeName(mar)
    End Sub

Posting Permissions

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