Using SpecialCells for multi-column same-row copy/pasting - excel

Is there a way to, when grabbing all numbers in column "B" by using the .SpecialCells(xlCellTypeConstants, 1), to also copy a cell in the same row?
Example:
Let's say the script found cells B2, B4, B5 with numbers. How would I also copy D2, D4, and D5? Can I do that and still use specialcells? Ultimately, I'd like to copy/paste those values into columns A & B on another sheet.
Thanks!
Dim strOutputFile As Variant
Dim wbkOut As Workbook
Dim tenln As Range
Dim tenlnPaste As Range
Dim wbkVer As Workbook
If strOutputFile(u) Like "*Lines.csv" Then
With wbkOut.Worksheets(1)
Set tenln = wbkOut.Worksheets(1).Cells(Rows.Count, 2).End(xlUp)
Set tenlnPaste = wbkVer.Worksheets("TLines").Range("A" & .Rows.Count).End(xlUp).Offset(1).Resize(tenln.Rows.Count, 1)
wbkOut.Worksheets(1).Range("B:B").SpecialCells(xlCellTypeConstants, 1).Copy
With wbkVer.Worksheets("TenLines")
tenlnPaste.PasteSpecial xlPasteValues
End With
End With
End If

Yes. It's actually very easy. Do like below:
Dim rngConst as Range
On Error Resume Next
Set rngConst = wbkOut.Worksheets(1).Range("B:B").SpecialCells(xlCellTypeConstants, 1)
If Not rngConst is Nothing Then
rngConst.Copy
'do your pasting
Set rngConts = rngConst.Offset(,2) 'for moving from B to D
rngConst.Copy
'do your pasting
End If
On Error Go To 0
You could also do this, to get it all into 1 copy area:
Dim rngConst as Range
On Error Resume Next
If Not rngConst is Nothing
Set rngConst = wbkOut.Worksheets(1).Range("B:B").SpecialCells(xlCellTypeConstants, 1)
Set rngConst = Range(rngConst, rngConst.Offset(,2))
rngConst.Copy
'do your pasting
End If
On Error Go To 0
But this will copy the data onto the new sheet into two contiguous columns. It will not copy from B to B and D to D, for instance.

Related

Looping code to search for blank cells and add text in the adjacent column

I am trying to put code together that automatically applies text to a cell, if the adjacent cell is not empty. My data set is as follows;
I am looking to populate everything in column A, providing it's adjacent column B cell is not empty. So, I want to A6 to 10 populated with OK. This will change each day, as more items are added to column B. This is the code I prepared;
Dim BlankCell As Range
Set BlankCell = Sheets("Pipeline").Range("A" & Rows.Count).End(xlUp).Offset(1, 1)
Do Until BlankCell = ""
If Not IsEmpty(BlankCell) Then
BlankCell.Offset(0, -1).Value = "OK"
End If
Loop
The IF statement seems to be working. If I run it on it's own it would populate A6 with OK, but then i'd have to run again for A7, and then A8, A9 and finally A10.
At the moment, the loop is running continuously, but only populating A6. It is not then moving to A7, A8 etc.
Any help is much apprecited.
Thanks.
Try this :
Sub Tester()
Dim wb As Workbook
Dim ws As Worksheet
Dim LastRow As Long
Dim AnyData As Integer
Set wb = ThisWorkbook
Set ws = Sheets("Pipeline")
ws.Activate
AnyData = WorksheetFunction.CountA(ws.Range("B2:B10000"))
If AnyData = 0 Then
Exit Sub
Else
GoTo Continue:
End If
Continue:
LastRow = ws.Range("B" & ws.Rows.Count).End(xlUp).Row
ws.Range("A2").Formula = "=IF(B2="""","""",""OK"")"
ws.Range("A2").AutoFill Range("A2:A" & LastRow)
End Sub
You could then call the sub when opening the workbook.
In ThisWorkbook add :

Formula in VBA, putting a formula in a range of cells but excluding certain cells

I have code that will go to a range and insert a formula all the way down. The below code will go to range N17:N160 and insert a relative formula.
Sub Macro9()
Range("n17:n160").FormulaR1C1 = "=IF(RC[-11]="""","""",IFERROR(VLOOKUP(RC[-11],R17C42:R160C53,7,FALSE),""""))"
End Sub
It works. However, I need to add a level where it looks at range B17:B160 and any cell in that range that has a "Y" in it will not update with the formula, whereas any one without a "Y", will update. Basically if and row has a Y in column B, don't put the formula in, for every other cell in the range, do put the formula in.
Thanks
First time posting, sorry if I didn't format everything correctly.
You'll need to build a range reference that meest your criteria for adding the Formula,
Something like
Sub Macro9()
Dim rng As Range, rngFiltered As Range
Dim dat As Variant
Dim idx As Long
Set rng = Range("n17:n160")
dat = rng.Offset(0, -12).Value2 ' Column B data
rng.ClearContents ' Clear existing data from range
For idx = 1 To UBound(dat)
If dat(idx, 1) <> "y" Then
If rngFiltered Is Nothing Then
Set rngFiltered = rng.Cells(idx, 1)
Else
Set rngFiltered = Application.Union(rngFiltered, rng.Cells(idx, 1))
End If
End If
Next
rngFiltered.FormulaR1C1 = "=IF(RC[-11]="""","""",IFERROR(VLOOKUP(RC[-11],R17C42:R160C53,7,FALSE),""""))"
End Sub

Copy & Paste Non-Contiguous Range Based on Criteria

I need to select a range of data in column Q that meet criteria found in column A (specifically, I wish to select only those cells which correspond to non-"" values in column A). The resulting range of selected cells will be non-contiguous.
I then want to copy these cells and paste their values in column K. The pasted values should retain the same row references as the copied range; basically, I'm just taking the values in the copied range and pasting the values x many columns to the left.
The problem I'm encountering is that it seems to only copy the final value in column Q and then paste this value in column K. So, I seem to be getting it to paste in the right place, but it's not copying the way I want it to.
The code I've written can be found below.
Option Explicit
Sub NonConRngPaste()
Dim rCell As Range
Dim rRng As Range
Dim r As Range
Dim pasteRng As Range
For Each rCell In Range("A1:A1000")
If rCell.Value <> "" Then
If rRng Is Nothing Then
Set rRng = rCell
Else
Set rRng = Application.Union(rRng, rCell)
End If
End If
Next
Set pasteRng = rRng.Offset(0, 10)
For Each r In rRng.Offset(0, 16).Cells
pasteRng.Value = r.Value
Next
End Sub
May as well be simpler?
Sub NonConRngPaste()
Dim c As Range
Application.Screenupdating = False
For Each c In Range("A1:A1000").cells
If len(c.value) > 0 Then
with c.EntireRow
.Columns("K").Value = .Columns("Q").Value
end with
End If
Next
End Sub

Compare sheets to copy differences

I have this working VBA code that compares 2 sheets and copies duplicates to the 'Changes' sheet. I need to do the opposite. I need to copy differences to the changes sheet.
Sub CompareSheets()
Dim Sht1Rng As Range
Dim Sht2Rng As Range
Set Sht1Rng = Worksheets("RXCP Order").Range("A1", Worksheets("RXCP Order").Range("A1000").End(xlUp))
Set Sht2Rng = Worksheets("QS1 Order").Range("A1", Worksheets("QS1 Order").Range("A1000").End(xlUp))
For Each c In Sht1Rng
Set d = Sht2Rng.Find(c.Value, LookIn:=xlValues)
If Not d Is Nothing Then
Worksheets("Changes").Range("A1000").End(xlUp).Offset(1, 0).Value = c.Value
Worksheets("Changes").Range("A1000").End(xlUp).Offset(0, 1).Value = c.Offset(0, 1).Value
Set d = Nothing
End If
Next c
End Sub
One way to find changes from one worksheet to another is to add sheet3 with the following formula in each cell of the used range of sheet1:
=if(Sheet1!A1<>Sheet2!A1,1,0)
Then add a column to sheet3 adding across the row.
Any rows with a count greater than zero should be copied.
You can filter then copy paste manually or run your for loop down the count column in sheet three and copy the row from sheet two based on the row it is on in sheet 3
dim aCell as range
with thisworkbook
for each acell in .sheets("Sheet3").Range("Z1:Z1000")
if acell.value > 0 then
.sheets("Sheet1").range("A" & acell.row).entirerow.copy _
.sheets("Sheet4").range("A" & .sheets("Sheet4").usedrange.rows.count + 1)
end if
next acell
end with
You can write a macro to copy the formula above to all the cells in the sheet3's used range, and copy the formula down the next available column also.

In reference to "Copy a row in excel if it matches a specific criteria into a new worksheet"

In reference to: Copy a row in excel if it matches a specific criteria into a new worksheet
I attempted applying the above hyperlink code to the needs of my own workbook. The only notable differences are: Object names, My data begins in "A2" instead of "A1", and my data is being copied to "L" column in a new worksheet instead of "A" column
Also... you can assume I have generated tabs in excel that correspond with each SelectCell.Value.
Sub Consolidate_Sheets()
Dim MyCell As Range
Dim MyRange As Range
Dim ws As Worksheet
Set MyRange = Sheets("Install_Input").Range("A2")
Set MyRange = Range(MyRange, MyRange.End(xlDown))
Call superSizeMe(MyCell, MyRange)
Sub superSizeMe(SelectCell As Range, SelectRange As Range)
Dim InstallInput As Worksheet
Dim strPasteToSheet As String
'New worksheet to paste into
Dim DestinationSheet As Worksheet
Dim DestinationRow As Range
'Define worksheet with input data
Set InstallInput = ThisWorkbook.Worksheets("Install_Input")
For Each SelectCell In SelectRange.Cells
InstallInput.Select
If SelectCell.Value <> "" Then
SelectCell.EntrieRow.Select ''''LOCATION OF RUN-TIME ERROR 438''''
Selection.Copy
Set DestinationSheet = Worksheets(SelectCell.Value)
Set DestinationRow = DestinationSheet.Range("L1:L" & DestinationSheet.Cells(Rows.Count, "L").End(xlUp).Row)
Range("L" & DestinationRow.Rows.Count + 1).Select
ActiveSheet.Paste
End If
Next SelectCell
InstallInput.Select
InstallInput.Cells(1, 1).Select
If IsObject(InstallInput) Then Set InstallInput = Nothing
If IsObject(SelectRange) Then Set SelectRange = Nothing
If IsObject(SelectCell) Then Set SelectCell = Nothing
If IsObject(DestinationSheet) Then Set DestinationSheet = Nothing
If IsObject(DestinationRow) Then Set DestinationRow = Nothing
End Sub
I am getting a Run-time error'438'
"Object doesn't support this property or method" on "SelectCell.EntireRow.Select"
Well your code has a typo
SelectCell.EntrieRow.Select
should say entire not Entrie. Personally I would use this method anyway, It selects the entire row based on the number you put in. FYI there is also a corresponding Columns().select if you need it in the future
sel_cell_row = SelectCell.Row
Rows(sel_cell_row).select
edit addressed to comment
The reason you get the 1004 error is like it says, the copy and paste areas don't match. Think of copying 10 rows, and trying to paste it into 2 rows, simply wouldn'y work. I'm guessing the problem actually stems from your destinationrows code. I'm not entirely sure what its trying to do, but here are two generic fixes
1)keep the copy code as it is, and modify the paste. Instead of selecting a range of cells to paste into, select the first cell (if your range was a1:a10, selecting a1 is sufficient) excel will then paste all the data starting at that first cell. so in your code do this
'comment out all this destination row stuff
'Set DestinationRow = DestinationSheet.Range("L1:L" & DestinationSheet.Cells(Rows.Count, "L").End(xlUp).Row)
'Range("L" & DestinationRow.Rows.Count + 1).Select
Range("L1").select 'only referencing the first cell to paste into
ActiveSheet.Paste
2)rather than selecting an entire row, why not select only the populated values in that row something like
sel_cell_row = SelectCell.Row
lastColumn = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column
range(Cells(sel_cell_row ,1),Cells(sel_cell_row ,lastColumn )).select
then do your copy as usual. the 1 is for column 1, or A. I'm assuming the data you want is in one row starting at column A and going till lastColumn. Maybe now this will match your destinationrows code.
3)Com,bine options 1 and 2. so copy only the populated cells, and paste to the first cell in the range

Resources