Results 1 to 6 of 6

Thread: Loop in Array

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Loop in Array

    Team,

    I am finally moving from manipulating data in worksheets to working with arrays.

    Can someone suggest an alternative to to the BOLD array loop? Is there a way to change AH(a,2) thru Ah(a,8) to zeros without looping?


    Code:
    Option Explicit
    Sub ChangeRows()
    Dim AH, a As Long, aa As Long
    AH = Cells.Cells(1).CurrentRegion.Resize(, 8)
    For a = LBound(AH) To UBound(AH)
      If AH(a, 1) = "DEF" Or AH(a, 1) = "GHI" Then
    
        For aa = 2 To 8
          AH(a, aa) = 0
        Next aa
    
      End If
    Next a
    Cells.Cells(1).CurrentRegion.Resize(, 8) = AH
    End Sub

    Thanks in advance.

    Have a great day,
    Stan
    Last edited by stanleydgromjr; 07-22-2011 at 04:11 PM.

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,122
    Rep Power
    10
    Hi Stan,

    Welcome to ExcelFox !!

    I don't think it's possible. I might be wrong though.

    You could try this, which uses less loop

    Code:
    Sub ChangeRows()
        
        Dim AH, a As Long, aa As Long
        Dim r As Range, ka() As String, k  As String, n As Long
        
        With Range("a1")
            AH = .CurrentRegion.Resize(, 8)
            Set r = .Cells(1, 2).Resize(.CurrentRegion.Rows.Count, 7)
        End With
        
        For a = LBound(AH) To UBound(AH)
          If AH(a, 1) = "DEF" Or AH(a, 1) = "GHI" Then
                n = n + 1
                ReDim Preserve ka(1 To n)
                ka(n) = a
          End If
        Next a
        Cells.Cells(1).CurrentRegion.Resize(, 8) = AH
        If n Then
            With r
                For a = 1 To n
                    .Rows(ka(a)).Value = 0
                Next
            End With
        End If
    
    End Sub

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0
    Admin,

    Thank you for the welcome to ExcelFox.

    I tried your code, and it did give a different approach to the problem - results were the same.

    This is a keeper for my archives.

    Thank you very much.


    Have a great day, and weekend,
    Stan

  4. #4
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,122
    Rep Power
    10
    Hi Stan,

    You are welcome !

    Have a great weekend to you as well

  5. #5
    Senior Member
    Join Date
    Apr 2011
    Posts
    190
    Rep Power
    14
    You can maybe use some of these comands-These are just tings to consider - hope they give you pointers


    Code:
    Option Explicit 'forces you to declare variables
    Option base 1 'All arrays are diminsioned using base 1 rather than zero - in many cases Excel uses base 1 when creating arrays
    
    Dim ah() AS variant 'By leaving the parentesies empty - you can use the Redim later
    
    'Now use
    Redim ah(1 to NumRows, 1 to NumColumns) 'Makes it a specific size -or use it with Preserve to simply change the size of the array - without losing current values.
    
    Erase ah ' will delete entire array
    xl2007 - Windows 7
    xl hates the 255 number

  6. #6
    Junior Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0
    Rasm,

    More good information for my archives.

    Thank you for responding.

    Have a great day,
    Stan

Similar Threads

  1. Delete worksheets without loop
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 5
    Last Post: 03-04-2014, 07:29 PM
  2. VBA Trick of the Week :: Slicing an Array Without Loop - Application.Index
    By Transformer in forum Tips, Tricks & Downloads (No Questions)
    Replies: 0
    Last Post: 06-12-2013, 04:40 PM
  3. Loop to two columns and Concatenate values
    By ivandgreat in forum Excel Help
    Replies: 15
    Last Post: 04-14-2013, 08:20 PM
  4. Loop through a folder and find word
    By k0st4din in forum Excel Help
    Replies: 7
    Last Post: 12-08-2012, 02:22 PM
  5. Speed up Loop VBA
    By PcMax in forum Excel Help
    Replies: 15
    Last Post: 04-09-2012, 04:20 PM

Tags for this Thread

Posting Permissions

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