+ Reply to Thread
Results 1 to 3 of 3

VB Loop to Mark Used Data

Hybrid View

Ricker090 VB Loop to Mark Used Data 07-27-2011, 03:38 PM
davegugg Re: VB Loop to Mark Used Data 07-27-2011, 03:57 PM
Ricker090 Re: VB Loop to Mark Used Data 07-27-2011, 04:09 PM
  1. #1
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    157

    VB Loop to Mark Used Data

    I need help with a macro that I wrote. I have two data ranges; one is a list of awarded training spot and the other a list of numbers that represent all the available training spots. I wrote a loop to put an "X" next to each spot that is used. It doesn't seem to work the way I want it to though. For example, if you look at August 10th, there were only two awarded spots, but my current macro puts an "X" next to all the spots on August 10th.

    I going insane trying to figure this out. Any suggestions are appreciated. Also, if there's a better and more efficient way to write this macro, I'm open to suggestions.

    I have attached my workbook.
    Attached Files Attached Files
    Last edited by Ricker090; 07-27-2011 at 04:09 PM. Reason: Modified example workbook

  2. #2
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: VB Loop to Mark Used Data

    I'd suggest this amendment to your code:

    Sub Used_Spaces()
    
        Dim srcR, destR, L As Integer
        
        For srcR = 2 To Cells(Rows.Count, 1).End(xlUp).Row
            If IsNumeric(Cells(srcR, 1).Value) Then
                L = Cells(srcR, 1).Value
                For destR = 2 To Cells(Rows.Count, 5).End(xlUp).Row
                    If L = Cells(destR, 4).Value And Cells(destR, 8).Value = "" Then
                        Cells(destR, 7).Value = "X"
                        Exit For
                    End If
                Next destR
            End If
        Next srcR
    
    End Sub
    First, find ways to not use "On Error Resume Next". This can lead to all sorts of problems. To make the code work without it, I check each cell in column A to make sure it is a number before attempting to reserve a spot with it.
    Next to make sure only one matching spot is Xed, I add an Exit For which will get out of the inner For loop once a cell has been marked X. In general it is bad to use Exit For, but for this simple code it is easier than the alternative.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    157

    Re: VB Loop to Mark Used Data

    Excelent! Thank you for the advise. It's very much appreciated. I learn something new everytime I log into this forum.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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