This question already has answers here:
Excel VBA Delete Rows
(1 answer)
Faster way to delete rows 40k+ rows at once
(2 answers)
Closed 3 years ago.
The below code only seems to work on the first result. I have to re-run it for the other results to be removed. Could anyone take a look and tell me why please? Thanks
I've tried copy and pasting the code several times to compensate but it gives me an error about duplicate code
Dim cell As Range
For Each cell In [AE1:AE2000]
If cell.Value = "REMOVE" Then Range(cell.Offset(0, -5).Address & ":" & cell.Offset(0, 0).Address).Delete Shift:=xlUp
Next cell
One thing you must do when deleting rows is loop backwards; otherwise you may skip rows, which is what is happening to you by the sounds of it. You have to use a counter when looping backwards, such as
Sub x()
Dim r As Long, cell As Range
Set cell = Range("AE1:AE2000")
For r = cell.Count To 1 Step -1
If UCase(cell(r).Value) = "REMOVE" Then
Range(cell(r).Offset(0, -5), cell(r)).Delete Shift:=xlUp
End If
Next r
End Sub
Related
This question already has answers here:
Excel VBA, getting range from an inactive sheet
(3 answers)
Why does Range work, but not Cells?
(1 answer)
Closed 1 year ago.
I need to filter some data based on duration (column) and copy the ids (other column) of the result to another sheet.
I have tried the following, but I always get 1 as the amount of filtered rows (actually it's over 100) and an error on the copy attempt:
dataSheet.UsedRange.AutoFilter Field:=durationColIndex, Criteria1:="<100"
Dim quickRange As Range, quickCount As Long
Set quickRange = dataSheet.UsedRange.SpecialCells(xlCellTypeVisible)
quickCount = quickRange.Rows.Count '<----- does not work, is always 1
If quickCount > 0 Then
Set specialIdsSheet = Sheets.Add 'defined before
'copy col 1
quickRange.Range(Cells(1, idIndex), Cells(25, idIndex)).Copy '<-- other issue. 25 to substitue the not working quickCount
specialIdsSheet.Cells(1, 1).PasteSpecial
End If
I have tried to work around it for some hours now, but I stuck and don't understand why it is not working.
I had a solution before that was using ActiveSheet and Select, but I wanted to move away from that.
ActiveSheet.Range("A1").CurrentRegion.Range(Cells(1, idIndex), Cells(ActiveCell.CurrentRegion.Rows.Count, idIndex)).Select
Selection.Copy
Sheets.Add After:=ActiveSheet
Thanks in advance!
This question already has answers here:
Copy Non Blank Cells From Range to Range
(2 answers)
Closed 2 years ago.
I am having difficulty having this macro copy and paste only the cells that have data in them. I would like the macro to only select and copy the cells in the columns that have data, ignore the empty cells. what I currently have is this
Sub testest()
Sheet4.Activate
'Dim lrow As Long
'lrow = Sheet4.Range("A", Rows.Count).End(xlUp).Row
Sheet4.Range("A2:A40").Select
Selection.Copy
Sheet5.Activate
Sheet5.Range("A2").PasteSpecial
Sheet4.Activate
Sheet4.Range("B2:B40").Select
Selection.Copy
Sheet5.Activate
Sheet5.Range("B2").PasteSpecial
End Sub
If you want them to paste only the cells with data by shifting the other cells up to fill in the blank ones, you should try the following commands after you select your desired copy range:
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Delete Shift:=xlUp
This logic mirrors going to "Find and Select" > "Blanks" > "Delete"
This is not an answer to your question, merely a way to show you how to reduce your code, by eliminating the obsolete activation and selection actions:
Sub testest()
Sheet4.Range("A2:A40").Copy
Sheet5.Range("A2").PasteSpecial
Sheet4.Range("B2:B40").Copy
Sheet5.Range("B2").PasteSpecial
End Sub
In fact, why don't you simply do this:
Sheet4.Range("A2:B40").Copy
Sheet5.Range("A2").PasteSpecial
(Ranges can go over different columns)
There are ways to avoid the usage of the clipboard, but then the complexity of the macro increases, which, I believe, is not what you're looking for.
Similar to the answer by #Dominique, but addressing the empty cells and assuming you gave up on the last row method:
Sub testest()
Dim lrow As Long
lrow = Sheet4.Range("A", Rows.Count).End(xlUp).Row
Sheet4.Range("A2:A" & lrow).SpecialCells(xlCellTypeVisible).Copy
Sheet5.Range("A2").PasteSpecial
Sheet4.Range("B2:B" & lrow).SpecialCells(xlCellTypeVisible).Copy
Sheet5.Range("B2").PasteSpecial
End Sub
I'm not very good at loops.
I'm trying to use VBA to loop through a column to look for any value, and then delete the entire row if it doesn't find anything (It's essentially a way of deleting rows of data that I've marked (or unmarked in this case)).
I've tried various things. My most recent attempt is below but its just deleting every row regardless of whether that cell has a value or not. Any suggestions?
Dim i as Long
For i = 1 To 50
If Cells(i, 1).Value = "" Then
Selection.EntireRow.Delete
Else
i = i + 1
End If
Next i
End Sub
There are several issues here:
When deleting rows in a loop work backwards, if going forward your row number changes as you delete.
There is no need to increment variable "i" next i already does this
Use the Worksheet object to delete the row rather than Selection
I would rewrite like this:
Sub delete()
Dim i As Long
For i = 50 To 1 Step -1
If Cells(i, 1).Value = "" Then
Worksheets("Sheet1").Rows(i).EntireRow.delete
End If
Next i
End Sub
This question already has answers here:
Add line breaks in cell to 1000 rows of data
(3 answers)
Closed 6 years ago.
I am naïve to macros and I need a macro button to enter line break at the end of every data in the row. I have around 1000 rows of data in specific columns. I need to apply this to selected columns. I am currently using ALT+ENTER, but it is time consuming.
Any help would be much appreciated.
I am currently using below code
Sub Macro
Dim Stem As Variant
Stem = ThisWorkbook.Worksheets ("Sheet1").Range("C2")
Range ("K2").Select
Range("K2").FormulaR1C1 = Stem & Chr(10) & ""
End Sub
Above code copies only C2 data and paste in K2 and apply formula. But I need all data in column C2:C to be copied and pasted in K2:K.
Thanks
If I understand that correctly, you want to loop through the copied records in column K and add a line break?
In that case you can use this (this will copy all columns starting from 1) and loop through cells in K starting from 2, you can change that if needed:
Sub copyAndNewLine()
'copy column C to K
Columns("C").Copy Destination:=Columns("K")
'loop through all cells in K and add new line
For i = 2 To Cells(Rows.Count, "K").End(xlUp).Row
Cells(i, "K").Value = Cells(i, "K").Value & vbCrLf
Next i
End Sub
This question already has answers here:
Find last used cell in Excel VBA
(14 answers)
Closed 6 years ago.
Follow on from earlier question. I am trying to count the number of rows in a table which contain data (not counting number of rows which are part of the table).
For example, my table is:
My code is:
With ThisWorkbook.Worksheets(1)
Set ATB = .ListObjects("Table1")
.Activate
numberRows = .Cells(.Rows.Count, "A").End(xlUp).Row
End With`
This returns the wrong number (trust me that column A has the same data count)
Likewise, I want to use VBA to resize the table nicely but need this row count for that.
Try this
Sub check()
With ThisWorkbook.Worksheets("Sheet1")
Set ATB = .ListObjects("Table1")
.Activate
numberRows = Application.CountA(.Range("A:A"))
End With
End Sub
Try this excel formula
=COUNTA(Table1[A1])