Copying Rows in For Loop and Pasting to new Sheet - excel

I am having problems copy/pasting rows based on criteria.
Dim lastrow1 As Long
Dim lastcolumn1 As Long
Dim Distance As Long
Distance = 14
Set sh = ThisWorkbook.Sheets("Sample Address Database")
Set sh2 = ThisWorkbook.Sheets("Workspace")
lastrow1 = sh.Cells(Rows.Count, "A").End(xlUp).row
lastcolumn1 = sh.Cells(1, Columns.Count).End(xlToLeft).Column
Dim L As Long
For L = 2 To lastrow1
If _
sh.Cells(L, Distance).Value <= CDbl(cboRadius.Value) Then
sh.Range("A" & L & ":" & lastcolumn1 & L).Copy _
Destination:=sh2.Range("A" & L)
End If
Next
cboRadius.Value is a number from a userform (there is no problem with that line.)
Whenever I try to run this code, I get a "Run-time error '1004': Method 'Range' of object '_Worksheet' failed, with the yellow arrow pointing to the destination line. What is the problem?
EDIT:
Ed Heywood-Lonsdale suggested I change
sh.Range("A" & L & ":" & lastcolumn1 & L).Copy _
To
sh.Range("A" & L & ":A" & lastcolumn1 & L).Copy _
Now only column A, or if I change it to B, C, D, etc. is being copied. I think the problem is that it may not be registering that lastcolumn1 and L are column/row numbers and is instead making them one value, thus causing a range malfunction.

Try adding "A" when defining the range to be copied:
sh.Range("A" & L & ":" & lastcolumn1 & L)
becomes
sh.Range("A" & L & ":A" & lastcolumn1 & L)

I would just filter your data in place using the built in Excel Filters, then copy the results over instead of trying to Loop over every row.
BUT If you want to loop the rows anyways:
In order to use the Range function you need to use column letters not column numbers.
You have 2 options here. Use
Chr(lastcolumn1 + 64)
instead of lastcolumn1. The flaw is This will only work for columns up to columns Z, and it won't work for double letter columns without an if statement and more code. Like the following should work for up to Column ZZZ
If lastcolumn1> 52 Then
strColumnLetter = Chr(Int((lastcolumn1- 1) / 52) + 64) & Chr(Int((lastcolumn1- 27) / 26) + 64) & Chr(Int((lastcolumn1- 27) Mod 26) + 65)
ElseIf lastcolumn1> 26 Then
strColumnLetter = Chr(Int((lastcolumn1- 1) / 26) + 64) & Chr(Int((lastcolumn1- 1) Mod 26) + 65)
Else
strColumnLetter = Chr(lastcolumn1+ 64)
End If
But you could also use
strColumnLetter = Split(Cells(1, lastcolumn1).EntireColumn.Address(False, False), ":")(0)
OR
strColumnLetter = Left(Replace(Cells(1, lastcolumn1).Address(1, 0), "$", ""), InStr(1, Replace(Cells(1, lastcolumn1).Address(1, 0), "$", ""), 1) - 1)
OR
strColumnLetter = Left(Cells(1, lastcolumn1).Address(1, 0), InStr(1, Cells(1, lastcolumn1).Address(1, 0), "$") - 1)
as that will work for as many columns as Excel will hold.
Your last option if you don't want to convert the number to the column Letter would be to get a range of Cells, as the Cells function CAN accept column numbers for arguments.
sh.Range(cells(L,1), cells(L,lastcolumn1))
Again I would suggest just using the standard built in filter function to filter out the data you don't want then just copy whats left though. This was just to add more options.
If you supply some sample info I could write you a sub that will do the filter copy paste for you but I don't know how your data is set up.
here is an example that should work based on your Original Question:
Sub FilterAndCopy()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Dim sh As Worksheet, sh2 As Worksheet
Dim lastrow1 As Long
Dim lastcolumn1 As Long
Dim Distance As Long
Distance = 14
Set sh = ThisWorkbook.Sheets("Sample Address Database")
Set sh2 = ThisWorkbook.Sheets("Workspace")
lastrow1 = sh.Cells(Rows.Count, "A").End(xlUp).Row
lastcolumn1 = sh.Cells(1, Columns.Count).End(xlToLeft).Column
With sh
.Range(.Cells(1, 1), .Cells(lastrow1, lastcolumn1)).AutoFilter , _
field:=Distance, _
Criteria1:="<=" & CDbl(151), _
Operator:=xlAnd
.Range(.Cells(2, 1), .Cells(lastrow1, lastcolumn1)).Copy _
sh2.Range("A2")
End With
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub

Related

If row contains any values, multiply two cells

I want column S to multiply the value of column G and column R until the last row of my data set (The length of the data set varies). Does anyone know how to go about this? Currently, I am trying things like:
Dim LastRow As Long, d As Long
LastRow = Cells(Rows.Count, "U").End(xlUp).Row
For d = 2 To LastRow
If Range("U" & d).Value = "" Then Set rw.Columns("S") = rw.Columns("F").Value * rw.Columns("R").Value * 0.01
Next d
But they don't work. It does not seem that difficult to me, but I still can't figure it out. I would really appreciate any help!
Please, try using the next code. You did not answer my clarification question, so it works on the assumption that the column S:S is filled with the multiplication result only for empty cells in column U:U. If not an empty cell, a null string will be filled. If already there are values in S:S, which must be kept, please state that and I will adapt the code to keep them:
Sub FillSSColl()
Dim sh As Worksheet, lastR As Long, rngS As Range
Set sh = ActiveSheet
lastR = sh.Range("U" & sh.rows.count).End(xlUp).row
Set rngS = sh.Range("S2:S" & lastR)
rngS.value = Application.Evaluate("=If(" & rngS.Offset(0, 2).Address(0, 0) & "= """"," & _
rngS.Offset(0, -13).Address(0, 0) & " * " & rngS.Offset(0, -1).Address(0, 0) & " * 0.01,"""")")
End Sub
But your question meaning in words does not match your code attempt...
If you want the code to calculate if row contains any value, as the title states, the code should be modified from If(" & rngS.Offset(0, 2).Address(0, 0) & "= """"," in If(" & rngS.Offset(0, 2).Address(0, 0) & "<> """",". I tried following what I could deduce looking to your code.
Edited:
Please, try the version filling all S:S column for the U:U column filled range:
Sub FillSSColl_bis()
Dim sh As Worksheet, lastR As Long, rngS As Range
Set sh = ActiveSheet
lastR = sh.Range("U" & sh.rows.count).End(xlUp).row
Set rngS = sh.Range("S2:S" & lastR)
rngS.value = Application.Evaluate(rngS.Offset(0, -13).Address(0, 0) & " * " & rngS.Offset(0, -1).Address(0, 0) & " * 0.01")
End Sub

VBA, Storing filtered values in array

My code filters out blanks and 0 records but my array is getting all values.
How can I just take into account the records filtered? Is this the best way I can do this?
Sub FilterAndCopy()
Dim LastRow As Long
Dim Arr As Variant
With Worksheets("BusinessDetails")
.Range("$A5:$AJ5").AutoFilter field:=33, Criteria1:="<>", Criteria2:="<>0", Criteria2:="<>-0"
LastRow = .Range("AG" & .Rows.Count).End(xlUp).Row
Arr = Range("AG8:AG" & LastRow)
Dim R As Long
Dim C As Long
For R = 1 To UBound(Arr, 1) ' First array dimension is rows.
For C = 1 To UBound(Arr, 2) ' Second array dimension is columns.
Debug.Print Arr(R, C)
Next C
Next R
Dim Destination As Range
Set Destination = Sheets(2).Range("D10")
Set Destination = Destination.Resize(UBound(Arr), 1)
Destination.Value = Application.Transpose(Arr)
Sheets(1).ShowAllData
End With
End Sub
Updated code:
Sub FilterAndCopy()
Dim LastRow As Long
Dim Arr As Variant
With Worksheets("BusinessDetails")
.Range("$A5:$AJ5").AutoFilter field:=33, Criteria1:="<>", Criteria2:="<>0", Criteria2:="<>-0"
LastRow = .Range("AG" & .Rows.Count).End(xlUp).Row
Set rFiltered = Range("A5:AJ" & LastRow).SpecialCells(xlCellTypeVisible)
ReDim Arr(1 To rFiltered.Areas.Count)
I = 0
For Each V In rFiltered.Areas
I = I + 1
Arr(I) = V
Next V
rFiltered.Copy Sheets("Step 4").Range("D10")
End With
End Sub
When you filter a range, you are left with different Areas.
So your choices are to read one cell at a time into the array, or one area at a time, as an array, into the Parent array.
For example, (data is in A1:C9 and the filtering is done on column A)
With Worksheets("Sheet1")
.Range("$A1:$C9").AutoFilter field:=1, Criteria1:="<>", Criteria2:="<>0"
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
Set rFiltered = Range("A1:C" & LastRow).SpecialCells(xlCellTypeVisible)
ReDim Arr(1 To rFiltered.Areas.Count)
I = 0
For Each V In rFiltered.Areas
I = I + 1
Arr(I) = V
Next V
Arr will now be an array of arrays, containing only the filtered cells.
Note
If all you want to do is copy the filtered range, then:
rFiltered.Copy Sheets("sheet2").Range("D10")
Note2
If you are always going to copy, you could then put that data into the array with something like (not tested):
arr = Sheets("sheet2").Range("D10").CurrentRegion
A possibility without the use of AutoFilter and looping:
(when you want to do more then only copying your filtered range)
Sub FilterAndCopyWithoutAutoFilter()
Dim rng As Range, adr As String, Fir As Long, y As Variant
With Worksheets("BusinessDetails")
Set rng = .Range("AG8:AG" & .Range("AG" & .Rows.Count).End(xlUp).Row)
adr = .Name & "!" & rng.Address
Fir = 7 'one less of first row number of your range
With Application
y = .Index(rng, .Transpose(Filter(.Transpose(.Evaluate("if(isnontext(" & adr & "),if(--" & adr & "<>0, row(" & adr & ")-" & Fir & ", ""##"" ),""##"")")), "##", False)), 1)
'or shorter when you want to include text values as well
'y = .Index(rng, .Transpose(Filter(.Transpose(.Evaluate("if(" _
& adr & "<>0, row(" & adr & ")-" & Fir & ", ""##"" )")), "##", False)), 1)
End With
End With
Sheets(2).Range("D10").Resize(UBound(y)).Value = y
End Sub

Is it possible to compare two columns in excel and update the master column using VBA?

I have two sheets in my excel workbook.
Contained in these sheets are my primary key columns.
I want to compare the first column (which is the master) to the second column (source) using a VBA loop.
The reason is because the source usually contains new primary keys.
Please can anyone be kind enough to help me figure out a logic to compare these columns and add the unique values to the master column.
Thank you.
this image shows the sample master code
this image shows the sample source code
The code below shows what I have so far
Sub PullUniques()
Dim rngCell As Range
For Each rngCell In Sheet1.Range("W3:W40")
If WorksheetFunction.CountIf(Range("D3:D40"), rngCell) = 0 Then
Range("C" & Rows.Count).End(xlUp).Offset(1) = rngCell
End If
Next
For Each rngCell In Sheet6.Range("D3:D40")
If WorksheetFunction.CountIf(Range("W3:W40"), rngCell) = 0 Then
Range("W" & Rows.Count).End(xlUp).Offset(1) = rngCell
End If
Next
End Sub
Try this code, please. It is based on the assumption that in source sheet there could be keys not existing in your "Master" sheet, which will be add on the first empty row of the master sheet.
Sub testMasterUpdate()
Dim shM As Worksheet, shS As Worksheet, s As Long, boolF As Boolean
Dim lastRM As Long, lastRS As Long, m As Long
Dim arrM As Variant, arrS As Variant, arrDif As Variant, d As Long
Set shM = Worksheets("Master") 'please, use here your sheet name
Set shS = Worksheets("Source") 'please, use here your sheet name
lastRM = shM.Range("A" & Cells.Rows.Count).End(xlUp).Row
lastRS = shS.Range("A" & Cells.Rows.Count).End(xlUp).Row
arrM = shM.Range("A2:A" & lastRM).value
arrS = shS.Range("A2:A" & lastRS).value
ReDim arrDif(1 To 1, 1 To UBound(arrM) + UBound(arrS)): d = 1
For s = 1 To UBound(arrS)
For m = 1 To UBound(arrM)
If arrS(s, 1) = arrM(m, 1) Then
boolF = True
Exit For
End If
Next m
If Not boolF Then
arrDif(1, d) = arrS(s, 1)
d = d + 1
End If
boolF = False
Next s
If d > 1 Then
ReDim Preserve arrDif(1 To 1, 1 To d - 1)
'shM.Range("A" & lastRM + 1).Resize(UBound(arrDif, 2), 1).value = _
WorksheetFunction.Transpose(arrDif)
shM.Range("A" & lastRM).Resize(UBound(arrDif, 2), 1).value = _
WorksheetFunction.Transpose(arrDif)
lastRM = shM.Range("A" & Cells.Rows.Count).End(xlUp).Row
shM.Range("A" & lastRM + 1).Formula = "=CountA(A2:A" & lastRM & ")"
End If
End Sub
Please, replace generic sheet names with your real ones.

How can I compare two sheets and generate a new list using VBA?

Beforehand, be aware that I just began using VBA, and I have few coding experience prior to it.
I have two sheets:
public
contacts
There is one parameter on column A that is definitely on "contacts" sheet, but may be or not be on column A on "public" sheet.
What I'm doing is:
Checking if the parameter contacts.A2 is on public.A2.
If it is, I need to copy columns, on the exact order:
public: A, C, G.
contacts: E, F.
I've found the following code online, and I'm running some adaptations to it, but I'm stuck.
Sub match()
Dim I, total, frow As Integer
Dim found As Range
total = Sheets("public").Range("A" & Rows.Count).End(xlUp).Row
'MsgBox (total) '(verifica se a contagem está ok)
For I = 2 To total
pesquisa = Worksheets("public").Range("A" & I).Value
Set found = Sheets("contacts").Columns("A:A").Find(what:=pesquisa) 'finds a match
If found Is Nothing Then
Worksheets("result").Range("W" & I).Value = "NO MATCH"
Else
frow = Sheets("contacts").Columns("A:A").Find(what:=pesquisa).Row
Worksheets("result").Range("A" & I).Value = Worksheets("public").Range("A" & frow).Value
Worksheets("result").Range("B" & I).Value = Worksheets("public").Range("C" & frow).Value
Worksheets("result").Range("C" & I).Value = Worksheets("public").Range("G" & frow).Value
Worksheets("result").Range("D" & I).Value = Worksheets("contacts").Range("F" & frow).Value
Worksheets("result").Range("E" & I).Value = Worksheets("contacts").Range("G" & frow).Value
End If
Next I
End Sub
What I expect:
to the code do ignore the line 1, as those are headers;
to eliminate de IF above, since I don't need the "NO MATCH"
to the resulting list to be ordered on ascending order, based on the A column.
Can you help me?
edited to include samples of the data and expected results:
I believe I can simplify my needs with the images above. I want to check a client on the public sheet, grab the manager contacts (emails) from the contacts sheet, and create a list that contains branch, manager, and both e-mails on the results sheet.
Creating those images, I realized I have forgotten to account for the second parameter (manager), as there can be multiple managers on a branch. So this is another parameter to account for.
`Public sheet (image)
Contacts sheet(image)
Result sheet(image)
spreadsheet
`
As per my comments, and your updated question with sample, I do believe that your current results do not match that what you say is required; which is looking for both parameters "Branch" and "Manager". Neither does your expected result look like the columns you wanted to extract according to your question. However, going by your sample data and expected output I tried the following:
Sub BuildList()
'Define your variables
Dim x As Long, y As Long
Dim arr1 As Variant, arr2 As Variant
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
'Fill 1st array variable from sheet Contacts
With Sheet1 'Change accordingly
x = .Cells(.Rows.Count, 1).End(xlUp).Row
arr1 = .Range("A2:D" & x).Value
End With
'Fill dictionary with first array
For x = LBound(arr1) To UBound(arr1)
dict.Add arr1(x, 1) & "|" & arr1(x, 2), arr1(x, 3) & "|" & arr1(x, 4)
Next x
'Fill 2nd array variable from sheet Public
With Sheet2 'Change accordingly
x = .Cells(.Rows.Count, 1).End(xlUp).Row
arr2 = .Range("A2:B" & x).Value
End With
'Compare array against dictionary and fill sheet Results
With Sheet3 'Change accordingly
y = 2
For x = LBound(arr2) To UBound(arr2)
If dict.Exists(arr2(x, 1) & "|" & arr2(x, 2)) Then
.Cells(y, 1).Value = arr2(x, 1)
.Cells(y, 2).Value = arr2(x, 2)
.Cells(y, 3).Value = Split(dict(arr2(x, 1) & "|" & arr2(x, 2)), "|")(0)
.Cells(y, 4).Value = Split(dict(arr2(x, 1) & "|" & arr2(x, 2)), "|")(1)
y = y + 1
End If
Next x
End With
End Sub
This solution makes use of arrays and dictionary which should be fast. It has given me the following result:
As David suggested, it would be better to have an input and output sample. Maybe you can try this:
Option Explicit
Public Sub match()
Dim wsPub As Worksheet
Dim wsCon As Worksheet
Dim wsRes As Worksheet
Dim pubRow As Long
Dim conRow As Long
Dim resRow As Long
Dim i As Long
Dim rng As Range
Dim cel As Range
Dim found As Long
Dim order(1 To 5) As Integer
Set wsPub = ThisWorkbook.Worksheets("public")
Set wsCon = ThisWorkbook.Worksheets("contacts")
Set wsRes = ThisWorkbook.Worksheets("result")
pubRow = wsPub.Cells(wsPub.Rows.Count, 1).End(xlUp).Row
conRow = wsCon.Cells(wsPub.Rows.Count, 1).End(xlUp).Row
resRow = wsRes.Cells(wsRes.Rows.Count, 1).End(xlUp).Row
Set rng = wsPub.Range("A2:A" & pubRow)
order(1) = 1
order(2) = 3
order(3) = 7
order(4) = 6
order(5) = 7
For Each cel In rng
If Not IsError(Application.match(cel.Value, wsCon.Range("A2:A" & conRow), 0)) Then
found = Application.match(cel.Value, wsCon.Range("A2:A" & conRow), 0) + 1
resRow = wsRes.Cells(wsRes.Rows.Count, 1).End(xlUp).Row
For i = 1 To 5
If i < 4 Then
wsRes.Cells(resRow, i).Offset(1, 0).Value _
= cel.Offset(0, order(i) - 1).Value
Else
wsRes.Cells(resRow, i).Offset(1, 0).Value _
= wsCon.Cells(found, order(i)).Value
End If
Next
End If
Next
wsRes.Range("A1").AutoFilter
wsRes.AutoFilter.Sort.SortFields.Clear
wsRes.AutoFilter.Sort.SortFields.Add2 Key:= _
Range("A1:A" & resRow), SortOn:=xlSortOnValues, order:=xlAscending, DataOption:= _
xlSortNormal
wsRes.AutoFilter.Sort.Apply
End Sub

Move Two characters from beginning to end of string VBA

I need to create a VBA script in excel that chanages an order number from having "CD" at the front to "CD" at the end so from "CD00001" to "00001CD"
Any help would be awesome. all of the order numbers are in Column B and start at row 5. please help.
What i have so far:
Private Sub OrderNumber_Click()
Dim Val As String
Dim EndC As Integer
EndC = Worksheets("Raw Data Upload").Range("A1048576").End(xlUp).Row
For i = 5 To EndC
Val = Right("B" & i, Len("B" & i) - 2) & Left("B" & i, 2)
Range("B" & i).Value = Val
Next
End Sub
This replaces the order numbers with B5, B6 and so on but if i put this function into Excel itself it works fine.
Like this? DO you want it in column B?
Option Explicit
Private Sub OrderNumber_Click()
Dim i As Long
Dim val As String
Dim EndC As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Raw Data Upload")
EndC = ws.Range("A1048576").End(xlUp).Row
For i = 5 To EndC
val = ws.Cells(i, "A")
Range("B" & i).Value = Mid$(val, 3, Len(val) - 2) & Left$(val, 2)
Next i
End Sub
dim beginStr, endStr, originalStr, outputStr as string
dim rng as range
'put the below into a loop, assigning a rng to the desired cell each time
originalStr = rng.value ' Change to chosen range
beginStr = left(originalStr,2)
endStr = right(originalStr, len(originalStr) - 2)
outputStr = endStr + beginStr
Range("B" & i).Value = outputStr
I haven't got a copy of Excel to test this on but it should work.
Simply use:
Right(Range("B" & i), Len(Range("B" & i)) - 2) & Left(Range("B" & i), 2)
An alternative is to set up the cell as a Range():
Sub t()
Dim cel As Range
Dim endC As Long
endC = Worksheets("Raw Data Upload").Range("A1048576").End(xlUp).Row
For i = 5 To endC
Set cel = Range("B" & i)
myVal = Right(cel, Len(cel) - 2) & Left(cel, 2)
Range("B" & i).Value = myVal
Next
End Sub
Currently, when you do Right("B" & i, Len("B" & i) - 2) & Left("B" & i, 2), for row 5, this becomes Right("B5", Len("B5") - 2) & Left("B5", 2) then this evaluates to simply:
Right("B5",0) & Left("B5",2), which is
[nothing] & B5, finally becoming
B5
Note the lack of using B5as a range. Instead it's being treated as a string.
(Also, I'm assuming this is to be run on the ActiveSheet. If not, please add the worksheet before the range, i.e. Worksheets("Raw Data Upload").Range("B" & i)...)
Try this
Private Sub OrderNumber_Click()
Dim cell As Range
With Worksheets("Raw Data Upload")
For Each cell in .Range("B5", .Cells(.Rows.Count, 2).End(xlUp))
cell.Value = Right(cell.Value, Len(cell.Value) - 2) & Left(cell.Value, 2)
Next
End With
End Sub

Resources