Please advise me how to change my code to select rows only if they have a value in BC column (ignore complete row if cell in BC column is blank):
Private Sub CommandButton3_Click()
Range("A:a,b:b,c:c,e:e,bc:bc").Select
Selection.Copy
Workbooks.Add
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=True, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=True, Transpose:=False
End Sub
First run your code as is. Then perform the row deletions in the added workbook:
Sub dural()
Dim N As Long, I As Long, r As Range
N = Cells(Rows.Count, "BC").End(xlUp).Row
For I = N To 1 Step -1
Set r = Cells(I, "BC")
If IsEmpty(r) Then
r.EntireRow.Delete
End If
Next
End Sub
You could do this by using a filter:
Filter on column BC by unchecking (Blanks)
Copy the columns
Paste into a new worksheet or workbook
If it has to be VBA, here are two codes that will perform as desired.
This first code uses the autofilter:
Private Sub CommandButton3_Click()
Dim wsData As Worksheet
Dim wsNew As Worksheet
Set wsData = ActiveSheet
Set wsNew = Sheets.Add
With Intersect(wsData.UsedRange, wsData.Columns("BC"))
.Parent.AutoFilterMode = False
.AutoFilter 1, "<>"
Intersect(.SpecialCells(xlCellTypeVisible).EntireRow, wsData.Range("A:A,B:B,C:C,E:E,BC:BC")).Copy
wsNew.Range("A1").PasteSpecial xlPasteValues
wsNew.Range("A1").PasteSpecial xlPasteFormats
.AutoFilter
End With
wsNew.Move
Set wsData = Nothing
Set wsNew = Nothing
End Sub
This second, alternative code uses a find loop:
Private Sub CommandButton3_Click()
Dim rngFound As Range
Dim rngCopy As Range
Dim strFirst As String
Set rngFound = Columns("BC").Find("*", Cells(Rows.Count, "BC"), xlValues, xlWhole)
If Not rngFound Is Nothing Then
strFirst = rngFound.Address
Set rngCopy = rngFound
Do
Set rngCopy = Union(rngCopy, rngFound)
Set rngFound = Columns("BC").Find("*", rngFound, xlValues, xlWhole)
Loop While rngFound.Address <> strFirst
End If
If Not rngCopy Is Nothing Then
Sheets.Add
Intersect(rngCopy.Parent.Range("A:A,B:B,C:C,E:E,BC:BC"), rngCopy.EntireRow).Copy
Range("A1").PasteSpecial xlPasteValues
Range("A1").PasteSpecial xlPasteFormats
ActiveSheet.Move
End If
Set rngFound = Nothing
Set rngCopy = Nothing
End Sub
Related
I'm trying to copy a variable range from one book (Book1) to the end of a variable range of the another book (book2). I'm interested only in values of the variable range in the book 1 and this is the problem. So I need to find the last row of values (not formulas). On this forum I found several options but none of them works in my case. Here is what I got (Please see the second part of the code "Copy Detail USHB"-'Select cells to copy):
''''''Copy Detail by Vendor''''''
'Last cell in column
Dim WS As Worksheet
Dim LastCell As Range
Dim LastCellRowNumber As Long
Set WS = Worksheets("Detail by Vendor")
With WS
Set LastCell = .Cells(.Rows.Count, "B").End(xlUp)
LastCellRowNumber = LastCell.Row + 1
End With
Dim wb As Workbook, wb2 As Workbook
Dim vFile As Variant
'Set source workbook
Set wb = ActiveWorkbook
'Open the target workbook
Workbooks.Open Filename:= _
"Book2.xlsm"
'Set selectedworkbook
Set wb2 = ActiveWorkbook
'Select cells to copy
Sheets("By Vendor").Select
Range("A2").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy
'Go back to original workbook you want to paste into
wb.Activate
Sheets("Detail by Vendor").Select
'Paste starting at the last empty row
wb.Worksheets("Detail by Vendor").Range("B" & LastCellRowNumber).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Application.ScreenUpdating = True
'''''Copy Detail USHB'''''
'Last cell in column
Set WS = Worksheets("Detail USHB")
With WS
Set LastCell = .Cells(.Rows.Count, "B").End(xlUp)
LastCellRowNumber = LastCell.Row + 1
End With
'Activate the target workbook
wb2.Activate
'Select cells to copy
Sheets("Detail USHB").Select
Dim jLastRow As Long
jLastRow = Columns("B").Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Range(Selection, ActiveCell.SpecialCells(xlLastRow).Select
Selection.Copy
'Go back to original workbook you want to paste into
wb.Activate
Sheets("Detail USHB").Select
'Paste starting at the last empty row
wb.Worksheets("Detail USHB").Range("B" & LastCellRowNumber).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
Following your comments, I believe you are trying to do the following:
'...
'''''Copy Detail USHB'''''
Dim D As Range
Dim S As Range
With wb2.Worksheets("Detail USHB")
'Locate the last non-blank value in source range
LastRow = .Range("B:B").Find(What:="*", _
LookIn:=xlValues, _
SearchDirection:=xlPrevious).Row
'Set range
Set S = .Range("B2:B" & LastRow)
End With
With wb.Worksheets("Detail USHB")
'Find last used cell in destination range
Set D = .Range("B" & .Rows.Count).End(xlUp)
'Offset to next row, and resize appropriately
Set D = D.Offset(1, 0).Resize(LastRow - 1, 1)
End With
'Copy values
D.Value = S.Value
End Sub
I have 15 columns of data, with rows ranging from 400 - 1000, and I have applied filters, I am keen to only copy visible cells, for column D and J, onto a different sheet, but paste special values through transpose into range D6.
I have used this below method, but it is only copying two visible rows, and not every single row according to the code, like it has done for me in the past for other sheets I have run after amending it. The problem could be I am running three or four process in one macro.
I would be keen to know how I can amend this code so it copies column d and column j visible cells, excluding headers into a different sheet
So where do I stand with the code, it runs and applies the filters, but fails to copy all the rows for this particular part of the macro and secondly, I would be keen to know how to amend it so it only copies the Column D and J as the above excluding headers and only copies visible cells for to paste special values through transpose.
Set rngToCopy = .Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy
Report.Range("D6").PasteSpecial xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
Dim rngToCopy As Range, rRange As Range
Set ws = Sheets("Sheet1")
With ws
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
Set rRange = .Range("A1:A" & lRow)
'~~> Remove any filters
.AutoFilterMode = False
With rRange 'Filter, offset(to exclude headers) and copy visible rows
.AutoFilter Field:=1, Criteria1:="<>"
Set rngToCopy = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
End With
'~~> Remove any filters
.AutoFilterMode = False
rngToCopy.Copy
'
'~~> Rest of the Code
'
End With
End Sub
I added thomas code to sub piece to see if the autofilter works and getting error 91
Sub Filter()
Dim Sheetx As Worksheet
Dim rngToCopy As Range, rRange As Range
With Sheetx
Set rRange = .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
With rRange
.AutoFilter Field:=11, Criteria1:="30"
.AutoFilter Field:=4, Criteria1:="1"
.AutoFilter Field:=2, Criteria1:="=*1", _
Operator:=xlAnd
With .SpecialCells(xlCellTypeVisible)
Set rngToCopy = Union(.Offset(0, 3), .Offset(0, 9))
End With
rngToCopy.Copy
End With
End With
End Sub
We can use Union and Range.Offset to join the cells together define the range.
MSDN: Application.Union Method (Excel)
Returns the union of two or more ranges.
Sub Sample()
Dim lRow As Long
Dim rngToCopy As Range, rRange As Range
With Sheets("Sheet1")
With .Range("A1").CurrentRegion
.AutoFilter Field:=11, Criteria1:="=30"
.AutoFilter Field:=4, Criteria1:="=1"
.AutoFilter Field:=2, Criteria1:="=1", _
Operator:=xlAnd
On Error Resume Next
Set rngToCopy = .SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If rngToCopy Is Nothing Then
MsgBox "SpecialCells: No Data", vbInformation, "Action Cancelled"
Exit Sub
End If
Set rngToCopy = Intersect(rngToCopy, .Range("B:B,H:H"))
If rngToCopy Is Nothing Then
MsgBox "Intersect: No Data", vbInformation, "Action Cancelled"
Exit Sub
End If
End With
End With
rngToCopy.Copy
Sheets("Sheet2").Range("C6").PasteSpecial xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
End Sub
for the code below, if a search comes up empty based on Criteria1:="Ship", then there is nothing to copy, and the code stops at Set rngCopy = Intersect(rngCopy, .Range("A1:H" & lRow), .Range("A1:H" & lRow).Offset(1)).SpecialCells(xlCellTypeVisible), how can I get rid of this bug? Additionally, I want the whole table to show even if no data meets the criteria. I have the line Worksheets("Efficiency").ShowAllData but this is under the assumption the code runs all the way.
Thanks,
Sub ImportShipper()
Dim wsEff As Worksheet
Dim wsShip As Worksheet
Dim wsFirst As Worksheet
Set wsEff = Worksheets("Efficiency")
Set wsFirst = Worksheets("1")
Set wsShip = ActiveSheet
wsShip.Name = wsFirst.Range("B34").Value
With wsEff
Dim lRow As Long
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("A1:H" & lRow).AutoFilter Field:=2, Criteria1:="Ship"
Dim rngCopy As Range
'All Columns A:H
Set rngCopy = .Columns("A:H")
'filtered rows, not including header row - assumes row 1 is headers
Set rngCopy = Intersect(rngCopy, .Range("A1:H" & lRow), .Range("A1:H" & lRow).Offset(1)).SpecialCells(xlCellTypeVisible)
rngCopy.Copy
End With
wsShip.Range("A4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Worksheets("Efficiency").ShowAllData
End Sub
you could go like this
Option Explicit
Sub ImportShipper()
Dim wsEff As Worksheet
Dim wsShip As Worksheet
Dim wsFirst As Worksheet
Set wsEff = Worksheets("Efficiency")
Set wsFirst = Worksheets("1")
Set wsShip = ActiveSheet
wsShip.Name = wsFirst.Range("B34").value
With wsEff
Dim lRow As Long
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
With .Range("A1:H" & lRow)
.AutoFilter Field:=2, Criteria1:="Ship"
With .Offset(1).Resize(.Rows.Count - 1)
If Application.WorksheetFunction.Subtotal(103, .Columns(2)) > 0 Then
.SpecialCells(xlCellTypeVisible).Copy
wsShip.Range("A4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
End With
End With
End With
Worksheets("Efficiency").ShowAllData
End Sub
Can some one please let me know how I can copy values from sheet1 and paste them to teh rest of existing values?
I have two sheets called "DTMGIS" and "DTMFinal" they have exactly same structure but I need to add values from "DTMGIS" to end of (I mean after Last Row) "DTMFinal"?
I already got this code from Soren at this Post which works for me on copying in an empty sheet but for appending data to existing I think I need something more
Sub CopyPasteValues()
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = ThisWorkbook.Sheets("DTMGIS")
Set ws2 = ThisWorkbook.Sheets("DTMEdit")
ws1.Range(ws1.UsedRange.Address).Copy
ws2.Range("a1").PasteSpecial xlPasteValues
End Sub
Try this
Sub CopyPasteValues()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim rng1 As Range, rng2 As Range
Set ws1 = ThisWorkbook.Sheets("DTMGIS")
Set ws2 = ThisWorkbook.Sheets("DTMEdit")
With ws1
Set rng1 = .Cells.Find("*", .Cells(1, 1), xlValues, xlWhole, xlByRows, xlPrevious)
Range(.Cells(1, 1), rng1).EntireRow.Copy
End With
With ws2
Set rng2 = .Cells.Find("*", .Cells(1, 1), xlValues, xlWhole, xlByRows, xlPrevious)
If rng2 Is Nothing Then
.Cells(1, 1).PasteSpecial xlPasteValues
Else
rng2.EntireRow.Cells(2, 1).PasteSpecial xlPasteValues
End If
End With
End Sub
Updated to avoid UsedRange
This is a General Solution to ur Question, Soluton can be made more specific if you can provide some sample data to be copy pasted
General Solution as follows :
Sub CopyPaste()
'Copying Data
Sheets("DTMGIS").Activate
Range("A1").Select
' DataStart = Selection.Address
' Selection.End(xlToRight).Select
' Selection.End(xlDown).Select
' DataEnd = Selection.Address
' Range(DataStart, DataEnd).Select
' Uncomment The Above 5 lines if u have Multiple columns of data
' and Comment the Below line.
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
' Pasting data
Sheets("DTMFinal").Activate
If WorksheetFunction.CountA(Cells) = 0 Then 'Checking If Sheet has no data
Range("A1").Select
ActiveCell.PasteSpecial
Else
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveCell.PasteSpecial
End If
End Sub
Feel Free to ask for any modifications in the code. As there was no sample data I have made some asumptions..
I have the code working exactly the way I'd like to, however I don't want it to skip onto another column. I just want my macro to run inside column C then exit.
I am new to VBA in excel, so please pardon my faults.
Any help would be much appreciated.
Thanks in advance.
Sub CopyValuetoRange()
'
' CopyValuetoRange Macro
Dim search_range As Range, Block As Range, last_cell As Range
Dim first_address$
Set search_range = ActiveSheet.UsedRange
Set Block = search_range.Find(what:="*", _
after:=search_range.SpecialCells(xlCellTypeLastCell), _
LookIn:=xlValues, searchorder:=xlColumns, searchdirection:=xlDown)
If Block Is Nothing Then Exit Sub
Set Block = Block.CurrentRegion
first_address$ = Block.Address
Do
Block.Select
Selection.End(xlDown).Select
ActiveCell.CurrentRegion.Rows(2).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.FormulaR1C1 = "=R[-1]C"
'MsgBox "Next Block Range"
Set last_cell = Block.Cells(Block.Rows.Count)
Set Block = search_range.FindNext(after:=last_cell).CurrentRegion
Loop Until Block.Address = first_address$ 'ActiveSheet.Range("C26").End(xlDown).Row
End Sub
Here is something I modified from something I found that will essentially do the same thing, however it puts the first cells value into all cells in the range. And this macro actually stays in Column C, since I found recently because it's not a region, it's a range.
Is there a way to change the following to add a formula to all cells in the range that points to the first cell in the range?
Sub Macro5()
Dim Rng As Range
Dim RngEnd As Range
Dim rngArea As Range
Set Rng = Range("C1")
Set RngEnd = Cells(Rows.Count, Rng.Column).End(xlDown)
If RngEnd.Row < Rng.Row Then Exit Sub
Set Rng = Range(Rng, RngEnd)
On Error GoTo ExitSub
Set Rng = Rng.SpecialCells(xlCellTypeConstants)
For Each rngArea In Rng.Areas
rngArea.Value = rngArea.Cells(Rng.Rows.Count, 1).Value
Next rngArea
ExitSub:
' Macro will exit here if the range is empty.
End Sub
How about you change your search_range, so that you only search Column C?
Set search_range = ActiveSheet.Range("C:C")
Set Block = search_range.Find(what:="*", _
LookIn:=xlValues, searchorder:=xlColumns, searchdirection:=xlDown)
Here's what I have, it's not pretty but it works. I added a column on both sides then removed them after the macro went through the entire column:
Sub CopyFirstCellInRangeInOneColumn()
'
' CopyValuetoRange Macro
Dim search_range As Range, Block As Range, last_cell As Range
Dim first_address$
''
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("E:E").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
''
Set search_range = ActiveSheet.Range("D:D")
Set Block = search_range.Find(what:="*", _
LookIn:=xlValues, searchorder:=xlColumns, searchdirection:=xlDown)
'Set search_range = ActiveSheet.UsedRange
'Set Block = search_range.Find(What:="*", _
' After:=search_range.SpecialCells(xlCellTypeLastCell), _
' LookIn:=xlValues, SearchOrder:=xlColumns, SearchDirection:=xlDown)
If Block Is Nothing Then Exit Sub
Set Block = Block.CurrentRegion
first_address$ = Block.Address
Do
Block.Select
Selection.End(xlDown).Select
ActiveCell.CurrentRegion.Rows(2).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.FormulaR1C1 = "=R[-1]C"
MsgBox "Next Block Range"
Set last_cell = Block.Cells(Block.Rows.Count)
Set Block = search_range.FindNext(After:=last_cell).CurrentRegion
Loop Until Block.Address = first_address$ 'ActiveSheet.Range("C26").End(xlDown).Row
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("D:D").Select
Selection.Delete Shift:=xlToLeft
End Sub