Copying rows containing a string from one workbook to another - vba

I found this piece of code that searches through the H column in a sheet and copies the cells which contain the word "apply" in a new workbook.
I then tried to change it so it would copy the entire row, but can't figure out what I'm doing wrong, as it now just opens a new workbook and leaves it empty.
Can someone look at the code and tell me what I'm doing wrong?
Many thanks
Sub test()
Dim K, X As Long, r As Range, v As Variant
K = 1
X = 5
Dim w1 As Workbook, w2 As Workbook
Set w1 = ThisWorkbook
Set w2 = Workbooks.Add
w1.Activate
For Each r In Intersect(Range("H:H"), ActiveSheet.UsedRange)
v = r.Value
X = X + 1
If InStr(v, "applied") > 0 Then
'**Initial line** - r.Copy w2.Sheets("Sheet1").Cells(K, 1)
With w2
w1.Sheets("Sheet1").Rows("X:X").Copy .Sheets("Sheet1").Rows("K")
K = K + 1
End With
End If
Next r
End Sub

There are multiple errors in your code.
You are using strings for row references. "X:X" will resolve to the string X:X. It will not substitute the value of X inside the string. Same for "K" on sheet 2.
you are copying the row five below the row in which you find "applied".
If you want to copy the same row, I would suggest:
Dim K, X As Long, r As Range, v As Variant
K = 1
Dim w1 As Workbook, w2 As Workbook
Set w1 = ThisWorkbook
Set w2 = Workbooks.Add
w1.Activate
For Each r In Intersect(Range("H:H"), ActiveSheet.UsedRange)
v = r.Value
X = X + 1
If InStr(v, "applied") > 0 Then
r.EntireRow.Copy w2.Sheets("Sheet1").Rows(K)
K = K + 1
End If
Next r
You could also change the Copy line to:
r.EntireRow.Copy w2.Sheets("Sheet1").Cells(K, 1)
but I don't know if the one is more efficient than the other.

Related

vba to search cell values in another workbook's column

I have a column "F" in workbook1 containing some values (obtained after using some excel formulas to extract and concatenate from other columns) like
blah-rd1
blah-rd5
blah-rd6
blah-rd48do I want to do this
blah-rd100
etc
I have another column "D" in workbook2 containing values like
rndm-blah-rd1_sgjgs
hjdf-blah-rd5_cnnv
sdfhjdf-blah-rd100_cfdnnv
ect
Basically "Blah-rdxx" is always present alongwith other strings in D column of workbook2
Now, what I want to do is -
If value in D column of workbook2 contains value of F column of workbook1 Then
copy corresponding value of S column of workbook2 in H column of workbook1 (5th column)
This is where I have reached so far but it doesnt copy anything probably coz there is some problem and the outer loop is not iterating, I tried following solution Nested For Next Loops: Outer loop not iterating and added n counter but still outer loop doesn't iterate -
Sub findandcopy()
Dim r As Range
Dim f As Range
Dim i As Long
Dim j As Long
Dim w1 As Worksheet
Dim w2 As Worksheet
Dim n As Integer
Set w1 = Workbooks("Book1.xlsm").Worksheets("sheet1")
Set w2 = Workbooks("Book2.xlsx").Worksheets("sheet1")
n = 0
For i = 1 To w2.Cells(Rows.Count, 1).End(xlUp).Row
For j = 1 To w1.Cells(Rows.Count, 1).End(xlUp).Row + n
If InStr(1, w2.Cells(i, 1).Value, w1.Cells(j, 3).Value) > 0 Then
w2.Cells(i, 2).Copy (w2.Cells(j, 5))
Exit For
n = n + 1
End If
Next j
Next i
End Sub
Try this
Option Explicit
Public Sub FindAndCopy()
Const F = "F"
Const D = "D"
Const H = 2
Const S = 15
Dim ws1 As Worksheet: Set ws1 = Workbooks("Book1.xlsm").Worksheets("Sheet1")
Dim ws2 As Worksheet: Set ws2 = Workbooks("Book2.xlsm").Worksheets("Sheet1")
Dim lr1 As Long: lr1 = ws1.Cells(ws1.Rows.Count, F).End(xlUp).Row
Dim lr2 As Long: lr2 = ws2.Cells(ws2.Rows.Count, D).End(xlUp).Row
Dim itm1 As Range, itm2 As Range
Application.ScreenUpdating = False
For Each itm2 In ws2.Range(ws2.Cells(1, D), ws2.Cells(lr2, D)) 'Book2
For Each itm1 In ws1.Range(ws1.Cells(1, F), ws1.Cells(lr1, F)) 'Book1
If Not IsError(itm1) And Not IsError(itm2) Then
If InStr(1, itm2.Value2, itm1.Value2) > 0 Then
itm1.Offset(, H).Formula = itm2.Offset(, S).Formula 'Book1.H = Book2.S
Exit For
End If
End If
Next
Next
Application.ScreenUpdating = True
End Sub
The original code, with explanations of functional issues:
Sub findandcopy()
Dim w1 As Worksheet, w2 As Worksheet
Dim i As Long, j As Long, n As Integer
Set w1 = Workbooks("Book1.xlsm").Worksheets("sheet1")
Set w2 = Workbooks("Book2.xlsx").Worksheets("sheet1")
n = 0
For i = 1 To w2.Cells(Rows.Count, 1).End(xlUp).Row 'for each used cell in w2.colA
For j = 1 To w1.Cells(Rows.Count, 1).End(xlUp).Row + n 'for each used cell in w1.colA
'Find the text from w1.colC (current w1 row), within cell in w2.colA (current w2 row)
If InStr(1, w2.Cells(i, 1).Value, w1.Cells(j, 3).Value) > 0 Then
'If found then copy cell in w2.colB into cell in w2.colE (current w2 row)
w2.Cells(i, 2).Copy (w2.Cells(i, 5))
Exit For 'this exits the inner For loop
n = n + 1 'this would jump over the next cell(s) in w1, but never executes
End If
Next j
Next i
End Sub
The missing indentation makes it hard to follow
There are unused variables (r, f), and w1 / w2 names can mean Workbook, or Worksheet
"Option Explicit" should be used at the top of every module
The code doesn't handle cells with errors
#N/A, #VALUE!, #REF!, #DIV/0!, #NUM!, #NAME?, or #NULL!
If you'd like a more detailed review of the code, once it's fixed you can post it on Code Review

VBA: Copying cell value in variable range

In the following code, I'm having the hardest time identifying a specific cell in the variable range "rngCell". In the "If" statement, I would like to copy a specific cell in that column or row that the rngCell (the active cell is at) instead of the value of rngCell. I've tried using offset but have been failing. Example: If rngCell is at e42, I may need a value from e2 or a42.
Thank you.
Dim rngCell As Range
Dim lngLstRow As Long
Dim ws As Worksheet, resultsWS As Worksheet
lngLstRow = ws.UsedRange.Rows.Count
Worksheets("FileShares").Select
j = 4
p = 1
q = 4
g = 6
Dim k&
For k = 9 To 50
With ws
For Each rngCell In .Range(.Cells(8, k), .Cells(lngLstRow, k))
For i = LBound(maxKeywords) To UBound(maxKeywords)
If rngCell.Value = maxKeywords(i) And rngCell.Interior.ColorIndex = 3 Then
resultsWS.Cells(g, 2).Offset(j + p, 0) = rngCell.Value
g = g + 1
j = q + p - 5 'Used to start at row 8 and every row after
End If
Next i
Next rngCell
End With
Next k
If rngCell is E42 then:
rngCell.EntireRow.Cells(1) '>>A42
rngCell.EntireColumn.Cells(2) '>>E2
or
ws.Cells(rngCell.Row, 1) '>>A42
ws.Cells(2, rngCell.Column) '>>E2

VBA: From all worksheet containing value in cell paste range

I am brand new to VBA and learning on my own. I want to make a code in which I type a number in a cell (E4, last worksheet) and when run the macro, go and search for all worksheets that match that number in cell F2 and then for each worksheet that match the number, copy a range (which is a column) and then paste all columns in a new worksheet (added at the end). I have try some of the code below in different ways and sometimes work and sometimes does not. Among other problems, the problem with the code below is that when it works it is only copying one column from one finding. If there is a better, more elegant way to write this ( and make it work) your help is appreciated.
Sub abc()
Dim wscount As Integer
Dim wb As Workbook
Set wb = ActiveWorkbook
wscount = wb.Worksheets.Count
k = 1
j = 1
If Worksheets(k).Range("F2").Value = Worksheets(wscount).Range("E4").Value Then
Worksheets(wscount + 1).Range(Cells(1, 1 + j), Cells(100, 1 + j)).Value = Worksheets(k).Range("F1:F100").Value
Worksheets(wscount + 1).Range(Cells(1, 1 + j), Cells(100, 1 + j)) = Worksheets(k).Range("F1:F100").Value
j = j + 1
End If
End Sub
You were not adding a new sheet and you were not looping among the sheets. Here's a way to do it:
Sub abc()
Dim wscount As Integer: wscount = Worksheets.Count
Dim j As Long, k As Long
Worksheets.Add After:=Worksheets(wscount)
For k = 1 To wscount - 1
If Worksheets(k).Range("F2").value = Worksheets(wscount).Range("E4").value Then
j = j + 1
Worksheets(wscount + 1).Columns(j).value = Worksheets(k).Columns("F").value
End If
Next
End Sub

Copy info and then count not NULL, Excel VBA

I have a piece of code to copy some info out of a random number of sheets (from the 7th sheet and on). as follows
Sub Controle()
Dim sh As Worksheet, N As Long
Dim i As Long, M As Long
N = Sheets.Count
M = 1
For i = 7 To N
Sheets(i).Range("E2").Copy
Sheets("Controle tabel").Cells(1, M).PasteSpecial (xlValues)
Sheets("Controle tabel").Cells(1, M).PasteSpecial (xlFormats)
M = M + 1
Next i
End Sub
I also want to count the amount on values in column A, I tried to add this
Sheets(i).Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count
but it won't run, I think I need to add how where to paste it, since it has to come under the names pasted using the code but I have no clue how this can ben done. I added a new variable L to make a list just as with M
Sheets(i).Range("A:A").Cells(1, L).SpecialCells(xlCellTypeConstants).Count
But it still won't run I think I might be using the wrong way to count no null (since you also have COUNTA, but since we don't know the names of the sheets this is hard I think)
So it also has to loop all the sheets after the 7th and count the not empty cells in column A
Guessing a little, but this?
Sub Controle()
Dim sh As Worksheet, N As Long
Dim i As Long, M As Long, L As Long
N = Sheets.Count
M = 1
For i = 7 To N
Sheets(i).Range("E2").Copy
Sheets("Controle tabel").Cells(1, M).PasteSpecial (xlValues)
Sheets("Controle tabel").Cells(1, M).PasteSpecial (xlFormats)
Sheets("Controle tabel").Cells(1, M + 1) = Sheets(i).Range("A:A").SpecialCells(xlCellTypeConstants).Count
M = M + 2
Next i
End Sub
Try this once -
Dim sh As Worksheet, N, i, m, lastrow, nullcount As Long
N = Sheets.Count
For i = 7 To N
lastrow = Sheets("Controle tabel").Range("A500000").End(xlUp).Row + 1
Sheets("Controle tabel").Range("A" & lastrow).Value = Sheets(i).Range("E2").Value
Next i
On Error Resume Next
nullcount = Application.WorksheetFunction.CountIf(Sheets("Controle tabel").Range("A:A"), "NULL")
Sheets("Controle tabel").Range("A" & lastrow).Value = nullcount

VBA paste to the same row

I am trying to figure out how to get my zero values to copy and paste to the same row. If I have a range of them from G4:G8, they paste to C1:C4, how can I get them to go directly over with out starting at the beginning of C.
Sub CopyZeroData()
Dim sh1 As Worksheet, x As Long, y As Long, N As Long, rng As Range
Set sh1 = Sheets("Ecars")
N = sh1.Cells(Rows.Count, "G").End(xlUp).Row
y = 1
For x = 1 To N
Set rng = sh1.Cells(x, "G")
If rng.Value = 0# Then
rng.Copy sh1.Cells(y, "C")
y = y + 1
End If
Next x
End Sub
Thanks!
You can use offset as well in your code. Your question, it looks like you are looping through column G, if it equals zero, you want column c to equal zero.
Sub If_Zero()
Dim sh1 As Worksheet, N As Long, rng As Range, c As Range
Set sh1 = Sheets("Ecars")
With sh1
N = .Cells(.Rows.Count, "G").End(xlUp).Row
Set rng = .Range("G1:G" & N)
End With
For Each c In rng.Cells
If c = 0 Then c.Offset(0, -4) = c
Next c
End Sub