delete cells but not rows - excel

I have an excel sheet where I am pulling data from other worksheets into separate columns per worksheet I pull from. I am trying to find any duplicates in any of my columns and delete all the duplicates (for example: if there are three of the code 12gb, I want to be left with one 12gb). I want this so that I can count how many unique values there are (automatically) and then populate a graph automatically. I have tried many different formulas to do this but I am thinking a VBA code is needed, however I have never used that coding before so I am not sure what to do.Below is an example of three columns of my excel sheet:(I wasn't able to post images/excel sheets)
12gb sdf vfg
22rg tttyhg dsf
dfg455 ggff df
fgfg fff vcs
4redd ccv dfgh
56ff 66hg 66y
yygf 66y 56ff
66ygt yggfg 12gb
ghhg
vfg

This is a hackish macro it relies on specific columns and placements so I'm not a big fan of this solution.
It was generated by recording the manual tasks I did
adding a row to denote end of columns
copying the columns 2 and 3 to the end of column 1.
removing the duplicates in column a
then manually creating the 3 columns again. based on the end of column notes added.
.
Range("A1").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlUp).Select
Range("A12").Select
ActiveCell.FormulaR1C1 = "1----"
Range("B12").Select
ActiveCell.FormulaR1C1 = "2----"
Range("C12").Select
ActiveCell.FormulaR1C1 = "3----"
Range("B1").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Cut
Selection.End(xlToLeft).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Range("A13").Select
ActiveSheet.Paste
Range("B13").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Cut
Range("A13").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Range("A25").Select
ActiveSheet.Paste
Columns("A:A").Select
ActiveSheet.Range("$A$1:$A$46").RemoveDuplicates Columns:=1, Header:=xlNo
Range("A13:A22").Select
Selection.Cut
Range("B1").Select
ActiveSheet.Paste
Range("A23").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Cut
Range("C1").Select
ActiveSheet.Paste
Range("B13").Select
Now this assumes Row data isn't relevant. If it is... this will not work. as Excel deletes the duplicates.

you could do something like this, although i'm sure there are more elegant ways. in principle i just put each value into an array after checking if the value is not already in the array:
Sub del_values()
Dim last_row, last_col, size, y As Integer
Dim arr() As Variant
'finding the last row and last column and calculating the possible size of the array
last_row = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
last_col = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column
size = last_row * last_col
y = 0
'adjusting the array-size
ReDim arr(size)
For Each cell In ActiveSheet.Range(Cells(1, 1), Cells(last_row, last_col))
'avoiding blank cells
If cell.Value > "" Then
'checking if value is already in the array - if not, add it to the array and increase y - if it is, clear the contents of the cell
If IsError(Application.Match(cell.Value, arr, 0)) Then
arr(y) = cell.Value
y = y + 1
Else
Cells(cell.Row, cell.Column).ClearContents
End If
End If
Next
End Sub
i was not sure what you wanted to do with the cells containing duplicates, so i just cleared the contents - you could of course just delete them with
Cells(cell.Row, cell.Column).delete
by the way, y is equal to the number of unique values, if you want to use it directly.

Related

Excel 2013 VBA - fill a range of cells with data from another worksheet related to a certain key

my problem described best with an example.
I got a worksheet like the following
I want to fill the empty cells at the bottom automatically like a drill down feature based on the data in another worksheet which looks like the following
The key value to the data from the second worksheet is the project code. So we have to loop over the data in the second sheet, fetch row by row and insert into first worksheet starting in row 5 just beneath "Task Code".
Unfortunately I found no solutions anywhere or my search was not exact enough and the topmost answers did not match my needs.
Thanks in advance for any suggestions
you can use this VBA code below and assign the macro to a button:
First Worksheet Sample
Second worksheet Sample
Sheets("Sheet1").Select
Range("A5:C5").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Dim searchValue As Integer
searchValue = Cells(2, 1).Value
Range("A2").Select
Selection.Copy
Sheets("Sheet2").Select
ActiveSheet.Range("A:A").AutoFilter Field:=1, Criteria1:=searchValue
Range("B2:D2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("A5").Select
ActiveSheet.Paste
Sheets("Sheet2").Select
ActiveSheet.Range("$A$1:$D$7").AutoFilter Field:=1
Range("A10").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = ""
Range("A11").Select
Sheets("Sheet1").Select
Range("A2").Select

Excel 2013 VBA select cell P for every row that has data in column A

Working on a function to put the filename in a specific column (P) of a file. I've got this running if I specify the cells to put the filename in (e.g. P1:P5).
However, I want to get this to run in the P column, but for all rows that have data in the A column.
I know I could do it for just the whole P column, but i dont want it to run on empty rows (they're of no use)
Code I have so far:
Sub Save_files()
Cells.Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Rows("1:3").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("P2").Select
ActiveCell.FormulaR1C1 = "=CELL(""filename"")"
Range("P2").Select
Selection.Copy
Range("p1:p5").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("D4").Select
End Sub
I want to replace Range("p1:p5").Select with something that selects every P cell that is on a row with data in A of the same row.
Things to note:
Column A will always have data
Columns B through to O may or may not have data
Thanks in advance!
I changed:
Range("p1:p5").Select
to:
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range("p1:p" & LastRow).Select

VBA Macro: Paste underneath last cell in column with variable range

Title isn't the best so here a an overview.
I'm using VBA to copy select columns from one workbook to another, as what will be part of a larger automated program.
On the Workbook I am copying from, there are different sheets containing a "Stock Number" column. When pasting into my other workbook, I am trying to get these columns to merge into 1 single column (pasting below the last entry from the first sheet and so on).
Here is my current code:
Sub import_adam_article()
Windows( _
"copyfrom.xlsx" _
).Activate
Columns("F:G").Select
Selection.Copy
Windows("pasteinto.xlsx").Activate
Columns("A:A").Select
ActiveSheet.Paste
Windows( _
"copyfrom.xlsx" _
).Activate
Columns("N:N").Select
Application.CutCopyMode = False
Selection.Copy
Windows("pasteinto.xlsx").Activate
Columns("C:C").Select
ActiveSheet.Paste
Rows("1:1").Select
Selection.Delete Shift:=xlUp
NextRow = Range("A1").End(xlDown).Row + 1
Windows( _
"copyfrom.xlsx" _
).Activate
Columns("F:G").Select
Selection.Copy
Windows("pasteinto.xlsx").Activate
Range("A" & (NextRow)).Select
ActiveSheet.Paste
[A:C].Select
With Selection
.NumberFormat = "General"
.Value = .Value
End With
End Sub
The difficulty is that the amount of Stock Numbers will change every new file that comes through, so it needs to be able to adjust to varying amounts.
I can't seem to find a way to make it work and I've tried searching for answers elsewhere.
EDIT: The current issue with the code that it is selecting the next empty row to paste into, but only that cell, not a variable length down as required by the copyfrom column.

Run formula for changing values from positive to negative numbers and then auto-fill down to end of data

Every Monday i run a report of credit memos from our vendor for us to take. The list length changes every time i run this report. The report gives me the dollar amounts as positive numbers even though they are credit memos and should be a negative. After formatting the sheet, my values fall into Columns G,H,I,J. I need to change these to a negative value. I have been manually doing it and would like to use a macro to help me through this part.
In columns L-O i would use the following: In L2 = G2*-1 ; M2 = H2*-1; N2=I2*-1; O2=J2*-1. Then i would autofill down the bottom of data. Copy it, paste values over my columns G-J and then delete my formula sections L-O. What is the best macro to use for this?
Record Macro stops at the bottom row of current list but then the next monday, it will only stop at that same row (for example 495) when my new data may go to row 721.
ActiveCell.Offset(1, 11).Range("A1").Select
ActiveCell.FormulaR1C1 = "=RC[-5]*-1"
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "=RC[-5]*-1"
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "=RC[-5]*-1"
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "=RC[-5]*-1"
ActiveCell.Offset(0, -3).Range("A1:D1").Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:D1450")
ActiveCell.Range("A1:D1450").Select
Selection.Copy
ActiveCell.Offset(0, -5).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, 5).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.ClearContents
ActiveCell.Offset(0, -5).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Style = "Comma"
For someone with no pressing need for VBA, the easy way is to enter -1 in a spare cell, copy it, select the relevant range and Paste Special Multiply.
Try this macro
Option Explicit
Sub Multiplication_by_number()
Dim my_num#: my_num = -1 ' You can Change this number
[e1] = my_num ' You can Change this Location to any Other range
Dim My_rg As Range
[e1].Copy
Set My_rg = Range("A1:C8") 'choose here your data
My_rg.PasteSpecial , Operation:=xlMultiply
Application.CutCopyMode = False
My_rg.Cells(1).Select
End Sub

Copying formulas to last row of data

I can see the question has been asked a couple of times in the forum, but as someone who doesn't really know macro, I cannot implement the code provided in the answers.
So I have recorded the below macro.
Macro6 Macro
'
'
Range("C2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX('Account codes'!C[-1],MATCH(DATA!RC[4],'Account codes'!C[-2],0))"
Range("C2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX('Account codes'!C2,MATCH(DATA!RC[4],'Account codes'!C1,0))"
Range("C2").Select
Selection.Copy
Range("E2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = _
"=INDEX('Account codes'!C2,MATCH(DATA!RC[4],'Account codes'!C1,0))"
Range("E2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX('Account codes'!C2,MATCH(DATA!RC[3],'Account codes'!C1,0))"
Range("E2").Select
Selection.AutoFill Destination:=Range("E2:E4")
Range("E2:E4").Select
Range("C2").Select
Selection.AutoFill Destination:=Range("C2:C4")
Range("C2:C4").Select
End Sub
In simple terms, I want Selection.AutoFill Destination:=Range("E2:E4") to fill down to the last row of data, and not just to E4. That is because the data can change in size.
How do I do this?
You seem to be overwriting some of the formulas with different relative/absolute references. It really doesn't seem to matter which is used as far as relative/absolute addressing is concerned but you have two distinct formulas for column E which reference two different cells on the data worksheet. I'll use the latter of the two.
Put all of the formulas into all of the cells at once. AutoFill is overrated and functionally redundant in this case.
The formulas for column C seem to be using a relative reference to a cell in column F. You can use that column to determine the last row to put formulas into.
sub buildFormulas()
dim lr as long
with worksheets("data")
lr = .cells(.rows.count, "F").end(xlup).row
.range(.cells(2, "C"), .cells(lr, "C")).formular1c1 = _
"=INDEX('Account codes'!C2, MATCH(RC7, 'Account codes'!C1, 0))"
.range(.cells(2, "E"), .cells(lr, "E")).formular1c1 = _
"=INDEX('Account codes'!C2, MATCH(RC8, 'Account codes'!C1, 0))"
end with
end sub

Resources