+ Reply to Thread
Results 1 to 6 of 6

Trouble Looping Through an Excel Range using a 2D 'For Loop' with VBA

  1. #1
    Registered User
    Join Date
    02-27-2013
    Location
    USA, Earth
    MS-Off Ver
    Excel 2010
    Posts
    16

    Question Trouble Looping Through an Excel Range using a 2D 'For Loop' with VBA

    Hello all,

    Would someone please be able to assist me with this problem? Currently I am trying to loop through an arbitrary range in excel using VBA to fill any empty (null) cells with zeroes. I know how this could be done with an array in C / C++ or MATLAB, but can't seem to figure this out even after searching prior posts.

    Here is the relating code segment, which is to be used with a button:

    Private Sub Layout_populate__buttn_Click()

    Application.ScreenUpdating = False ' Temporarily Disable Screen Updating:

    ' Define Variables:
    ' ========================================================
    ' Worksheets:
    ' ------------
    Dim Main_populated__sheet As Worksheet ' Main (current) worksheet to work off of
    Dim Linear_Interpolation_1__sheet As Worksheet ' Sheet that data is saved onto and to be worked with in this example

    ' Iterators:
    ' ------------
    Dim Count_i As Integer ' Loop position in Cartesian x-direction
    Dim Count_j As Integer ' Loop Position in Cartesian y-direction
    Dim Iterator_placement_A As Integer ' Iterator defined during the main code run

    ' Other Variables defined in the rest of the code:
    ' ----------------------------------------------
    Dim Selection_total_range As Integer
    Dim Column_A As Integer
    Dim Column_B As Integer

    ' Initialize Variables:
    ' ========================================================
    Linear_Interpolation_1__sheet = Sheets("Linear_Interpolation_1")

    Count_i = 0
    Count_j = 0

    ' The following are defined in the code prior to this point,
    ' eliminating a .Range("X#:Y#") solution
    ' --------------------------------------------------
    Iterator_placement_A = 16
    Selection_total_range = 177
    Column_A = 2 ' Column "B"
    Column_B = 6 ' Column "G"


    ' Fill Empty Cells with Zeroes:
    ' ========================================================

    With Linear_Interpolation__sheet_1 ' Select Worksheet to do work on

    For Count_i = Column_A To Column_B ' Columns to loop through

    For Count_j = Iterator_placement_A To Selection_total_range Step 1 ' Rows to loop through

    If Linear_Interpolation__sheet_1.Range(Cells(Count_j, Count_i)).txt = "" Then

    Linear_Interpolation__sheet_1.Range(Cells(Count_j, Count_i)).Value = 0
    ' If cell looked at is blank, fill it with a zero

    End If

    Next Count_j
    Next Count_i
    End With

    ' Re-Enable Screen Updating:
    ' =====================================================
    Application.ScreenUpdating = True

    End Sub

    If possible please explain what differences are present if you find a solution to this problem. I actually want to learn and understand what it is my code is doing rather than 'just making it work.'

    This perhaps should be in a seperate post, but why is it that in Excel the traditional Cartesian X and Y planes are reversed? I have seen it in a few places where one will select:

    .Cells(row_position, column_position)

    This just seems rather confusing and unintuitive to me.


    Thanks in advance for any help!

    ~ Sarvazad


    EDIT: I have attached the quoted region in a quick text file for cleaner viewing, since all the spacing does not appear on the web page.
    Issue_w_2D_for_loop_(example_file).txt
    Last edited by Sarvazad; 03-14-2013 at 03:40 PM. Reason: attaching file

  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: Looping Through an Excel Range using a 2D For Loop with VBA

    Try:

    Please Login or Register  to view this content.
    Matrices are designated r,c ?!

    If you've been ooping then you're not used to having to set objects since in oop everything is already an object - I think that's probably the biggest difference you're facing
    Last edited by xladept; 03-14-2013 at 03:46 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
    Registered User
    Join Date
    02-27-2013
    Location
    USA, Earth
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Looping Through an Excel Range using a 2D For Loop with VBA

    Hi xladept,

    Thank you for the reply. I have tried your suggestion, and even furthered this with:

    For Count_i = 2 To 6 Step 1
    However, I am still having no luck. Any other ideas?


    I have considered other types of loops, but the core issue would still be the error:

    " Run-time error '1004': Method 'Range' of object '__Worksheet' failed " at the line:
    If Linear_Interpolation__sheet_1.Range(Cells(Count_j, Count_i)).txt = "" Then

  4. #4
    Forum Contributor
    Join Date
    01-13-2013
    Location
    Oklahoma
    MS-Off Ver
    Office 2007 / Office 2010
    Posts
    123

    Re: Looping Through an Excel Range using a 2D For Loop with VBA

    Here's your existing code fixed. Everything in red is what I added or changed.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    02-27-2013
    Location
    USA, Earth
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Looping Through an Excel Range using a 2D For Loop with VBA

    Yraen, thank you very much! That was an absolutely perfect reply, and directly fixed my problems!

    I had rushed to make a tinier code to illustrate the problems I was having but unfortunately missed typing the "Set" command earlier in the code. The set actively being used was just to illustrate that the work being done was not on the current sheet, right in the code for anyone who may refer to this later.

    I had no idea you could directly use the ".Cells()" command on its own like that without the range. Between that and changing the ".txt" to a ".value" fixed everything.

    I have to ask, how did you insert your code changes like that so neatly? It would be nice to not force anyone to read code in italics and without the white space organization in the future haha.

    Also, when is it appropriate to use the '.txt' versus the '.value' command? This clearly caused a problem here as well, and would like to not make a similar mistake.

    Thank you again!

  6. #6
    Forum Contributor
    Join Date
    01-13-2013
    Location
    Oklahoma
    MS-Off Ver
    Office 2007 / Office 2010
    Posts
    123

    Re: Trouble Looping Through an Excel Range using a 2D 'For Loop' with VBA

    Thanks for the rep.

    For the code tags, go to the advanced editor, paste your code, select it and click the pound (#) sign above. Or use [ code ] and [ /code ] without the spaces.

    I'm unaware of any .txt field in excel VBA. I'm not saying it doesn't exist, just I've never used it. .Value is used for the value of the cell, .Formula for the formula, .FormulaR1C1 for the formula using the R1C1-style notation.

+ 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