+ Reply to Thread
Results 1 to 28 of 28

Loop for vba macro from a specific column to the last column with data

  1. #1
    Registered User
    Join Date
    09-28-2016
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    26

    Loop for vba macro from a specific column to the last column with data

    Hi all,


    I need to create a macro that distributes proportionally a value into several columns taking into account that the value changes in each row. One important point is that the first column in which I have to start is previously stated.

    Let's take that the value is 1000 and it must paste in each column 150, starting from the thirth. I would have 0,0,150,150,150,150,150,150, and in the next one only 50 (what I have left) . In the rest, all would be 0 again, until the last one.

    The loops I'm creating don't work because I'm unable to count all the cells with data in each row (I'm using "endd = Cells(1, Cells.Columns.Count).End(xlToRight).Column")
    but I'm sure there are more wrong things over there.

    I know that there are here many very good experts... Would be anyone of you able to give a macro capable to do all these things??



    Thank very much in advance for your kind answers.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Loop for vba macro from a specific column to the last column with data

    Could you post a workbook so we can see your set up?

  3. #3
    Registered User
    Join Date
    09-28-2016
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    26

    Re: Loop for vba macro from a specific column to the last column with data

    Quote Originally Posted by StephenR View Post
    Could you post a workbook so we can see your set up?

    Thank you for your response, StephenR.

    Do you mean anything like this?

    Worksheets("NUMBER").Select

    startt = Range("B2")

    'Selection.End(xlToRight).Select

    'endd = ActiveCell.Column

    Worksheets("Sheet1").Select
    'endd = Cells(1, Cells.Columns.Count).End(xlToRight).Column

    For i = 2 To 25
    totalnumber = Range(i, 1)

    For j = 3 To endd

    totalnumber = Range(i, 1)



    Range(i, j) = 160

    totalnumber = totalnumber - 160


    If totalnumber < 160 Then
    If totalnumber > 0 Then

    Range(i, j) = totalnumber
    Else
    Range(i, j) = 0
    End If
    End If



    Next j
    Next i

    End Sub

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Loop for vba macro from a specific column to the last column with data

    Not quite! First, please can you add code tags to your post as per forum rules?

    Is this code your attempt which does not work?

    What I was asking for is for you to upload a workbook containing some data so we can see how it is arranged and how the results should appear. I don't know where the 1000, 150 etc come from.

  5. #5
    Registered User
    Join Date
    09-28-2016
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    26

    Re: Loop for vba macro from a specific column to the last column with data

    tabla.jpg


    Sorry, I hope this is closer to what you were asking for....

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Loop for vba macro from a specific column to the last column with data

    Could you post a workbook please, not a picture. It saves me from having to make something up.

    And please add the code tags as I asked last time.

  7. #7
    Registered User
    Join Date
    09-28-2016
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    26

    Re: Loop for vba macro from a specific column to the last column with data

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    09-28-2016
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    26

    Re: Loop for vba macro from a specific column to the last column with data

    I appologise, but I can't find how to upload a whole workbook. I don't have any activated icon for attaching files....

  9. #9
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Loop for vba macro from a specific column to the last column with data

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  10. #10
    Registered User
    Join Date
    09-28-2016
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    26

    Re: Loop for vba macro from a specific column to the last column with data

    Ok, thanks a lot.


    I don't know whether it would apply to use a "before" and "after" sheet, as I plan to make the changes appear directly into the table (although I always
    appreciate suggestions). The table can be imported by the user from the user's computer. This is done in the module "import". In any case, it's in the other
    module, called "automation", where I'm having problems... I need to select the values in the first column and distribute them proportionally into the date
    columns (160 each). The distribution must start in the month that corresponds to the one in the second column of the correspondent row (that's why I use the
    function "datepart" in the first module.

    The sheet called "NUMBER" has the buttons and alows the user to put manually the starting point (month). It should make the macro be more simple....


    I hope it's all more understable, now.....
    Attached Files Attached Files
    Last edited by lcgar; 10-06-2016 at 10:37 AM.

  11. #11
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Loop for vba macro from a specific column to the last column with data

    I think so. One thing - if the start month is 7 does that mean the numbers should only start from March 2017 (the 7th month across)?

  12. #12
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Loop for vba macro from a specific column to the last column with data

    See if this starts you off - I'm just not clear about the start month
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    09-28-2016
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    26

    Re: Loop for vba macro from a specific column to the last column with data

    Quote Originally Posted by StephenR View Post
    I think so. One thing - if the start month is 7 does that mean the numbers should only start from March 2017 (the 7th month across)?
    This is just a random number I typed to test the macro. It's a variable itself and corresponds to the starting point. Let's say that the first non-zero value in each column is the first month whose number is the number of that cell.

    Example 1: Number 7 --> july -->column N
    Example 2: Number 10---> october --> column E

    I know it's a bit confusing, but I think it simplifies the process, after all....

    Thank you again for the selfless effort you're making.

  14. #14
    Registered User
    Join Date
    09-28-2016
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    26

    Re: Loop for vba macro from a specific column to the last column with data

    Quote Originally Posted by StephenR View Post
    See if this starts you off - I'm just not clear about the start month
    Please Login or Register  to view this content.
    Thanks for your code, Stephan. It is not far from what I was looking for, but I would like to start in each row in the month of the date of the second column.

    On the other hand, all blanks in the table should be zero numbers.

    Thank you!

  15. #15
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Loop for vba macro from a specific column to the last column with data

    What if the month in the second column doesn't appear in the top row or is blank - there are a lot of them?

  16. #16
    Registered User
    Join Date
    09-28-2016
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    26

    Re: Loop for vba macro from a specific column to the last column with data

    Good question. Then I think that we may consider all the correspondent row values to be zero.

  17. #17
    Registered User
    Join Date
    09-28-2016
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    26

    Re: Loop for vba macro from a specific column to the last column with data

    ... Or we can just let what the macro displays without further changes.... These rows are not important, really....

  18. #18
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Loop for vba macro from a specific column to the last column with data

    OK, give this a try
    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    09-28-2016
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    26

    Re: Loop for vba macro from a specific column to the last column with data

    Run-time error '1004'
    Application-defined or object-defined error

    here: .Offset(1, 3).Resize(.Rows.Count - 1, .Columns.Count - 3).SpecialCells(xlCellTypeBlanks).Value = 0

  20. #20
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Loop for vba macro from a specific column to the last column with data

    Worked for me on your example file - are you trying it elsewhere?

  21. #21
    Registered User
    Join Date
    09-28-2016
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    26

    Re: Loop for vba macro from a specific column to the last column with data

    No.... Could you send me your Workbook, please?

    is it your macro capable to associate the date month in column 2 to the right top row value?
    Last edited by lcgar; 10-07-2016 at 10:02 AM.

  22. #22
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Loop for vba macro from a specific column to the last column with data

    It's only your file, but am having trouble attaching a workbook. Can you try uploading a file with the code?

  23. #23
    Registered User
    Join Date
    09-28-2016
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    26

    Smile Re: Loop for vba macro from a specific column to the last column with data

    Here you go.


    is it your macro able to associate the date month to the right column?
    Attached Files Attached Files

  24. #24
    Registered User
    Join Date
    09-28-2016
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    26

    Smile Re: Loop for vba macro from a specific column to the last column with data

    Here you go.


    is it your macro able to associate the date month to the right column?

  25. #25
    Registered User
    Join Date
    09-28-2016
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    26

    Re: Loop for vba macro from a specific column to the last column with data

    Here you go.



    is it your macro capable to associate the date month in column 2 to the right top row value?
    Attached Files Attached Files

  26. #26
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Loop for vba macro from a specific column to the last column with data

    I see you're having trouble with the forum too! It's all messed up here at the moment.

    Try changing this line as below
    Please Login or Register  to view this content.

  27. #27
    Registered User
    Join Date
    09-28-2016
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    26

    Re: Loop for vba macro from a specific column to the last column with data

    I works perfectly!!!! Thank you again, and again, and again for taking your time to help me.

    Reputation added for you! You deserve it!

  28. #28
    Registered User
    Join Date
    09-28-2016
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    26

    Re: Loop for vba macro from a specific column to the last column with data

    I works perfectly!!!! Thank you again, and again, and again for taking your time to help me.

    Reputation added for you! You deserve it!

+ 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 through range to copy row if data exists in column, then delete the column
    By wpryan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-01-2015, 05:03 PM
  2. [SOLVED] Loop through Column B for Specific value
    By GFOREMAN in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-10-2014, 08:42 PM
  3. [SOLVED] Copy/Paste Macro Loop For A column of Data
    By brooksbil in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-31-2012, 02:31 PM
  4. Replies: 6
    Last Post: 02-28-2012, 07:49 AM
  5. Loop Macro to Find Minimum Date for Column Data
    By abpexcelforum in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-05-2011, 05:03 PM
  6. Replies: 1
    Last Post: 05-06-2011, 03:08 AM
  7. Macro Loop to Extract Specific Values and Min/Max Values From Column/Rows Range
    By ExcelQuestFL in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 12-19-2010, 09:19 AM

Tags for this Thread

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