I am a new programmer when it comes to VBA and I am writing a code that scans multiple Criteria and then copy and pastes to another sheet. I have 2 Sheets right now Sheet1 and Sheet2, I will first need to scan J9 on Sheet1 and then lookup Sheet2 for Sheet1’s J9, afterwards I will pull everything with J9 in it from Sheet2 to Sheet1 with it starting at a specific Row, I’ve tried doing AutoFilter but as I am rarely new I believe that my code should be obsolete, please go easy on me. Many Thanks in advance! PS(I am using VBA 2013)
Sub tgr()
Dim wsData As Worksheet
Dim wsDest As Worksheet
Dim acrit() As String
Set wsData = Sheets("MaterialReport") 'Copying FROM this worksheet (it contains your data)
Set wsDest = Sheets("TIMINGBELTWPULLEY") 'Copying TO this worksheet (it is your destination)
'Populate your array of values to filter for
ReDim acrit(1 To 2)
acrit(1) = "TIMING"
acrit(2) = "PULLEY"
With wsData.Range("C1", wsData.Cells(wsData.Rows.Count, "C").End(xlUp))
.AutoFilter 1, aFruit, xlFilterValues 'Filter using the array, this avoids having to do a loop
'Copy the filtered data (except the header row) and paste it as values
.Offset(1).EntireRow.Copy
wsDest.Cells(wsDest.Rows.Count, "B29").End(xlUp).Offset(1).PasteSpecial xlPasteValues
Application.CutCopyMode = False 'Remove the CutCopy border
.AutoFilter 'Remove the filter
End With
This is my code that I’m working with, I’ve pulled it from a training site and edited it to my needs
Related
My code currently takes data from a table, filters the data by criteria in a column, and then it pastes the data in a specific location on a separate sheet.
I am trying to have it copy all columns except for ColumnQ of the filtered data, and paste as values onto a different sheet.
My current code is on top, however I want it to function like the second bit of code.
Dim dndWS As Worksheet
Dim wb As Workbook
Set wb = ThisWorkbook
Set dndWS = wb.Worksheets("DO NOT DELETE")
With dndWS
.AutoFilterMode = False
With .Range("H3:Q500")
.AutoFilter Field:=9, Criterial1:="ColumnQ"
.SpecialCells(xlCellTypeVisible).Copy Destination:=wb.Worksheets("MASTER").Range("A22:I57")
End With
End With
I want the code to function as such:
Dim dndWS As Worksheet
Dim wb As Workbook
Set wb = ThisWorkbook
Set dndWS = wb.Worksheets("DO NOT DELETE")
With dndWS
.AutoFilterMode = False
With .Range("H3:Q500")
.AutoFilter Field:=9, Criterial1:="ColumnQ"
ONLY SELECT/COPY RANGE H:P FROM FILTERED TABLE
PASTE AS VALUES TO wb.Worksheets("MASTER").Range("A22:I57")
End With
End With
As far as I know you have three options: 1) loop through the entire data range, skipping column Q, writing all values into an array, then paste that into the target sheet, then format everything. 2) copy/paste in two operations, once for cols A-P, then again for R+. 3) copy/paste once, copying all columns, then delete Q from the new sheet. I think 3 is probably the easiest.
I have code to copy a Worksheet A, columns A:C (no set row quantities, this will be re-used and the quantities will change) and paste to the first blank row in the same workbook, different sheet, Worksheet B (this also has no set row quantities and will change).
Worksheet B has a formula in the same columns that I want to paste to that returns "" if there is no data. I think VBA is seeing the "" and assuming there is data there; however, it is not pasting even to lines without said formula.
Sub Copy_Created_Fringe_Accounts()
Dim SourceRange As Range
Dim DestRange As Range
Dim DestSheet As Worksheet
Dim LastRow As Long
'Source sheet and range
Set SourceRange = Sheets("CREATED FRINGE ACCTS").Range("A2:C500")
'Destination sheet and range
Set DestSheet = Sheets("99 BUDGET WORKSHEET")
'Last Row
LastRow = DestSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
'Copy and paste to destination
Set DestRange = DestSheet.Range("A" & LastRow + 1)
SourceRange.Copy DestRange
End Sub
Nothing happens when I run it. I expect to see the data from Worksheet A copied to Worksheet B, starting at the first available empty row.
I am fairly new to VBA so any help/understanding is appreciated.
Finding the last row
Try using UsedRange to find the last used row as this is safer than using Find.
LastRow = DestSheet.UsedRange.Rows.Count
A side note
If your code resides in the same place as these worksheets then I would recommend using their code name. This will protect you from running into an error if the sheet doesn't exist.
I currently have multiple sheets for storing records of payments (things to be Pay and CantPay). I am trying to write a macro that will copy and paste Cells A:M on every row where column T = "Resolved" on the CantPay sheet (where the next empty row is the next row where "a" & row-number = blank) to the "Pay" sheet.
Within the sheet which i want to copy from there is data in columns A:T but N:T are not needed once the problem is resolved. So once i have copy and pasted the data within cells A:M i want to just delete the entire row. I have written some code from what i knew and looking online which isn't working. Any help would be much appreciated.
Thanks
I have tried recording a macro and writing my own but it seems the macro i have wrote is deleting row 1 which is where all my column headers are stored.
Sub MoveToPay()
Dim CantPay As Worksheet: Set CopySheet = Sheets("Can't Pay")
Dim ReadyToPay As Worksheet: Set PasteSheet = Sheets("£ Pay")
Dim lr As Long
Dim S As String
Application.ScreenUpdating = False
Columns(20).AutoFilter 1, "Resolved"
With Range("a2", Range("M" & Rows.Count).End(3))
.Copy PasteSheet.Cells(Rows.Count, 1).End(3).Offset(1)
.EntireRow.Delete
End With
Columns(20).AutoFilter
Application.ScreenUpdating = True
End Sub
This is a difficult Visual Basic question so I’m not sure if anybody in this forum will be able help. But it’s worth a try.
I wrote a program in Visual Basic to be used as a macro in Excel.
In the macro, I am taking data in sheet1 (FINAL) and copying & pasting the values into sheet2 (Data). My data range in sheet1 has many blank cells so I wanted to create a program that will only paste rows with values (versus rows with only blank cells).
My program right now modifies my data range in sheet 1 before pasting into sheet2 and I don’t want that……..my formatting gets all screwed up as a result too. Instead I want the data in my sheet1 to stay completely the same and the blank rows to be removed in the paste action going into sheet2.
My data in sheet1 begins at Column AL and proceeds to Column CD.
It’s very important that the integrity of the rows be maintained. I don’t want blank cells to be erased during the paste, but rather BLANK ROWS from the range to be erased during the paste. So if there is a row between columns AL and CD that has even just one data point, the row as a whole must be maintained in the paste. But for any rows between columns AL and CD that are completely blank, they need to be removed in the paste action going into sheet2.
My existing program is below. Any help would be greatly appreciated.
Dim ws As Worksheet
Set ws1 = Worksheets("FINAL")
Set ws2 = Worksheets("Data")
With ws1.UsedRange
lastcolumn = .Cells(1, 1).Column + .Columns.Count - 1
lastrow = .Cells(1, 1).Row + .Rows.Count - 1
End With
ws1.Range(Cells(1, 38), Cells(lastrow, lastcolumn)).AutoFilter field:=1, Criteria1:="<>"
ws1.Range(Cells(1, 38), Cells(lastrow, lastcolumn)).Copy
ws2.Range("A1").PasteSpecial xlPasteValues
Application.CutCopyMode = False
This is a difficult Visual Basic question so I’m not sure if anybody in this forum will be able help. But it’s worth a try.
Hope it was worth a try :P
Is this what you are trying?
Sub Sample()
Dim wsInput As Worksheet, wsOutput As Worksheet
Dim rng As Range, CellsTobeCopied As Range, aCell As Range
'~~> Sheet which has range that you want to copy
Set wsInput = ThisWorkbook.Sheets("Sheet1")
'~~> Set range that you would like to copy
Set rng = wsInput.Range("A1:E4")
'~~> Output Sheet where you want to paste
Set wsOutput = ThisWorkbook.Sheets("Sheet2")
For Each aCell In rng.Rows
'~~> Check if the entire row is blank
If Application.WorksheetFunction.CountA(aCell) <> 0 Then
'~~> Construct your range to be copied
If CellsTobeCopied Is Nothing Then
Set CellsTobeCopied = aCell
Else
Set CellsTobeCopied = Union(CellsTobeCopied, aCell)
End If
End If
Next
'~~> Copy final range
If Not CellsTobeCopied Is Nothing Then
CellsTobeCopied.Copy
'~~> In case you want to preserve formats
wsOutput.Range("A1").PasteSpecial xlPasteAll
'~~> If you wan tto paste values then comment the above and use this
' CellsTobeCopied.Copy wsOutput.Range("A1")
End If
End Sub
Screenshot
I have a spreadsheet I'm using to compile text that changes all the time.
In column AD, Row 4(AD4) I put the contents of text, and it can have data going 1000 to 4000 rows down. It changes every time, so there is no static range name. I need a macro that
finds the final piece of data in that column,
then automatically "drags a box" from that spot two columns to the left (AB4)
and copies it... (A 3000 row piece of text would be AB4:AD3004) (Macro stops there, with text to be copied highlighted)
The current version finds the bottom cell correctly, but if I run the macro a 2nd time, with new data, it keeps trying to copy the same range. (I used the Formula Define.Name method, to name the cell, and then selected AB4:LastRow) but it is ALWAYS 3160 whether data goes to row 4000 or not.....
Sub Last_row()
Cells(Application.Rows.Count, 30).End(xlUp).Select
' following lines of code are useless
Range("AB4:AD3160").Select
Range("AD3160").Activate
Selection.Copy
End Sub
To answer your question directly:
With Sheet1
.Range("AB4", .Cells(Rows.Count, "AD").End(xlUp)).Copy
End With
Copy to specific location WITHOUT using clipboard:
With Sheet1
.Range("AB4", .Cells(Rows.Count, "AD").End(xlUp)).Copy Sheet2.[A1]
End With
Copy and exclude formatting:
With Sheet1
With .Range("AB4", .Cells(Rows.Count, "AD").End(xlUp))
Sheet2.Cells(1, "A").Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
End With
Note: Replace all sheet codenames (sheet1, Sheet2) above with your actual sheet codenames.
Your current code hard-codes the range of interest with
Range("AB4:AD3160").Select
This code will define a dynamic range starting from AB4 to the last non-empty cell in column AD
You can then use this range (without selecting) for changing values elsewhere (note that you may not need to actually copy rng1, it is possible to dump these values to a separate range directly without a copy and paste.
Sub Last_row()
Dim rng1 As Range
Set rng1 = Range([ab4], Cells(Rows.Count, 30).End(xlUp))
rng1.Copy
End Sub
Update: Example of how to copy a dynamic sized range from one sheet to another without a copy and paste:
Sub Last_row2()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng1 As Range
Set ws1 = Sheets(1)
Set ws2 = Sheets(2)
Set rng1 = ws1.Range(ws1.[ab4], ws1.Cells(Rows.Count, 30).End(xlUp))
ws2.[a1].Resize(rng1.Rows.Count, rng1.Columns.Count).Value = rng1.Value
End Sub