I have 600k rows and want to remove starting and trailing whitespace. I have the following, but it is rather slow:
Sub Macro1()
'
' Macro1 Macro
'
'
Range("D1").Select
ActiveCell.FormulaR1C1 = "=TRIM(RC[-1])"
Range("D1").Select
Selection.AutoFill Destination:=Range("D1:D4")
Range("D1:D4").Select
Columns("D:D").Select
Selection.Copy
Range("C1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("D:D").Select
Application.CutCopyMode = False
Selection.ClearContents
Range("C1").Select
End Sub
Is there a way that I can apply the function on itself. I would like to avoid running a function in an empty column, then copying the values to the original column.
I tried VBA to fill formula down till last row in column as well as to speed up the formula. I have a few columns to do this with, and wonder if it is possible to only work on column C and trim the whitespace without the extra computations.
Thanks
This does not use a second column and does all the values in Column C. It moves the values to an array, iterates the array and trims the excess space and overwrites the values in C with the array.
Sub macro1()
Dim rng As Variant
Dim ws As Worksheet
Dim i As Long
Set ws = Worksheets("Sheet1") 'Change to your sheet name.
With ws
rng = .Range("C1", .Cells(.Rows.Count, 3).End(xlUp)).Value
For i = LBound(rng) To UBound(rng)
rng(i, 1) = Application.Trim(rng(i, 1))
Next i
.Range("C1", .Cells(.Rows.Count, 3).End(xlUp)).Value = rng
End With
End Sub
Change the code like this so you don't use Select. Using Select and Selection slows everything down horribly.
Sub Macro1()
Range("D1").FormulaR1C1 = "=TRIM(RC[-1])"
Range("D1").AutoFill Destination:=Range("D1:D4")
Columns("D:D").Copy
Range("C1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Columns("D:D").ClearContents
End Sub
Related
I'm recording a macro and need some help. I'd like copy and paste the values from the column G of the "SalesData" worksheet into cells A2, A12, A22 etc of the "Results" worksheet until there's no more values in the column G.
VBA is pretty new to me, I've tried using Do/Until, but everything crashed. Could you please help me? Please see the code I've recorded below. Thank you!
Sub(x)
Sheets("SalesData").Select
Range("G2").Select
Selection.Copy
Sheets("Results").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A12").Select
Sheets("SalesData").Select
Range("G3").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Results").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A22").Select
Sheets("SalesData").Select
Range("G4").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Results").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A32").Select
Sheets("SalesData").Select
Range("G5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Results").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
I prefer to find the last cell in the column first then use a For loop.
Since you are only doing the values we can avoid the clipboard and assign the values directly.
Since you paste is every 10 cells we can use a separate counter to move down 10 each loop.
Sub x()
Dim ws As Worksheet
Dim lst As Long
Dim i As Long, j As Long
'use variable to limit the number of times we type the same thing
Set ws = Worksheets("Results")
'First row of the output
j = 2
'using with and the "." in front of those items that belong to it also limits the typing.
With Worksheets("SalesData")
'Find the last row with values in Column G
lst = .Cells(.Rows.Count, 7).End(xlUp).Row
'Loop from the second row to the last row.
For i = 2 To lst
'Assign the value
ws.Cells(j, 1).Value = .Cells(i, 7).Value
'Move down 10 rows on the output
j = j + 10
Next i
End With
End Sub
here is the same thing but using range variables
Sub x()
Dim src As Range
Dim dst As Range
Set dst = Worksheets("Results").Range("a2") ' point to top cell of destination
With Worksheets("SalesData")
For Each src In Range(.Cells(2, "g"), .Cells(.Rows.Count, "g").End(xlUp)) ' loop through used cell range in column G
dst.Value = src.Value
Set dst = dst.Offset(10) ' move destination pointer down 10 rows
Next src
End With
End Sub
This is just for fun/practice for another way to do it:
Sub copyFromG()
Dim copyRng As Range, cel As Range
Dim salesWS As Worksheet, resultsWS As Worksheet
Set salesWS = Sheets("SalesData")
Set resultsWS = Sheets("Results")
Set copyRng = salesWS.Range("G2:G" & salesWS.Range("G2").End(xlDown).Row) ' assuming you have a header in G1
For Each cel In copyRng
resultsWS.Range("A" & 2 + 10 * copyRng.Rows(cel.Row).Row - 30).Value = cel.Value
Next cel
End Sub
Sometimes, I need to run a formula on a selection of a filtered range and then convert it to values, every time I want to copy and then special paste values, I must clear the filters first. I need a macro that will convert the formula to values without clearing the filters, I also want to use a shortcut key for this operation.
The code below will allow this operation, the CTRL+M shortcut is available for this operation. Edit the macro to assign this shortcut key.
Sub PasteFilterValues()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
If Selection.Cells.Count = 1 Then
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Else
Dim rng As Range
Set rng = Selection.SpecialCells(xlCellTypeVisible)
For Each cl In rng
cl.Copy
cl.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next cl
rng.Select
End If
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.CutCopyMode = False
End Sub
This should be faster than the other solution
Option Explicit
Sub fla2values(rng As Range)
Dim c As Range
For Each c In rng.SpecialCells(xlCellTypeVisible)
c.Value = c.Value
Next c
End Sub
Sub test_fla2values()
fla2values Selection
End Sub
I'm trying to remove some duplicates from selected column, but the function removes all duplicates irrespective of the case. RemoveDuplicates considers lower case, upper case, etc as duplicate. E.g. the function removed CENTRAL, central and Central.
I have simply recorded the following code and only changed it a little bit. I need to keep items with different cases and don't want to remove as duplicates.
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+q
'
ActiveWorkbook.Sheets(3).Range("A:A").Clear
Selection.Copy
Sheets("Sheet3").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.RemoveDuplicates Columns:=1, Header:=xlNo
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Sheet2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
'Range("B12").Select
Selection.End(xlToRight).Select
ActiveWorkbook.Sheets(3).Range("A:A").Clear
End Sub
Try the following code using a Dictionary to remove duplicates with case sensitivity:
Option Explicit
Sub Test()
RemoveDuplicates Sheet1.Range("A1:A12")
End Sub
Sub RemoveDuplicates(rngDataColumn As Range)
'assumes rngDataColumn is a column of data
Dim dic As Object
Dim rngCell As Range
Dim varKey As Variant
Dim lngCounter As Long
'create dictionary
Set dic = CreateObject("Scripting.Dictionary")
'dictionary becomes case sensitive
dic.CompareMode = vbBinaryCompare
'iterate range for unique values
For Each rngCell In rngDataColumn
If Not dic.Exists(rngCell.Value) Then
dic.Add Key:=rngCell.Value, Item:=True
End If
Next rngCell
'clear source range
rngDataColumn.ClearContents
'output unique items - with case sensitivity
lngCounter = 1
For Each varKey In dic.Keys
rngDataColumn(lngCounter, 1).Value = varKey
lngCounter = lngCounter + 1
Next varKey
End Sub
A1:A12 in my test case is as follows:
So, to update your recorded macro, you could try:
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+q
'
ActiveWorkbook.Sheets(3).Range("A:A").Clear
Selection.Copy
Sheets("Sheet3").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
'use the new function here
RemoveDuplicates Selection
'Selection.RemoveDuplicates Columns:=1, Header:=xlNo
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Sheet2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
'Range("B12").Select
Selection.End(xlToRight).Select
ActiveWorkbook.Sheets(3).Range("A:A").Clear
End Sub
I have found here and tested some nice solution, that seems to meet your expectations. You have to paste this function into your project:
Option Compare Binary
Sub deleteExactDuplicates(ByVal rng As Range)
Application.ScreenUpdating = False
With CreateObject("scripting.dictionary")
For Each i In rng.Cells
v = i.Value
If .exists(v) Then
i.ClearContents
Else
.Add v, 1
End If
Next i
End With
On Error Resume Next
rng.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
Then, you have to call it in your code. If I understand, you want to remove duplicates from selected range, so the macro would look like this:
Sub test()
deleteExactDuplicates Selection
End Sub
Now, this solution delete not only the values in selected range, but also entire rows, where duplicated values occurred. Are you OK with that, or you need something that removes duplicates only from particular range?
I have 50 worksheets in a workbook. columns a,b,c,d are same as columns e,f,g,h, but both sets might have different number of rows/observations. I need to consolidate all in a single sheet having only 3 columns. I need to append the column names, start copying and pasting (values) from 3rd row onwards (till end of data). I tried recording a macro too but in that case, I have to go through all the sheets manually. Can someone lead me to the right direction? I'm very new to VBA and a little help will be much appreciated. My recorded macro for copying 2 sheets goes like this:
Sheets("page 9").Select
Range("A3:D3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveWindow.ScrollWorkbookTabs Position:=xlLast
Sheets.Add After:=Sheets(Sheets.Count)
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.End(xlDown).Select
Range("A67").Select
ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
Sheets("page 9").Select
Range("E3:H3").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.ScrollWorkbookTabs Position:=xlLast
Sheets("Sheet1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.End(xlDown).Select
Range("A132").Select
ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
Sheets("page 10").Select
Range("A65").Select
Selection.End(xlUp).Select
Range("A3:D3").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.ScrollWorkbookTabs Position:=xlLast
Sheets("Sheet1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.End(xlDown).Select
Range("A197").Select
ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
Sheets("page 10").Select
Range("E3:H3").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
ActiveWindow.ScrollWorkbookTabs Position:=xlLast
Sheets("Sheet1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.End(xlUp).Select
Range("A1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Date"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Type"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Size"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Discount"
Range("A1").Select
End Sub
I doubt that anyone can decypher this code; certainly I lack the ability.
The Macro recorder is a great way of learning the syntax of new commands but it does not produce "good" code. It does not know your objective and records every little step as you do it.
Take the time to study Excel VBA. Search the internet for "Excel VBA Tutorial" or visit a good library or bookshop and select an Excel VBA Primer. There are many to chose from so I am sure you will find something that suits your learning style. This study will quickly repay your investment.
Look through the excel-vba questions on StackOverflow. Many, perhaps most, will be of no current interest to you. But some will show techniques you did not know about but which will be useful. Perhaps the most difficult aspect of learning VBA is discovering what is possible. Once you know statement X exists, you can look it up and study its syntax and functionality.
Below are four macros that demonstrate relevant code. Copy them to a workbook and try them. You could not have learnt how to write these macros from a study of macro recorder output.
A This macro outputs the name of every worksheet to the Immediate Window.
Sub A()
Dim InxWsht As Long
For InxWsht = 1 To Worksheets.Count
Debug.Print Worksheets(InxWsht).Name
Next
End Sub
B This adds a new worksheet at the end of the current list and names it "Consolidate". It then creates a bold, coloured header line.
Range(CellId).Value is one way of accessing a cell's value. I have used "A1" as the cells's Id but this is just a string and could have been built at runtime. Cells(RowId, ColId).Value is another way. RowId must be a number or an integer variable. ColId can be a number, an integer variable or a column letter. I suggest you be consistent and not mix and match as I have.
I show two method of specifying a range so I can set the entire header row bold and coloured in single statements.
If I have written Range("A1").Value = "Date" this statement would have operated on cell A1 of the active worksheet. The . before Range means this statement operates of cell A1 of the worksheet identified in the With statement. Using With means I do not have to switch worksheets using Select which is a slow command.
Sub B()
Dim WhshtCons As Worksheet
Set WhshtCons = Sheets.Add(After:=Sheets(Sheets.Count))
WhshtCons.Name = "Consolidate"
With WhshtCons
.Range("A1").Value = "Date"
.Cells(1, 2).Value = "Type"
.Cells(1, "C").Value = "Size"
.Cells(1, 4).Value = "Discount"
.Range("A1:D1").Font.Bold = True
.Range(.Cells(1, 1), .Cells(1, "D")).Font.Color = RGB(0, 128, 128)
End With
End Sub
C This outputs the value of Cell A1 of every worksheet except "Consolidate".
Sub C()
Dim InxWsht As Long
For InxWsht = 1 To Worksheets.Count
If Worksheets(InxWsht).Name <> "Consolidate" Then
With Worksheets(InxWsht)
Debug.Print "Cell A1 of Worksheet " & .Name & " contains [" & _
.Cells(1, 1).Value & "]"
End With
End If
Next
End Sub
D I will not explain this macro because it is somewhat more advanced than the others. It demonstrates moving columns of data from all the other worksheets to worksheet "Consolidate". I doubt this is close to what you seek but it demonstrates that what you seek is possible.
Sub D()
Dim ColConsCrnt As Long
Dim InxWsht As Long
Dim RowLast As Long
Dim WhshtCons As Worksheet
ColConsCrnt = 1
Set WhshtCons = Worksheets("Consolidate")
WhshtCons.Cells.EntireRow.Delete
For InxWsht = 1 To Worksheets.Count
If Worksheets(InxWsht).Name <> "Consolidate" Then
With Worksheets(InxWsht)
RowLast = .Cells(Rows.Count, "A").End(xlUp).Row
WhshtCons.Cells(1, ColConsCrnt).Value = .Name
.Range(.Cells(1, "A"), .Cells(RowLast, "A")).Copy _
Destination:=WhshtCons.Cells(2, ColConsCrnt)
End With
ColConsCrnt = ColConsCrnt + 1
End If
Next
End Sub
Welcome to programming. I hope you find it as much fun as I do.
What am I doing wrong with my code below? I am trying to name a range of data that is highlighted in excel and be able to call it in the VBA code and paste it, transpose it, etc. somewhere else but it keeps giving me an error.
Sub routine()
Dim rng As Range
Set rng = ActiveCell.CurrentRegion
Cells(10, "D").Select
rng.PasteSpecial
End Sub
I also notice that when type "ActiveCell." and hit space i get a drop down of options. however the case isn't true when i type "Cells(1,1)." and space. Why is that? Thank you guys for your help!
Edit: after reading the comments:
Here is a simpler way to copy a range of cells, then pasting special (values) to somewhere else. I obtained this code my recording a macro entirely.
Sub Macro1()
Range("A1:C3").Select
Selection.Copy
Cells(10,"D").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
If you meant to copy the D10 range to whatever activecell is, then Change
Cells(10, "D").Select
to
Cells(10, "D").copy
You also need to specify what do you want to SPECIALLY PASTE (values? format?)
So your full code should be like
Sub routine()
Dim rng As Range
Set rng = ActiveCell.CurrentRegion
Cells(10, "D").Copy
rng.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False 'This will only paste values
Application.CutCopyMode = False
End Sub