Consulting

Results 1 to 8 of 8

Thread: Help in modifying a macro

  1. #1
    VBAX Regular
    Joined
    Sep 2004
    Location
    Sydney, Australia
    Posts
    34

    Help in modifying a macro

    Hi
    I have the following macro that adds rows between existing row values. How do i change it so that it repeats the value of the cell above the inserted row.
    Eg, If A6=Nails; A7=Screws, A8= Bolts and if i insert 5 rows between each of them, i want rows A7-A11 to repeat the name Nails and A12-A17 = Screws ...and so on.
    Sub xxxx()
    	Dim myRange As Range
    	Dim c As Range
     
    	Set myRange = Range("A6:A65536")
    	x = InputBox("Enter Number ") '3
     
    	i = Application.WorksheetFunction.CountA(myRange)
    	For Each c In myRange
    		If c.Value <> "" Then
    			Range(c.Offset(1, 0), c.Offset(x, 1)).EntireRow.Insert
    		End If
    	Next c
    End Sub
    Any help will be appreciated
    Thanks
    Suju


    Land Down-Under... Over the Hill and Farrrr Away.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [VBA]
    Sub xxxx()
    Dim myRange As Range
    Dim c As Range
    Dim iLastRow As Long
    Dim i As Long
    Dim x

    iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    x = InputBox("Enter Number") '3

    For i = iLastRow To 6 Step -1
    If Cells(i, "A").Value <> "" Then
    Rows(i + 1).Resize(x).Insert
    Cells(i, "A").AutoFill Cells(i, "A").Resize(x + 1)
    End If
    Next i
    End Sub[/VBA]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Sep 2004
    Location
    Sydney, Australia
    Posts
    34
    Thanx XLD.
    The code runs faster than before. One small question:
    When i filter data down into the adjacent colmns, i notice that they only filter down to the end of the first group..thats is from A6-A11 (Nails)....and not not all the way down the colmn. Why is that. The old code did not affect the corresponding colmns.

    Thanx again
    Suju


    Land Down-Under... Over the Hill and Farrrr Away.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by SUJU
    Thanx XLD.
    The code runs faster than before. One small question:
    When i filter data down into the adjacent colmns, i notice that they only filter down to the end of the first group..thats is from A6-A11 (Nails)....and not not all the way down the colmn. Why is that. The old code did not affect the corresponding colmns.
    I am not sure that I understand what you mean.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Sep 2004
    Location
    Sydney, Australia
    Posts
    34
    Adjacent to each value in Col A, dates and qty is filtered into the workbook from an external source. With the old code, the dates and qties were filtering in without a problem. However, with your code, the dates and qties only filter up the end of the group of names (Nails, in this case). I cant seem to understand why the change in your code would interfere with the adjacent columns.

    HTH
    Suju


    Land Down-Under... Over the Hill and Farrrr Away.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Can you post your workbook?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    It's because of this line ...

    [vba]Cells(i, "A").AutoFill Cells(i, "A").Resize(x + 1)[/vba]

    It does not take into account any other columns except that of A. As an example, pick this apart ...

    [vba]Option Explicit

    Sub AdjustColsToo()
    Dim c As Range, r As Range
    Dim i As Long, x As Long
    Set r = Cells(i, "A")
    Set c = r.Resize(x + 1, 2)
    r.AutoFill c
    End Sub[/vba]

  8. #8
    VBAX Regular
    Joined
    Sep 2004
    Location
    Sydney, Australia
    Posts
    34

    Solved: Modifying a macro

    Thanx Fireftr and Xld.
    Phew!! that really helps
    You guys are geniuses...take a bow
    Suju


    Land Down-Under... Over the Hill and Farrrr Away.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •