?
Solved

Excel Macro to loop thru code the finds cuts and pastes

Posted on 2015-01-08
6
Medium Priority
?
110 Views
Last Modified: 2015-01-09
I have a macro I am working on.  I have found a way do the find, cut and paste, but it does not loop,  It stops after the first  find, cut and paste.

When there are blanks in K there will be data data sitting in L that needs to be moved to P.  See the attachment please.
snip of excel sheet

Below is the code I used to find a blank K to then cut L to paste in P, but it only works for the first line.  I need it to loop through a variable length sheet.  A column that will be full of data until the end of the sheet if needed to reference an end is column C.  

Range("K:K").Select
 Selection.SpecialCells(xlCellTypeBlanks).Select
ActiveCell.Offset(0, 1).Select
    Selection.Cut
    ActiveCell.Offset(0, 4).Select
    ActiveSheet.Paste
0
Comment
Question by:jenit
[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 34

Expert Comment

by:Norie
ID: 40538854
Perhaps.
Dim rngBlanks As Range
Dim cl As Range

    Set rngBlanks = Range("K:K").SpecialCells(xlCellTypeBlanks).Select
    
    For Each cl In rngBlanks.Cells
        cl.Offset(0, 1).Cut cl.Offset(0, 5)
    Next cl

Open in new window

0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 40539603
Hi,

I would put a limit to the range

Dim rngBlanks As Range
Dim cl As Range

    Set rngBlanks = Range("K1:K"& Range("L"& Cells.Rows.count).End(xlUp).Row).SpecialCells(xlCellTypeBlanks)
    
    For Each cl In rngBlanks.Cells
        cl.Offset(0, 1).Cut cl.Offset(0, 5)
    Next cl

Open in new window

Regards
0
 
LVL 34

Expert Comment

by:Norie
ID: 40539628
There's no real need to add code to limit the range, when you use Special cells automatically does it.

For example, if you have data from K1:K10 and cells K3, K6 and K8 are blank this Range("K:K").SpecialCells(xlCellTypeBlanks) will only return K3, K6 and K8.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 40539698
Right I have been deceived by the file I used

But the select at the set instruction gives an error

then try

Dim rngBlanks As Range
Dim cl As Range

    Set rngBlanks = Range("K:K").SpecialCells(xlCellTypeBlanks)
   
    For Each cl In rngBlanks.Cells
        cl.Offset(0, 1).Cut cl.Offset(0, 5)
    Next cl
0
 
LVL 34

Accepted Solution

by:
Norie earned 2000 total points
ID: 40539713
Oops, that's a typo left over from the original code.:)

Should be this.
Dim rngBlanks As Range
Dim cl As Range

    Set rngBlanks = Range("K:K").SpecialCells(xlCellTypeBlanks)
    
    For Each cl In rngBlanks.Cells
        cl.Offset(0, 1).Cut cl.Offset(0, 5)
    Next cl

Open in new window

0
 

Author Closing Comment

by:jenit
ID: 40540103
Thanks!!!  That did the trick!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
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.

777 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