?
Solved

how to insert rows and copy cells in Excel VBA

Posted on 2011-02-22
6
Medium Priority
?
644 Views
Last Modified: 2012-05-11
Using VBA code, I need to go through a range of cells in column P, and where column P contains data, I need to check the quantity on that row (col H). If the quantity on that row is greater than 1.......

Here's the part I don't know how to do:.....

If the quantity on that row is greater than 1, Insert (quantity minus one) rows BELOW that row, AND copy down the data in cells I & J, AND make those cells in column Q yellow interior.

For example, if the code discovers that cell P20 has data and cell H20 = 4, then insert new rows 21, 22, and 23. Copy I20:J20 down to I23:J23. Make Q20:Q23 yellow interior. Then, the next cell in the range and repeat.

Thanks!
0
Comment
Question by:nbozzy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 24

Expert Comment

by:StephenJR
ID: 34953176
Could you post a sample workbook please?
0
 

Author Comment

by:nbozzy
ID: 34953278
EE-sample.xls
Attached. Thanks!
0
 
LVL 24

Accepted Solution

by:
broomee9 earned 1000 total points
ID: 34953434
Try this:
Option Explicit

Sub CheckQuantities()

    Dim i As Long
    Dim lastRow As Long
    Dim rowsToInsert As Integer
    Dim startRow As Long
    Dim endRow As Long
    
    lastRow = Range("P" & Rows.Count).End(xlUp).Row
    
    For i = 5 To lastRow
        Debug.Print i & " - " & lastRow
        If Cells(i, "P").Value <> "" Then
            If Cells(i, "H").Value <> 1 Then
                rowsToInsert = Cells(i, "H").Value - 1
                startRow = i
                endRow = startRow + rowsToInsert
                
                'Insert rows
                Rows(startRow + 1 & ":" & endRow).Insert Shift:=xlDown
                
                'Copy Data
                Range("I" & startRow & ":J" & startRow).Copy
                Range("I" & startRow & ":J" & endRow).PasteSpecial xlPasteAll
                
                'Highlight Column Q
                Range("Q" & startRow & ":Q" & endRow).Interior.ColorIndex = 6
                Range("Q" & startRow & ":Q" & endRow).Interior.Pattern = xlSolid
                
                'Reset i counter
                i = endRow
                lastRow = lastRow + rowsToInsert
            Else
            End If
        Else
        End If
    Next

End Sub

Open in new window

EE-sample.xls
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 24

Assisted Solution

by:StephenJR
StephenJR earned 1000 total points
ID: 34953452
If the qty is 1 do you still want yellow?
Sub x()

Dim r As Long

For r = Cells(Rows.Count, "G").End(xlUp).Row To 5 Step -1
    If Len(Cells(r, "P")) > 0 Then
        If Cells(r, "H") > 1 Then
            Cells(r + 1, "H").Resize(Cells(r, "H") - 1).EntireRow.Insert shift:=xlDown
            Cells(r + 1, "I").Resize(Cells(r, "H") - 1, 2).Value = Cells(r, "I").Resize(, 2).Value
            Cells(r, "Q").Resize(Cells(r, "H")).Interior.Color = vbYellow
        End If
    End If
Next r

End Sub

Open in new window

0
 

Author Comment

by:nbozzy
ID: 34953743
Thank you both! Both solutions worked, so I will split points.
StephenJR: yes, I do want yellow only if quantity is 1, but I can modify the code to accommodate that. ;)
Broomee9: your code did leave the copy "marquis" visible along with cells selected, but I know how to fix those things.

I appreciate the help and the new knowledge!
0
 

Author Closing Comment

by:nbozzy
ID: 34953750
I didn't choose a "Best Solution" because that depends upon the particular user. Thanks!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question