Solved

Copy a row

Posted on 2016-10-20
12
52 Views
Last Modified: 2016-10-24
I need to be able to copy a row from sheet 1 to sheet 2 and remove the row from sheet 1. If a cell in AB is true the entire row needs to be copied once a button is pressed.
0
Comment
Question by:Sam Coombes
  • 6
  • 6
12 Comments
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41852897
Please try this.....
Sub CopyRow()
Dim sws As Worksheet, dws As Worksheet
Dim lr As Long
Application.ScreenUpdating = False
Set sws = Sheets("Sheet1")
Set dws = Sheets("Sheet2")
lr = sws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
With sws.Range("AB1:AB" & lr)
   .AutoFilter field:=1, Criteria1:="True"
   If .SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then
      sws.Range("AB2:AB" & lr).SpecialCells(xlCellTypeVisible).EntireRow.Copy dws.Range("A" & Rows.Count).End(3)(2)
      sws.Range("AB2:AB" & lr).SpecialCells(xlCellTypeVisible).EntireRow.Delete
   End If
   .AutoFilter
End With
Application.ScreenUpdating = True
End Sub

Open in new window

0
 

Author Comment

by:Sam Coombes
ID: 41853353
That's great but I am confused why there is an error with line 11.

Also I have renamed sheet one blueteq

Any ideas
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41853397
What error you get?
It's hard to tell unless you upload a sample workbook. I assume sheet2 doesn't contain merged cells.
0
 

Author Comment

by:Sam Coombes
ID: 41853521
Hi
That's no problem I have attached a copy of the spreadsheet.
Thank you so much for your assistance.
0
 

Author Comment

by:Sam Coombes
ID: 41853522
Sorry here its is
Blueteqemail-v1.xlsm
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41853535
Okay try this....
Sub CopyRow()
Dim sws As Worksheet, dws As Worksheet
Dim lr As Long
Application.EnableEvents = False
Application.ScreenUpdating = False
Set sws = Sheets("Blueteq")
Set dws = Sheets("Sheet2")
lr = sws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
With sws.Range("AB5:AB" & lr)
   .AutoFilter field:=1, Criteria1:="True"
   If .SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then
      sws.Range("AB6:AB" & lr).SpecialCells(xlCellTypeVisible).EntireRow.Copy dws.Range("A" & Rows.Count).End(3)(2)
      sws.Range("AB6:AB" & lr).SpecialCells(xlCellTypeVisible).EntireRow.Delete
   End If
   .AutoFilter
End With
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub

Open in new window

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41853544
Either you clear the filter from Row4 before running the code or try the tweaked code...
Sub CopyRow()
Dim sws As Worksheet, dws As Worksheet
Dim lr As Long
Application.EnableEvents = False
Application.ScreenUpdating = False
Set sws = Sheets("Blueteq")
Set dws = Sheets("Sheet2")
lr = sws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
sws.AutoFilterMode = False
With sws.Range("AB5:AB" & lr)
   .AutoFilter field:=1, Criteria1:="True"
   If .SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then
      sws.Range("AB6:AB" & lr).SpecialCells(xlCellTypeVisible).EntireRow.Copy dws.Range("A" & Rows.Count).End(3)(2)
      sws.Range("AB6:AB" & lr).SpecialCells(xlCellTypeVisible).EntireRow.Delete
   End If
   .AutoFilter
End With
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub

Open in new window

1
 

Author Comment

by:Sam Coombes
ID: 41853571
That's fantastic thank you
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41853577
You're welcome Sam! Glad to help.
1
 

Author Comment

by:Sam Coombes
ID: 41854056
Great solution but it is causing Excel to crash any ideas ?
0
 
LVL 28

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41854093
This code has nothing that would cause excel to crash by any means for sure.
Either your file may be corrupt or since you have already have few event codes that might be doing some damage to the formatting etc.
Just copy the data from the current workbook (values only not the formatting etc) to another workbook and run this code as many times as you wish. I am sure that won't cause any problem.
1
 

Author Closing Comment

by:Sam Coombes
ID: 41858035
Fantastic thank you
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now