Copying and pasting visible cells only - excel

I need to copy a column of visible cells and paste to the next column over.
I can't find a macro that works. I had one going, but it only copies some numbers.
Here is the code
Sub TryMe()
Sheet1.Range("A1:A100").SpecialCells(xlCellTypeVisible).Copy _
Destination:=Range("A1").Offset(ColumnOffset:=1)
End Sub
This image is before I run the macro. Notice the rows that are hidden. I need these numbers to copy to the next column.
This image is after I run the macro. I don't understand why only some of the numbers are copying. The hidden rows contain the numbers 3 and 6. Why are they in the outcome, but not the visible numbers? I need to copy what is seen.

You can't do it that way even if you manually do it.
You will have to loop to get what you want. So give this a try.
Dim c As Range
For Each c In Sheet1.Range("A1:A100").SpecialCells(xlCellTypeVisible)
If Len(c) <> 0 Then c.Offset(0, 1) = c
Next
The odd thing about your result is why does it copy the values in reverse order.
I can understand if it copies all the visible cells at B1 onwards, but not the reversal of values.
Anyways, try above first if it gets you going.

I'm not sure how you have the output in reverse order, but for me your code works:
Sub TryMe()
'1. some visible values in col A will be will be hidden in col B by hidden rows
Sheet1.Range("A1:A10").SpecialCells(xlCellTypeVisible).Copy _
Destination:=Range("A1").Offset(ColumnOffset:=1)
'2. all visible values in col A will be will be visible bellow
Sheet1.Range("A1:A10").SpecialCells(xlCellTypeVisible).Copy _
Destination:=Range("A11").Offset(ColumnOffset:=1)
End Sub

Use this code guys! Works like a charm :)
Sub PasteToFilteredCells()
Dim rng1 As Range
Dim rng2 As Range
Dim InputRng As Range
Dim OutRng As Range
Dim n As Integer
n = 0
Dim x As Integer
Dim c As Integer
c = 0
xTitleId = "Paste Buddy"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Copy Range :", xTitleId, InputRng.Address, Type:=8)
Set OutRng = Application.InputBox("Paste Range:", xTitleId, Type:=8)
x = InputRng.SpecialCells(xlCellTypeVisible).Count
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
For Each rng1 In InputRng
If rng1.EntireRow.RowHeight > 0 Then
rng1.Copy
c = c + 1
Else
GoTo NextIte
End If
Do While (c < (x + 1))
If (OutRng.Offset(n, 0).EntireRow.RowHeight > 0) Then
OutRng.Offset(n, 0).PasteSpecial
n = n + 1
GoTo NextIte
Else
n = n + 1
End If
Loop
NextIte:
Next rng1
Application.CutCopyMode = False
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
End Sub

Related

Excel VBA deleting rows based on an if statement (speed up)

I am deleting rows based on the value in column P.
Cells in column P have an if statement: IF(K<10,0,1)
If the value in column P is 0, then the row needs to be deleted.
I am using the following macro which works but takes quite long.
I would like to beable to process about 10000 rows.
It would be much appreciated if I could have some suggestions on speeding up this code.
[I had tried using this if statement: IF(K<10,"",1)
And then deleting rows using SpecialCells(XlCellTypeBlanks) but the the cells are not interpreted as blank , due to the presence of the formula I presume. ]
Sub RemoveBlankRows()
Application.ScreenUpdating = False
'PURPOSE: Deletes any row with 0 cells located inside P
'Reference: www.TheSpreadsheetGuru.com
Dim rng As Range
Dim blankrng As Range
Dim cell As Range
'Store blank cells inside a variable
'On Error GoTo NoBlanksFound
Set rng = Range("P2:P30000") '.SpecialCells(xlCellTypeBlanks)
'On Error GoTo 0
For Each cell In rng
If cell.Value = 0 Then
cell.EntireRow.Delete
'Value = ""
End If
Next
Application.ScreenUpdating = True
End Sub
This looks for 0 and avoids blanks:
Sub RowKiller()
Dim rKill As Range, r As Range, rng As Range
Set rng = Range("P2:P30000")
Set rKill = Nothing
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
For Each r In rng
If r.Value = 0 And r.Value <> "" Then
If rKill Is Nothing Then
Set rKill = r
Else
Set rKill = Union(rKill, r)
End If
End If
Next r
If Not rKill Is Nothing Then rKill.EntireRow.Delete
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub
This is only demo code. Tailor it to meet your needs.

Evaluate a list of values in a column against a combobox value most efficiently

I am trying to delete duplicate values in a temporary list based on a value in a combobox. The code below loops through individual rows to check whether a value matches. It is slow.
Dim ws As Worksheet
Dim i As Long
Set ws = Sheets("TempList3")
On Error Resume Next
For i = Cells(Rows.Count, 2).End(xlUp).Row To 1 Step -1
If Cells(i, 2) <> Sheets("Sheet1").ComboBox2.Value Then
ws.Rows(i).EntireRow.Delete
End If
Next
Is there a way to evaluate the entire column's values against the combobox's value once and then delete all rows on a worksheet. Or perhaps there is a better way?
I used a looping Find function, it deletes the row where the value was found and then it searches again and deletes the next row it finds until it can no longer find the Combo value on the sheet:
Sub find_cell()
Dim find_cell As Range
Set ws = Sheets("TempList3")
stop_loop = False
Do Until stop_loop = True
Set find_cell = ws.Cells.Find(What:=Sheets("Sheet1").ComboBox2.Value, LookAt:=xlWhole)
If Not find_cell Is Nothing Then
ws.Rows(find_cell.Row).EntireRow.Delete
Else
stop_loop = True
End If
Loop
End Sub
Not knowing how many rows you are talking about, I used 10 thousand for my example codes. here are two examples, try the both and see what works best for you.
You can run through the column and unionize the range found, then delete the rows, for example.
See here for example workbook
Sub UnIonRng()
Dim FrstRng As Range
Dim UnIonRng As Range
Dim c As Range, s As String
s = Sheets("Sheet1").ComboBox2
Set FrstRng = Range("B:B").SpecialCells(xlCellTypeConstants, 23)
For Each c In FrstRng.Cells
If c = s Then
If Not UnIonRng Is Nothing Then
Set UnIonRng = Union(UnIonRng, c) 'adds to the range
'MsgBox UnionRng.Address 'remove later
Else
Set UnIonRng = c
End If
End If
Next c
UnIonRng.EntireRow.Delete
End Sub
Or you can try to filter the column B and delete the rows that way:
Sub FilterDeleteRow()
Dim ws As Worksheet
Dim LstRw As Long, Rng As Range, s As String, x
Set ws = Sheets("TempList3")
s = Sheets("Sheet1").ComboBox2
Application.ScreenUpdating = 0
With ws
LstRw = .Cells(.Rows.Count, "B").End(xlUp).Row
x = Application.WorksheetFunction.CountIf(.Range("B:B"), s)
If x > 0 Then
Columns("B:B").AutoFilter Field:=1, Criteria1:=s
Set Rng = .Range("B2:B" & LstRw).SpecialCells(xlCellTypeVisible)
Rng.EntireRow.Delete
.AutoFilterMode = 0
Else: MsgBox "Not Found"
End If
End With
End Sub

VBA Excel Merging of Cells based on a Specific cell value

I would like to automate the merging of cells based by column for multiple columns based on the information in a specific column.
Based on the below picture the values in column c will determine the number of rows that need to be merged together for Columns A through K. With each change in the value in Column C - the merging would begin again.
Private Sub MergeCells_C()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim rngMerge As Range, cell As Range
Set rngMerge = Range("C1:C1000") 'Set the range limits here
MergeAgain:
For Each cell In rngMerge
If cell.Value = cell.Offset(1, 0).Value And IsEmpty(cell) = False Then
Range(cell, cell.Offset(1, 0)).Merge
GoTo MergeAgain
End If
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
This worked for me:
Sub MergeCellsByC()
Dim c As Range, sht As Worksheet, currV
Dim n As Long, rw As Range, r As Range
Set sht = ActiveSheet
Set c = sht.Range("C4") 'adjust to suit....
currV = Chr(0) 'start with a dummy value
Do
If c.Value <> currV Then
If n > 1 Then
Set rw = c.EntireRow.Range("A1:K1") 'A1:K1 relative to the row we're on...
Application.DisplayAlerts = False
'loop across the row and merge the cells above
For Each r In rw.Cells
r.Offset(-n).Resize(n).Merge
Next r
Application.DisplayAlerts = True
End If
currV = c.Value
n = 1
Else
n = n + 1 'increment count for this value
End If
If Len(c.Value) = 0 Then Exit Do 'exit on first empty cell
Set c = c.Offset(1, 0) 'next row down
Loop
End Sub

VBA Excel: Prevent Excel to change data as date after changing all cells to uppercase

I have the following code to capitalize all data in two specified ranges and then run some comparing code.
The issue is once it runs the capitalize code cells that contain something like 1-2 gets changed to 2-Jan. I cannot apply .NumberFormat = "#" to the entire worksheet or that specific column because I am making the sheet dynamic and this data won't always be in the same column. Anyone know how to take care of this problem?
Dim rangeToUse As Range, singleArea As Range, cell1 As Range, cell2 As Range, rng As Range, rng2 As Range
Dim I As Integer, J As Integer
'Set two range selections
Set rng = Application.InputBox("Select First Range", "Obtain 1st Range Object", Type:=8)
Set rng2 = Application.InputBox("Select Second Range", "Obtain 2nd Range Object", Type:=8)
Set MultiRange = Union(rng, rng2)
MultiRange.Select
Set rangeToUse = Selection
Cells.Interior.ColorIndex = 0
Cells.Borders.LineStyle = xlNone
'Capitalizes all cells in selected range
'Turn off screen updating to increase performance
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'Worksheets("Phase 3 xwire").Range(rangeToUse).NumberFormat = "#"
'Convert all constants and text values to proper case
For Each LCell In Cells.SpecialCells(xlConstants, xlTextValues)
LCell.Formula = UCase(LCell.Formula)
Calculate
Next
If Selection.Areas.Count <= 1 Then
MsgBox "Please select more than one area."
Else
rangeToUse.Interior.ColorIndex = 0
For Each singleArea In rangeToUse.Areas
singleArea.BorderAround ColorIndex:=1, Weight:=xlMedium
Next singleArea
'Areas.count - 1 will avoid trying to compare
' Area(count) to the non-existent area(count+1)
For I = 1 To rangeToUse.Areas.Count - 1
For Each cell1 In rangeToUse.Areas(I)
'I+1 gets you the NEXT area
Set cell2 = rangeToUse.Areas(I + 1).Cells(cell1.Row - 1, cell1.Column - 1)
If IsEmpty(cell2.Value) Then
GoTo Done
Else
If cell1.Value <> cell2.Value Then
cell1.Interior.ColorIndex = 38
cell2.Interior.ColorIndex = 38
End If
End If
Next cell1
Next I
Done:
End If
'Turn screen updating back on
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
If you are keeping the Input Boxes you could add this line of code after your MultiRange.Select command
Selection.NumberFormat = "#"

Select all Cells at once above limit value

I can Select only the Cells with in region that contain numbers:
Region.SpecialCells(xlCellTypeConstants , xlNumbers)
but I don't know how to Select only the cells that are above a number. For example those above 1.0
I have a big Sheet with numbers and I want to cap all numbers above 1, and set them to 1. I would love to do it without having to loop on each cell.
thanks!
This method below avoids the cell by cell loop - while it is significantly longer than your range loop code I share your preference for avoiding cell by cell range loops where possible
I have updated my code from A fast method for determining the unlocked cell range to provide a non cell by cell loop method
the code checks that SpecialCells(xlCellTypeConstants , xlNumbers)
exist on the sheet to be updated (error handling should always be
used with SpecialCells
if these cells exist, a working sheet is created, and a formula is inserted into the range from step 1 to create a deliberate error (the 1/0) if the value on the main sheet is >1
SpecialCells(xlCellTypeFormulas, xlErrors) returns a range of cells from the working sheet where the values were greater than 1 (into rng3)
All areas in rng3 are set to 1 with rng3.Value2=1
Sub QuickUpdate()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim lCalc As Long
Set ws1 = ActiveSheet
On Error Resume Next
Set rng1 = ws1.Cells.SpecialCells(xlConstants, xlNumbers)
On Error GoTo 0
'exit if there are no contants with numbers
If rng1 Is Nothing Then Exit Sub
'disable screenupdating, event code and warning messages.
'set calculation to manual
With Application
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
lCalc = .Calculation
.Calculation = xlCalculationManual
End With
ws1.Copy After:=Sheets(Sheets.Count)
Set ws2 = ActiveSheet
'test for cells constants > 1
ws2.Cells.SpecialCells(xlConstants, xlNumbers).FormulaR1C1 = "=IF('" & ws1.Name & "'!RC>1,1/0,'" & ws1.Name & "'!RC)"
On Error Resume Next
Set rng2 = ws2.Cells.SpecialCells(xlCellTypeFormulas, xlErrors)
On Error GoTo 0
If Not rng2 Is Nothing Then
Set rng3 = ws1.Range(rng2.Address)
rng3.Value2 = 1
Else
MsgBox "No constants < 1"
End If
ws2.Delete
'cleanup user interface and settings
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
lCalc = .Calculation
End With
'inform the user of the unlocked cell range
If Not rng3 Is Nothing Then
MsgBox "Cells updated in Sheet " & vbNewLine & ws1.Name & vbNewLine & " are " & vbNewLine & rng3.Address(0, 0)
Else
MsgBox "No cells updated in " & ws1.Name
End If
End Sub
I say, forget about SpecialCells. Just load all cells that need testing into a Variant array. Then loop over that array and do your capping. That is very efficient, contrary to looping over cells in a sheet. Finally, write it back to the sheet.
With 50,000 cells containing random values between 0 and 2, this code ran in 0.2 s on my antique laptop.
The added bonus is that this is quite clear and readable code, and you retain full control over what range will be operated on.
Dim r As Range
Dim v As Variant
Set r = Sheet1.UsedRange
' Or customise it:
'Set r = Sheet1.Range("A1:HZ234") ' or whatever.
v = r ' Load cells to a Variant array
Dim i As Long, j As Long
For i = LBound(v, 1) To UBound(v, 1)
For j = LBound(v, 2) To UBound(v, 2)
If IsNumeric(v(i, j)) And v(i, j) > 1 Then
v(i, j) = 1 ' Cap value to 1.
End If
Next j
Next i
r = v ' Write Variant array back to sheet.
What is the harm in looping? I just tested this code on a range of 39900 cells and it ran in 2 Secs.
Sub Sample()
Dim Rng As Range, aCell As Range
Set Rng = Cells.SpecialCells(xlCellTypeConstants, xlNumbers)
For Each aCell In Rng
If aCell.Value > 1 Then aCell.Value = 1
Next aCell
End Sub
My only concern is the use of SpecialCells as they are unpredictable and hence I rarely use them.
Also have a look at this KB article: http://support.microsoft.com/?kbid=832293

Resources