+ Reply to Thread
Results 1 to 10 of 10

MAcro To split a number dividing by 40

Hybrid View

  1. #1
    Registered User
    Join Date
    12-28-2012
    Location
    Chennai
    MS-Off Ver
    Excel 2003
    Posts
    13

    MAcro To split a number dividing by 40

    Hi All,

    I attached a sheet for your reference. Please check.

    I am trying to write a macro which will help to divide a whole number by 40 and than place the reminder in next rows kindly help for the same.

    Thanks,
    Vignesh kumar.S
    Attached Files Attached Files
    Last edited by vigneshkumar; 04-02-2013 at 12:53 AM.

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: MAcro To split a number dividing by 40

    Hi, vigneshkumar,

    give this code a try:
    Sub 911415()
    Dim lngCounter As Long
    Dim lngRow As Long
    Dim lngLastCol As Long
    Dim lngMod As Long
    Dim blnWrite As Boolean
    
    Const clngDATA As Long = 4
    Const clngDIV As Long = 40
    lngRow = clngDATA
    
    For lngCounter = 4 To Cells(clngDATA, Columns.Count).End(xlToLeft).Column
      lngMod = Int(Cells(clngDATA, lngCounter).Value / clngDIV)
      If lngMod > 0 Then
        lngRow = lngRow + 1
        With Cells(lngRow, lngCounter)
          .Value = Cells(clngDATA, lngCounter).Value Mod clngDIV
          .Offset(0, -lngMod).Resize(1, lngMod).Value = clngDIV
        End With
      End If
    Next lngCounter
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    12-28-2012
    Location
    Chennai
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: MAcro To split a number dividing by 40

    Hi Holger,

    Thanks a lot for your quick response.

    i will rephrase my question i am trying to split any number in the form of 40 for example take a number 90 this number i am trying to split as 40, 40, 10 or take a number 129 this number should be split into 40, 40, 40,9 like this, hope you got what I am looking for. Thanks for ur help,
    regards,
    Vignesh

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: MAcro To split a number dividing by 40

    Hi, Vignesh ,

    just copy sheet1, clear the rows under Row 4 and then run the macro.
    i will rephrase my question i am trying to split any number in the form of 40
    What does the macro do? Personally I wonīt let data start in the middle of nowhere but as this is done in the attachment the code takes care of the situation in Sheet1 and hopefully doubles up what I thought was the desired outcome.

    Ciao,
    Holger

  5. #5
    Registered User
    Join Date
    12-28-2012
    Location
    Chennai
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: MAcro To split a number dividing by 40

    Hi Holger,

    The macro should take care of splitting the particular number in 40s , for example 90 should get split into 40, 40 and the left out number 10 should be placed in next row. Thanks and if i am confusing you i'm really sorry

    Thanks,
    Vignesh

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Macro To split a number dividing by 40

    Hi, Vignesh,

    if i am confusing
    Whatīs the good of posting a workbook which should show the situation as is and as desired and then come back because you want something else than what is shown in the workbook? Even with what you tell right now itīs unclear to me what you want: all data in one row or all data cascading down under the original? Only show numbers if they are greater than 40 or show all rests?

    Regarding your attached workbook and the sample given there:
    Sub EF911415_2a()
    Dim lngCounter As Long
    Dim lngRow As Long
    Dim lngLastCol As Long
    Dim lngMod As Long
    Dim blnWrite As Boolean
    
    Const clngDATA As Long = 4
    Const clngDIV As Long = 40
    lngRow = clngDATA
    
    For lngCounter = 4 To Cells(clngDATA, Columns.Count).End(xlToLeft).Column
      lngMod = Int(Cells(clngDATA, lngCounter).Value / clngDIV)
      If lngMod > 0 Then
        lngRow = lngRow + 1
        Cells(lngRow, lngCounter).Value = Cells(clngDATA, lngCounter).Value Mod clngDIV
      End If
    Next lngCounter
    End Sub
    Sub EF911415_2b()
    Dim lngCounter As Long
    Dim lngRow As Long
    Dim lngLastCol As Long
    Dim lngMod As Long
    Dim blnWrite As Boolean
    
    Const clngDATA As Long = 4
    Const clngDIV As Long = 40
    lngRow = clngDATA + 1
    
    For lngCounter = 4 To Cells(clngDATA, Columns.Count).End(xlToLeft).Column
      lngMod = Int(Cells(clngDATA, lngCounter).Value / clngDIV)
      If lngMod > 0 Then
        Cells(lngRow, lngCounter).Value = Cells(clngDATA, lngCounter).Value Mod clngDIV
      End If
    Next lngCounter
    End Sub
    Sub EF911415_2c()
    Dim lngCounter As Long
    Dim lngRow As Long
    Dim lngLastCol As Long
    Dim lngMod As Long
    Dim blnWrite As Boolean
    
    Const clngDATA As Long = 4
    Const clngDIV As Long = 40
    lngRow = clngDATA + 1
    
    For lngCounter = 4 To Cells(clngDATA, Columns.Count).End(xlToLeft).Column
      If Cells(clngDATA, lngCounter).Value > 0 Then
        lngMod = Int(Cells(clngDATA, lngCounter).Value / clngDIV)
        Cells(lngRow, lngCounter).Value = Cells(clngDATA, lngCounter).Value Mod clngDIV
      End If
    Next lngCounter
    End Sub
    as I mentioned before keep a number 90 in one cell b6 then if i run the macro than it should automatically place in cell c4 40 in cell c5 40 and in cell c6 10
    No, as the attached workbook showed numbers in one row and the results underneath.

    Holger

  7. #7
    Registered User
    Join Date
    12-28-2012
    Location
    Chennai
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: MAcro To split a number dividing by 40

    Hi Holger,

    I need help only in the splitting and placing the numbers in a cell, as I mentioned before keep a number 90 in one cell b6 then if i run the macro than it should automatically place in cell c4 40 in cell c5 40 and in cell c6 10, hope it helps u to understand my problem.
    Thanks,
    VIgnesh

  8. #8
    Registered User
    Join Date
    12-28-2012
    Location
    Chennai
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: MAcro To split a number dividing by 40

    Hi HaHobe,

    Thanks a lot, I am able to get the required result, only thing is number 40 is not visible, I will try to resolve it on my own. Thanks for ur great help.

    Regards,
    Vignesh

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: MAcro To split a number dividing by 40

    Hi, Vignesh,

    the first macro code showed a way using Offset and Resize (although result was in a row, not a column).

    Ciao,
    Holger

  10. #10
    Registered User
    Join Date
    12-28-2012
    Location
    Chennai
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: MAcro To split a number dividing by 40

    Thanks for the great help excel guru

    You are really a expert.

+ 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