I have some reports that need to be modified by deleting some specific cells such as blank cells or highlights in the background
I have tried to record Macro to delete the special cells. however, the position will be changed regarding the difference of the row numbers. I could not specify the certain position of each cell.
Here is my data
here is what I expect to get
Here is my recorded Macro
Sub Macro1()
'Macro1 Macro
Range("B4").Select
Selection.Delete Shift:=xlToLeft
Range("B16").Select
Selection.Delete Shift:=xlUp
Range("B24").Select
Selection.Delete Shift:=xlToLeft
ActiveWindow.SmallScroll Down:=12
Range("B30").Select
Selection.Delete Shift:=xlToLeft
End Sub
You can try the below, change the columns you want to delete
you can change criteria by using xlcelltype XXXX
for xample of xlcelltype
Dim b As Range
For Each b In [B1:B2000].SpecialCells(xlCellTypeBlanks, 1).Offset(0, -1).Areas
b.Delete Shift:=xlToLeft
Next
Cells.Select
Cells.EntireColumn.Autofit
End Sub
Go in advance to proper click on chosen cells and pick the Delete from the right-clicking menu. And then take a look at the Entire row choice in the popping up Delete dialog box, and click on the OK button. Now you will see all the cells containing the sure fee are removed.
Related
I am trying to create a macro that will filter data in tables, regardless of their table name. For example:
Sub Macro4()
Macro4 Macro
ActiveSheet.ListObjects("Table2").Range.AutoFilter Field:=3, Criteria1:= _
">5", Operator:=xlAnd
Columns("D:D").Select
Selection.Delete Shift:=xlToLeft
Range("B3").Select
End Sub
This is a very simple code that filters a column of a table by a criteria. The code reads "Table2" so when I run it for another table with a different name, it gives me an error. How can I code it so it will filter any table regardless of its name?
Note that all my tables always have the same headers and amount of columns.
Okay: turns out I only need to change "Table1" for a 1. So that line would be ActiveSheet.ListObjects(1)...
Is there a way to have that as default for my next Macro recordings?
As long as you only have 1 table per sheet, you can use this. It uses a count of the objects in the sheet and uses the first one, rather than the explicit named reference which doesn't work on other tables.
Sub Macro4()
Macro4 Macro
ActiveSheet.ListObjects(1).Range.AutoFilter Field:=3, Criteria1:= _
">5", Operator:=xlAnd
Columns("D:D").Select
Selection.Delete Shift:=xlToLeft
Range("B3").Select
End Sub
One improvement I would make is that you can avoid this section:
Columns("D:D").Select
Selection.Delete Shift:=xlToLeft
Range("B3").Select
by switching it to this, since the selections aren't really doing anything
Columns("D:D").Delete Shift:=xlToLeft
I am working on a spreadsheet where I recorded macros to fill a formula into a column in excel, but it does not fill every single cell--there are certain cells I skipped over because they are either blank or a subtotal within the column. Will it be possible to delete some rows without messing up the relative references used to record the macro?
I am hoping to be able to easily edit the code somehow, or maybe there is something I can put into the code to edit itself? The purpose of this spreadsheet is to avoid a lot of manual work, so having to pick through excel code and edit out the deleted rows would defeat the purpose of macros in the first place.
piece of VBA code:
ActiveCell.Offset(0, -5).Columns("A:E").EntireColumn.Select
Selection.EntireColumn.Hidden = False
ActiveCell.Offset(0, 2).Columns("A:B").EntireColumn.Select
Selection.EntireColumn.Hidden = True
ActiveCell.Offset(7, 3).Range("A1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-5]-RC[-1]"
ActiveCell.Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A12"), Type:= _
xlFillDefault
ActiveCell.Range("A1:A12").Select
ActiveWindow.SmallScroll Down:=3
ActiveCell.Offset(11, 0).Range("A1").Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A4"), Type:= _
xlFillDefault
ActiveCell.Range("A1:A4").Select
ActiveWindow.SmallScroll Down:=6
ActiveCell.Offset(3, 0).Range("A1").Select
my layout:
The subtotals and empty cells need to be skipped. Each subtotal is a sum of the cells above it, and all the other cells have a simple =A1-B1, etc.
My recorded macro is meant to fill in the =A1-B1 down the cells on a certain day, and another day a similar macro will change the formulas to =B1-C1, and so on. Is there a way to make a macro change the formula based on what formula is already in the cell?
there are lot of people that have asked and answered a similar question but none of the suggestions are working for me. I have a sheet where the data ends in column 'S'. However, when I go to print it the preview shows an extra 8 columns on the right. When I apply a filter these same columns also get the filter added, even though they are empty (I checked if the cells were empty with =ISBLANK() and it returned true). If I highlight and delete the 8 columns, there is no difference in the print preview, however it does delete the filter.
I think I have just figured out why this is happening but I still do not know how to fix it. The data in the sheet that is causing the problems is copied from another workbook. The original data has an extra 8 columns within it. But I run a macro which formats everything and deletes these columns as they are either blank or unneeded.
So I think for some reason when I delete the 8 columns, 8 columns at the right most side of the sheet are added?
Does anyone know how I can fix this? Or what I should change to stop this happening.
Heres a snippet of one of the macros. This is the only piece of code that is deleting any columns.
Range("C:C").Select
Selection.Delete Shift:=xlToLeft
Range("G:G").Select
Selection.Delete Shift:=xlToLeft
Range("H:H").Select
Selection.Delete Shift:=xlToLeft
Range("K:L").Select
Selection.Delete Shift:=xlToLeft
Range("L:M").Select
Selection.Delete Shift:=xlToLeft
Range("O:O").Select
Selection.Delete Shift:=xlToLeft
Range("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "Crew"
Range("A1").Select
ActiveCell.FormulaR1C1 = "Yes/ No"
Range("G1").Select
ActiveCell.FormulaR1C1 = "RPC"
Range("H1").Select
ActiveCell.FormulaR1C1 = "SCE"
Range("M1").Select
ActiveCell.FormulaR1C1 = "Craft"
Range("O1").Select
ActiveCell.FormulaR1C1 = "Frequency"
Range("P1").Select
ActiveCell.FormulaR1C1 = "Unit"
Range("S1").Select
ActiveCell.FormulaR1C1 = "Duration"
See related
Excel resetting “UsedRange” and Excel VBA usedRange Property and reset usedRange
I seem to recall that the last cell in the UsedRange is cached. Meaning if you had typed something in column X (furthest right column in use), went about your business, then later cleared column X... then the UsedRange still goes out to column X despite there not being any filled in cells. You'd have to either Save the file, or run some VBA against the UsedRange in order to clear the cached setting.
I basically solved or did a work around for this. I wrote a piece of code that can go at the end of another macro or can be used as its own macro. It just sets the print area to be whatever size the used range is.
Sub printPrep()
Dim rng As Range
Set rng = ActiveSheet.UsedRange
With ActiveSheet
.PageSetup.PrintArea = .UsedRange.Address
Debug.Print (rng.Address)
End With
End Sub
I am using a macro that creates a copy of a worksheet then renames it, deletes some columns and inserts a formula into a cell and then drags it down.
The part that drags it down does not work!
Sub filterData()
'
' filterData Macro
' Filter data
'
' Keyboard Shortcut: Ctrl+m
'
Sheets("devices").Select
Sheets("devices").Copy After:=Sheets(1)
Sheets("devices (2)").Select
Sheets("devices (2)").Name = "filterData"
ActiveWorkbook.Save
Columns("G:J").Select
Selection.Delete Shift:=xlToLeft
Columns("H:AA").Select
Selection.Delete Shift:=xlToLeft
ActiveWorkbook.Save
Range("G2").Select
ActiveCell.FormulaR1C1 = _
"=LEFT(devices!RC[4],IFERROR(SEARCH("""""""",devices!RC[4]),SEARCH(""-"",devices!RC[4]))-1)"
ActiveWorkbook.Save
Range("G2").Select
Selection.AutoFill Destination:=Range("Table122[Display]")
Range("Table122[Display]").Select
End Sub
As you can see, the part that drags the formula down to last populated cell is not working!!!
Update
I have added the following to the code as well and it still does not work;
lastrow = Range("G2").End(xlUp).Row --new line
Range("G2").Select
ActiveCell.FormulaR1C1 = _
"=LEFT(devices!RC[4],IFERROR(SEARCH("""""""",devices!RC[4]),SEARCH(""-"",devices!RC[4]))-1)"
Range("G2").Select
Selection.AutoFill Destination:=Range("G2:G" & lastrow) --new line
Since it's a Table (VBA ListObject) you can fill in the whole ListColumn at once:
ActiveCell.ListObject.ListColumns("Display").DataBodyRange.Formula = _
"=LEFT(devices!RC[4],IFERROR(SEARCH("""""""",devices!RC[4]),SEARCH(""-"",devices!RC[4]))-1)"
Also, note that you can get rid of the Select statements generated by the Macro Recorder. For example:
Columns("G:J").Select
Selection.Delete Shift:=xlToLeft
can be just:
Columns("G:J").Delete Shift:=xlToLeft
And one final tip is to fully qualify ranges, e.g.:
Worksheets("filterData").Columns("G:J").Delete Shift:=xlToLeft
I'll stop now since you already accepted this answer. Except to say I do like the animation!
The problem with your macro is probably this line:
Range("Table122[Display]").Select
You are trying to auto-fill the Display column, including the Header row, which it probably doesn't like. Try it like this instead:
Range("Table122[[#Data],[Display]]").Select
For a simple solution to macro for dragging formula down to dynamic row count:
You can also use the previous column for a reference of how many rows you want to drag the formula.
Generic: Range("B2:B" & Range("A65000").End(xlUp).Row).FormulaR1C1 = "{=formulahere}"
So in your case it would look something like:
Range("G2:G" & Range("F65000").End(xlUp).Row).FormulaR1C1 = "=LEFT(devices!RC[4],IFERROR(SEARCH("""""""",devices!RC[4]),SEARCH(""-"",devices!RC[4]))-1)"
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to make a VBA macro that searches through column A and every time it finds 2 cells in that column that are the same (and adjacent!) to delete one of the 2 and shift up. I want it to stay in column A, and every time a duplicate came up that was adjacent to delete it.
Pretty much I have an excel sheet that alternates information like so:
A
B
A
B
But I often hit snags like this
A
B
A
B
B
A
Those 2 B's are what I want it to automatically clear and shift cells up on, but I am unsure how to go about it.
Any suggestions?
EDIT: I solved my own question. The answer is down below but here it is again to help others who may have come across the issue as I have.
Nevermind folks. I got this one. I recorded a macro that uses filters and a simple formula. In column B I put this in: =IF(a1=a2,"STOP","") Then I dragged it down to copy it. Then I filtered column B to show only "STOP" cells. Then I cleared out those cells. Then I unfiltered column B to show all. Then I went to Home>Find and Select>Special>Select Blanks Then I deleted and shifted cells up the selected blanks. Then I re dragged the B column formula down to make a working loop for my recorded macro. Heres what the recording ended up looking like:
Sub DUPES()
ActiveCell.FormulaR1C1 = "=IF(RC[-1]=R[1]C[-1],""DLT"","""")"
Range("J2").Select
Selection.AutoFill Destination:=Range("J2:J1000"), Type:=xlFillDefault
Range("J2:J1000").Select
ActiveSheet.Range("$A$1:$L$500").AutoFilter Field:=10, Criteria1:="<>"
Range("I11:J11").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("A11").Select
ActiveSheet.Range("$A$1:$L$500").AutoFilter Field:=10
ActiveWindow.SmallScroll Down:=-42
Columns("I:I").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Delete Shift:=xlUp
Range("J2").Select
Selection.AutoFill Destination:=Range("J2:J1000"), Type:=xlFillDefault
Range("J2:J1000").Select
Range("I2").Select
End Sub
sub removedup
Columns("A:B").Select
ActiveSheet.Range("$A$1:$B$8").RemoveDuplicates Columns:=1, Header:=xlYes
end sub
There is a remove duplicate button in excel 2007/10/13 in the ribbon - but as you want vba this assumes that you have column headers in a1 and b1
Nevermind folks. I got this one. I recorded a macro that uses filters and a simple formula.
In column B I put this in:
=IF(a1=a2,"STOP","")
Then I dragged it down to copy it.
Then I filtered column B to show only "STOP" cells.
Then I cleared out those cells.
Then I unfiltered column B to show all.
Then I went to Home>Find and Select>Special>Select Blanks
Then I deleted and shifted cells up the selected blanks.
Then I re dragged the B column formula down to make a working loop for my recorded macro.
Heres what the recording ended up looking like:
Sub DUPES()
ActiveCell.FormulaR1C1 = "=IF(RC[-1]=R[1]C[-1],""DLT"","""")"
Range("J2").Select
Selection.AutoFill Destination:=Range("J2:J1000"), Type:=xlFillDefault
Range("J2:J1000").Select
ActiveSheet.Range("$A$1:$L$500").AutoFilter Field:=10, Criteria1:="<>"
Range("I11:J11").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("A11").Select
ActiveSheet.Range("$A$1:$L$500").AutoFilter Field:=10
ActiveWindow.SmallScroll Down:=-42
Columns("I:I").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Delete Shift:=xlUp
Range("J2").Select
Selection.AutoFill Destination:=Range("J2:J1000"), Type:=xlFillDefault
Range("J2:J1000").Select
Range("I2").Select
End Sub