+ Reply to Thread
Results 1 to 7 of 7

Loop - Shorten my code

Hybrid View

  1. #1
    Registered User
    Join Date
    05-21-2014
    Posts
    7

    Loop - Shorten my code

    Hello,
    i have got several stock data sets and would like to delete automatically every date with an volume of zero.
    I have for every stock another loop, is it possible to shorten this? By now I have only 3 stocks that makes it easy to handle, but i assume that it will be confusing with more stocks as I have to add another loop for every stock.

    The code is part of an portfolio optimization code, I'm starting to set up...
    Dim i As Integer, j As Integer, k As Integer
        Dim letzteZeileBMW As Long:    letzteZeileBMW = wsBMW.Cells(2, 6).End(xlDown).Row
        Dim lZBASF As Long:            lZBASF = wsBASF.Cells(2, 6).End(xlDown).Row
        Dim lZRWE As Long:             lZRWE = wsRWE.Cells(2, 6).End(xlDown).Row
         
        For i = 1 To letzteZeileBMW
            With wsBMW
            If .Cells(i, 6) = "0" Then
            .Rows(i).Delete
            End If
            End With
        Next i
         
        For j = 1 To lZBASF
            With wsBASF
            If .Cells(j, 6) = "0" Then
            .Rows(j).Delete
            End If
            End With
        Next j
         
        For k = 1 To lZRWE
            With wsRWE
            If .Cells(k, 6) = "0" Then
            .Rows(k).Delete
            End If
            End With
        Next k

    Thank you very much for your help!

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Loop - Shorten my code

    Something like:
    for each ws in activeworkbook.sheets
      for i = ws.cells(2,6).end(xldown).row to 1
        if ws.cells(i,6) = "0" then .rows(i).delete
      next i
    next ws
    replace activeworkbook.sheets with the collection of sheets you want to apply it to or put a test in to skip certain sheets.

    Note - I changed your code to loop backwards, so that it won't skip rows (i.e. if you delete row 5, row 6 becomes row 5, but the i variable will still become row 6 the next time, so what was row 6 is effectively skipped

  3. #3
    Registered User
    Join Date
    05-21-2014
    Posts
    7

    Re: Loop - Shorten my code

    Quote Originally Posted by ragulduy View Post
    Something like:
    for each ws in activeworkbook.sheets
      for i = ws.cells(2,6).end(xldown).row to 1
        if ws.cells(i,6) = "0" then .rows(i).delete
      next i
    next ws
    replace activeworkbook.sheets with the collection of sheets you want to apply it to or put a test in to skip certain sheets.

    Note - I changed your code to loop backwards, so that it won't skip rows (i.e. if you delete row 5, row 6 becomes row 5, but the i variable will still become row 6 the next time, so what was row 6 is effectively skipped
    Doesn't work with me...VBA is marking
    .Rows(i)
    and says Error at compiling: incorrect or inadaequalty defined "reference" --> rough translation
    Last edited by mibikeks; 06-18-2014 at 02:34 PM.

  4. #4
    Valued Forum Contributor
    Join Date
    11-02-2012
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    564

    Re: Loop - Shorten my code

    It is possible to shorten. Are wsBMW, wsBASF etc are worksheets? Can you upload the workbook?

  5. #5
    Registered User
    Join Date
    05-21-2014
    Posts
    7

    Re: Loop - Shorten my code

    Hello,

    thanks for your quick replys.
    Here is my workbook:http://www.herber.de/bbs/user/91168.xlsm

  6. #6
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Loop - Shorten my code

    sorry, it should be ws.rows(I).delete

  7. #7
    Registered User
    Join Date
    05-21-2014
    Posts
    7

    Re: Loop - Shorten my code

    Well,
    thanks for your help! My code is running but it's not working
    Option Explicit
    Option Base 1
    
    Sub 1()
        
        Dim wsBMW As Worksheet:     Set wsBMW = Worksheets("BMW")
        Dim wsBASF As Worksheet:    Set wsBASF = Worksheets("BASF")
        Dim wsRWE As Worksheet:     Set wsRWE = Worksheets("RWE")
        
        Dim ws As Worksheet
       
        Dim letzteZeileBMW As Long:    letzteZeileBMW = wsBMW.Cells(2, 6).End(xlDown).Row
        Dim lZBASF As Long:            lZBASF = wsBASF.Cells(2, 6).End(xlDown).Row
        Dim lZRWE As Long:             lZRWE = wsRWE.Cells(2, 6).End(xlDown).Row
        
           Dim i As Integer
     
    For Each ws In ActiveWorkbook.Sheets
      For i = ws.Cells(2, 6).End(xlDown).Row To 1
        If ws.Cells(i, 6) = "0" Then ws.Rows(i).Delete
      Next i
    Next ws
       
           
    End Sub
    I tried it, but it doesn't delete anything in my Excel-Worksheet. I have attached my file: http://www.herber.de/bbs/user/91195.xlsm

    Thank you very much for your help!

+ 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. Loop to shorten repetitive and long code?
    By humboldtguy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-16-2013, 03:29 AM
  2. Create a loop by defining X as integer, to shorten length of code
    By ahmerjaved in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-11-2013, 01:11 PM
  3. [SOLVED] Very inefficient code because of different sizes of arrays, how do i shorten my code?
    By Brammer88 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-28-2012, 04:49 PM
  4. [SOLVED] Shorten up som sheets-kode maby a loop !
    By excelent in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-05-2006, 04:00 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