Solved

Excel Macro to loop thru code the finds cuts and pastes

Posted on 2015-01-08
6
105 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 51

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 51

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 500 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

734 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