I wanted to reach out on a concern i am getting with with my loop. i need to copy all trainee data in the class list sheet from rows 13 on and put it into a roster registry sheet same workbook. however the code i wrote initially is giving me and error when i use .Range(i, 1) so i changed it to .Cells. Main concern is that the code writes the data into the roster registry sheet but only the last trainee data.
Option Explicit
Sub ExportcReg()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim wbk1 As Workbook
Dim s1, cReg As Worksheet
Dim x, i, FinalRow As Long
Dim thisvalue As String
Set wbk1 = Workbooks.Open(ThisWorkbook.Worksheets("Info").Range("A1").Value)
Set s1 = wbk1.Sheets("ClassRegistry")
Set cReg = ThisWorkbook.Worksheets("Class Registry")
With cReg
' Find the last row of data in Column "A"
FinalRow = .Cells(.Rows.Count, "A").End(xlUp).Row
' Loop through each row
For x = 2 To FinalRow
' Decide if to copy based on column A
thisvalue = .Cells(x, 1).Value
If thisvalue <> "" Then
i = s1.Cells(s1.Rows.Count, 1).End(xlUp).Row + 1
s1.Cells(i, 1).Value = thisvalue
s1.Cells(i, 2).Value = .Cells(x, 2).Value
s1.Cells(i, 3).Value = .Cells(x, 3).Value
s1.Cells(i, 4).Value = .Cells(x, 4).Value
s1.Cells(i, 5).Value = .Cells(x, 5).Value
s1.Cells(i, 6).Value = .Cells(x, 5).Value
s1.Cells(i, 7).Value = .Cells(x, 7).Value
s1.Cells(i, 8).Value = .Cells(x, 8).Value
s1.Cells(i, 9).Value = .Cells(x, 9).Value
s1.Cells(i, 10).Value = .Cells(x, 10).Value
s1.Cells(i, 11).Value = .Cells(x, 11).Value
s1.Cells(i, 12).Value = .Cells(x, 12).Value
s1.Cells(i, 13).Value = .Cells(x, 13).Value
s1.Cells(i, 14).Value = .Cells(x, 14).Value
s1.Cells(i, 15).Value = .Cells(x, 15).Value
s1.Cells(i, 16).Value = .Cells(x, 16).Value
s1.Cells(i, 17).Value = .Cells(x, 17).Value
s1.Cells(i, 18).Value = .Cells(x, 18).Value
End If
Next x
End With
wbk1.Close savechanges:=True
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
You have too much logical and technical errors in your code which I cannot explain line by line. Besides you can achieve your goal with less coding. Maybe what you need is a working code to understand how things work. Take a look at this:
Sub CListtoRReg()
'From Class List to Roster Registry
Dim ws1 As Worksheet: Set ws1 = Worksheets("Class List")
Dim ws2 As Worksheet: Set ws2 = Worksheets("Roster Registry")
Dim i, j As Long
For i = 13 To ws1.Cells(1, 1).End(xlDown).Row
ws2.Cells(1, 1).End(xlDown).Offset(1) = ws1.Range("C2").Value
For j = 2 To 11
ws2.Cells(1, 1).End(xlDown).Offset(0, j - 1) = ws1.Cells(i, j - 1).Value
Next j
Next i
End Sub
Related
I have two hardware devices that record data and I need to sync the times recorded by each so the data matches on both devices.
The times are close but not always identical: I record data every 0.2 seconds, but sometimes one device will have a slightly larger or smaller gap.
Currently, I import the times from L unit and R unit into excel, then round the times to the nearest 0.1 sec. That way, the times either match exactly or are off by 0.1 sec (which is close enough for my purposes).
I wrote a VBA script (below) to paste the data from the R unit into the L unit. It works fine, but it is too slow for the amount of data I am dealing with (25,000+ rows)
I hoping someone can examine the code and suggest a faster way to do the same thing.
Sub NewTimesComparisonLoop()
Application.ScreenUpdating = False
Dim LBottomRow As Long
Dim RBottomRow As Long
Dim LSheet As Worksheet
Dim Rsheet As Worksheet
Dim LStartCell As Range
Dim RStartcell As Range
Dim Li As Long
Dim Ri As Long
Set LSheet = Worksheets("Sheet1")
Set Rsheet = Worksheets("Sheet2")
'find the last row of times in column b
Set LStartCell = Range("B1")
LBottomRow = LSheet.Cells(LSheet.Rows.Count, LStartCell.Column).End(xlUp).row
Set RStartcell = Range("B1")
RBottomRow = Rsheet.Cells(Rsheet.Rows.Count, RStartcell.Column).End(xlUp).row
'get data set of sheet1, column B
'LSheet.Range(StartCell, LSheet.Cells(BottomRow, 2)).Select
'loop through each R value, comparing against a loop of L values
'if they match, or if R is under by 0.1 sec, copy the R values into columns j through P
For Ri = 1 To RBottomRow
For Li = 1 To LBottomRow
If Sheets("Sheet2").Cells(Ri, 2).Value = Sheets("Sheet1").Cells(Li, 2).Value Then
Sheets("Sheet1").Cells(Li, 10).Value = Sheets("Sheet2").Cells(Ri, 3).Value
Sheets("Sheet1").Cells(Li, 11).Value = Sheets("Sheet2").Cells(Ri, 4).Value
Sheets("Sheet1").Cells(Li, 12).Value = Sheets("Sheet2").Cells(Ri, 5).Value
Sheets("Sheet1").Cells(Li, 13).Value = Sheets("Sheet2").Cells(Ri, 6).Value
Sheets("Sheet1").Cells(Li, 14).Value = Sheets("Sheet2").Cells(Ri, 7).Value
Sheets("Sheet1").Cells(Li, 15).Value = Sheets("Sheet2").Cells(Ri, 8).Value
Sheets("Sheet1").Cells(Li, 16).Value = Sheets("Sheet2").Cells(Ri, 9).Value
ElseIf Sheets("Sheet2").Cells(Ri, 2).Value + 0.1 = Sheets("Sheet1").Cells(Li, 2).Value Then
Sheets("Sheet1").Cells(Li, 10).Value = Sheets("Sheet2").Cells(Ri, 3).Value
Sheets("Sheet1").Cells(Li, 11).Value = Sheets("Sheet2").Cells(Ri, 4).Value
Sheets("Sheet1").Cells(Li, 12).Value = Sheets("Sheet2").Cells(Ri, 5).Value
Sheets("Sheet1").Cells(Li, 13).Value = Sheets("Sheet2").Cells(Ri, 6).Value
Sheets("Sheet1").Cells(Li, 14).Value = Sheets("Sheet2").Cells(Ri, 7).Value
Sheets("Sheet1").Cells(Li, 15).Value = Sheets("Sheet2").Cells(Ri, 8).Value
Sheets("Sheet1").Cells(Li, 16).Value = Sheets("Sheet2").Cells(Ri, 9).Value
End If
Next Li
Next Ri
Application.ScreenUpdating = True
End Sub
Use a collection when matching values. Here I use a Scripting.Dictionary.
Sub NewTimesComparisonLoop()
Application.ScreenUpdating = False
Dim cell As Range, dict As Object
Set dict = CreateObject("Scripting.Dictionary")
With Sheets("Sheet2")
For Each cell In .Range("B1", .Range("B" & .Rows.Count).End(xlUp))
If Not dict.Exists(cell.Value) Then dict.Add cell.Value, cell.Offset(0, 1).Resize(1, 7).Value
Next
End With
With Sheets("Sheet1")
For Each cell In .Range("B1", .Range("B" & .Rows.Count).End(xlUp))
If dict.Exists(cell.Value) Then
cell.Offset(0, 1).Resize(1, 7).Value = dict(cell.Value)
ElseIf dict.Exists(cell.Value + 0.1) Then
cell.Offset(0, 1).Resize(1, 7).Value = dict(cell.Value + 0.1)
End If
Next
End With
Application.ScreenUpdating = True
End Sub
Technically, this type of post belongs in CodeReview.SE.
But I don't know how to vote to migrate it there, so here's what should be a significantly smaller code... how it compares in regards to execution time is a little up in the air, but it ought to be faster as well.
Replace this:
For Ri = 1 To RBottomRow
For Li = 1 To LBottomRow
If Sheets("Sheet2").Cells(Ri, 2).Value = Sheets("Sheet1").Cells(Li, 2).Value Then
Sheets("Sheet1").Cells(Li, 10).Value = Sheets("Sheet2").Cells(Ri, 3).Value
Sheets("Sheet1").Cells(Li, 11).Value = Sheets("Sheet2").Cells(Ri, 4).Value
Sheets("Sheet1").Cells(Li, 12).Value = Sheets("Sheet2").Cells(Ri, 5).Value
Sheets("Sheet1").Cells(Li, 13).Value = Sheets("Sheet2").Cells(Ri, 6).Value
Sheets("Sheet1").Cells(Li, 14).Value = Sheets("Sheet2").Cells(Ri, 7).Value
Sheets("Sheet1").Cells(Li, 15).Value = Sheets("Sheet2").Cells(Ri, 8).Value
Sheets("Sheet1").Cells(Li, 16).Value = Sheets("Sheet2").Cells(Ri, 9).Value
ElseIf Sheets("Sheet2").Cells(Ri, 2).Value + 0.1 = Sheets("Sheet1").Cells(Li, 2).Value Then
Sheets("Sheet1").Cells(Li, 10).Value = Sheets("Sheet2").Cells(Ri, 3).Value
Sheets("Sheet1").Cells(Li, 11).Value = Sheets("Sheet2").Cells(Ri, 4).Value
Sheets("Sheet1").Cells(Li, 12).Value = Sheets("Sheet2").Cells(Ri, 5).Value
Sheets("Sheet1").Cells(Li, 13).Value = Sheets("Sheet2").Cells(Ri, 6).Value
Sheets("Sheet1").Cells(Li, 14).Value = Sheets("Sheet2").Cells(Ri, 7).Value
Sheets("Sheet1").Cells(Li, 15).Value = Sheets("Sheet2").Cells(Ri, 8).Value
Sheets("Sheet1").Cells(Li, 16).Value = Sheets("Sheet2").Cells(Ri, 9).Value
End If
Next Li
Next Ri
With this:
For Ri = 1 To RBottomRow
For Li = 1 To LBottomRow
If ("Sheet2").Cells(Ri, 2).Value - Sheets("Sheet1").Cells(Li, 2).Value <= 0.1 Then _
Sheets("Sheet1").Range("J" & Li & ":P" & Li).Value = _
("Sheet2").Range("C" & Ri & ":I" & Ri).Value
Next Li
Next Ri
I have three sheets, sheet S, Sheet P and Sheet Data.
I first copy the column of Sheet S to Sheet Data. Then in column E of sheet Data, I look for the ID. The ID In column E of data sheet, matches with the column A of P sheet, then I copy the corresponding ID.
The problem here is the Sheet data contains 214 rows, while sheet P contains 1110.
while comparing the ID, there are two different ID from row 870 and 871, which are not copied, even though they are same.
Could someone guide what could be the reason ?
Sub lookup()
Dim lLastrow, totalrows As Long
Dim rng As Range
Dim i As Long
'Copy lookup values from S to Data
With Sheets("S")
lLastrow = .Cells(.Rows.count, 1).End(xlUp).Row
.Range("P5:P" & lLastrow).Copy Destination:=Sheets("Data").Range("E5")
.Range("G5:G" & lLastrow).Copy Destination:=Sheets("Data").Range("H5")
End With
totalrows = Sheets("P").Cells(Sheets("P").Rows.count, "A").End(xlUp).Row
For i = 5 To lLastrow
'Search for the value on P_APQP
With Sheets("P")
Set rng = .Columns(1).Find(Sheets("Data").Cells(i, 5).Value & "*", lookat:=xlWhole)
End With
'If it is found put its value on the destination sheet
If Not rng Is Nothing Then
With Sheets("Data")
.Cells(i, 6).Value = rng.Value
.Cells(i, 1).Value = rng.Offset(0, 1).Value
.Cells(i, 2).Value = rng.Offset(0, 2).Value
.Cells(i, 3).Value = rng.Offset(0, 3).Value
.Cells(i, 4).Value = rng.Offset(0, 9).Value
.Cells(i, 9).Value = rng.Offset(0, 10).Value
.Cells(i, 13).Value = rng.Offset(0, 6).Value
.Cells(i, 14).Value = rng.Offset(0, 5).Value
.Cells(i, 15).Value = rng.Offset(0, 4).Value
.Cells(i, 16).Value = rng.Offset(0, 8).Value
End With
End If
Next i
End Sub
I'll post the whole code. I also made an adjustment to your first line of declarations - as you had it, only totalrows was being declared as Long. You have to spell each one out I'm afraid.
Sub lookup()
Dim lLastrow As Long, totalrows As Long
Dim rng As Range
Dim i As Long
With Sheets("S")
lLastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range("P5:P" & lLastrow).Copy Destination:=Sheets("Data").Range("E5")
.Range("G5:G" & lLastrow).Copy Destination:=Sheets("Data").Range("H5")
End With
totalrows = Sheets("P").Cells(Sheets("P").Rows.Count, "A").End(xlUp).Row
For i = 5 To lLastrow
'Search for the value on P_APQP
With Sheets("P")
'amended below
Set rng = .Columns(1).Find(Trim(Sheets("Data").Cells(i, 5).Value) & "*", lookat:=xlWhole)
End With
'If it is found put its value on the destination sheet
If Not rng Is Nothing Then
With Sheets("Data")
.Cells(i, 6).Value = rng.Value
.Cells(i, 1).Resize(, 3).Value = rng.Offset(0, 1).Value
.Cells(i, 2).Value = rng.Offset(0, 2).Value
.Cells(i, 3).Value = rng.Offset(0, 3).Value
.Cells(i, 4).Value = rng.Offset(0, 9).Value
.Cells(i, 9).Value = rng.Offset(0, 10).Value
.Cells(i, 13).Value = rng.Offset(0, 6).Value
.Cells(i, 14).Value = rng.Offset(0, 5).Value
.Cells(i, 15).Value = rng.Offset(0, 4).Value
.Cells(i, 16).Value = rng.Offset(0, 8).Value
End With
End If
Next i
End Sub
Sub CreateTableD()
Dim WB As Workbook
Dim WS1 As Worksheet
Dim WS2 As Worksheet
Dim i As Long
Dim k As Long
'Dim n As Long
Set WB = Excel.ActiveWorkbook
Set WS1 = WB.Worksheets("List1")
Set WS2 = WB.Worksheets("List2")
i = 1
k = 1
'While Not IsEmpty(WS1.Cells(i, 1))
Do While WS1.Cells(i, 1).Value <> ""
If (WS1.Cells(i, 4).Value = "Depo" And WS1.Cells(i, 8).Value = "CZK") Then
WS2.Cells(k + 1, 4).Value = WS1.Cells(i, 7).Value
WS2.Cells(k + 2, 4).Value = WS1.Cells(i, 7).Value
WS2.Cells(k + 1, 7).Value = "79010000"
WS2.Cells(k + 2, 7).Value = "79010000"
ElseIf (WS1.Cells(i, 4).Value = "Loan" And WS1.Cells(i, 8).Value = "CZK") Then
WS2.Cells(k + 1, 4).Value = WS1.Cells(i, 7).Value
WS2.Cells(k + 2, 4).Value = WS1.Cells(i, 7).Value
WS2.Cells(k + 1, 7).Value = "75010000"
WS2.Cells(k + 2, 7).Value = "75010000"
k = k + 2
End If
i = i + 1
'Wend
Loop
Range("D1").Select
ActiveCell.FormulaR1C1 = "CZK"
End Sub
Hi. I have a code, but it doesnt work properly. If two conditions are satisfied it must return interest on another worksheet and also some static data( which is in the code) I've shown the right result on second picture.
first worksheet with conditions
on this picture i showed what i need to get
The problem is that you are only incrementing k when it is a loan.
If (WS1.Cells(i, 4).Value = "Depo" And WS1.Cells(i, 8).Value = "CZK") Then
ElseIf (WS1.Cells(i, 4).Value = "Loan" And WS1.Cells(i, 8).Value = "CZK") Then
k = k + 2
End If
Incrementing k when eith condition is True will fix the problem.
If (WS1.Cells(i, 4).Value = "Depo" And WS1.Cells(i, 8).Value = "CZK") Then
k = k + 2
ElseIf (WS1.Cells(i, 4).Value = "Loan" And WS1.Cells(i, 8).Value = "CZK") Then
k = k + 2
End If
I usually create a separate function to handle adding data to a table. Breaking up the code into smaller units helps simplify debugging.
Here is how I would write it.
Sub CreateTableD()
Dim x As Long
With Worksheets("List1")
For x = 2 To .Range("D" & .Rows.Count).End(xlUp).Row
If .Cells(x, 8).Value = "CZK" Then
If .Cells(x, 4).Value = "Depo" Then
AddList2Entry .Cells(x, 7).Value, "79010000"
AddList2Entry .Cells(x, 7).Value, "79010000"
ElseIf .Cells(x, 4).Value = "Loan" Then
AddList2Entry .Cells(x, 7).Value, "75010000"
AddList2Entry .Cells(x, 7).Value, "75010000"
End If
End If
Next
End With
End Sub
Sub AddList2Entry(interest As Double, StaticValue As Double)
Dim newRow As Long
With Worksheets("List2")
newRow = .Range("D" & .Rows.Count).End(xlUp).Row + 1
.Cells(newRow, "D").Value = interest
.Cells(newRow, "G").Value = StaticValue
End With
End Sub
I have create 2 sub function like this:
Sub Product1()
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Inventory")
lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
With ws
If IsEmpty(UserForm5.ComboBox5.Value) Then
Exit Sub
Else
.Cells(lRow, 1).Value = UserForm5.TextBox1.Value
.Cells(lRow, 2).Value = UserForm5.ComboBox2.Value
.Cells(lRow, 3).Value = UserForm5.ComboBox3.Value
.Cells(lRow, 4).Value = UserForm5.ComboBox4.Value
.Cells(lRow, 5).Value = UserForm5.ComboBox1.Value
.Cells(lRow, 6).Value = UserForm5.ComboBox5.Value
.Cells(lRow, 7).Value = UserForm5.TextBox2.Value
.Cells(lRow, 8).Value = UserForm5.TextBox5.Value
.Cells(lRow, 9).Value = UserForm5.TextBox6.Value
.Cells(lRow, 10).Value = UserForm5.TextBox4.Value
.Cells(lRow, 11).Value = UserForm5.TextBox7.Value
End If
End With
End Sub
Sub Product2()
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Inventory")
lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
With ws
If IsEmpty(UserForm5.ComboBox6.Value) Then
Exit Sub
Else
.Cells(lRow, 1).Value = UserForm5.TextBox1.Value
.Cells(lRow, 2).Value = UserForm5.ComboBox2.Value
.Cells(lRow, 3).Value = UserForm5.ComboBox3.Value
.Cells(lRow, 4).Value = UserForm5.ComboBox4.Value
.Cells(lRow, 5).Value = UserForm5.ComboBox1.Value
.Cells(lRow, 6).Value = UserForm5.ComboBox6.Value
.Cells(lRow, 7).Value = UserForm5.TextBox9.Value
.Cells(lRow, 8).Value = UserForm5.TextBox11.Value
.Cells(lRow, 9).Value = UserForm5.TextBox12.Value
.Cells(lRow, 10).Value = UserForm5.TextBox10.Value
.Cells(lRow, 11).Value = UserForm5.TextBox8.Value
End If
End With
End Sub
I was wondering that, if my combobox6 is empty, it should not transfer the data for to exel sheet.
What I faced now is if the combobox6 is empty( did not select any value), it will still copy all the data to the excel sheet.
Is there any way to fix it?
Change
If IsEmpty(UserForm5.ComboBox5.Value) Then
to
If UserForm5.ComboBox5.Value = "" Then
And make the same sort of change to the Product2 sub.
If the combo box is "empty" then checking its value will give you an empty string.
Sub Product1()
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Inventory")
lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
With ws
If UserForm5.ComboBox5.Value <> "" Then
Exit Sub
Else
.Cells(lRow, 1).Value = UserForm5.TextBox1.Value
.Cells(lRow, 2).Value = UserForm5.ComboBox2.Value
.Cells(lRow, 3).Value = UserForm5.ComboBox3.Value
.Cells(lRow, 4).Value = UserForm5.ComboBox4.Value
.Cells(lRow, 5).Value = UserForm5.ComboBox1.Value
.Cells(lRow, 6).Value = UserForm5.ComboBox5.Value
.Cells(lRow, 7).Value = UserForm5.TextBox2.Value
.Cells(lRow, 8).Value = UserForm5.TextBox5.Value
.Cells(lRow, 9).Value = UserForm5.TextBox6.Value
.Cells(lRow, 10).Value = UserForm5.TextBox4.Value
.Cells(lRow, 11).Value = UserForm5.TextBox7.Value
End If
End With
End Sub
Sub Product2()
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Inventory")
lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
With ws
If UserForm5.ComboBox6.Value <> "" Then
Exit Sub
Else
.Cells(lRow, 1).Value = UserForm5.TextBox1.Value
.Cells(lRow, 2).Value = UserForm5.ComboBox2.Value
.Cells(lRow, 3).Value = UserForm5.ComboBox3.Value
.Cells(lRow, 4).Value = UserForm5.ComboBox4.Value
.Cells(lRow, 5).Value = UserForm5.ComboBox1.Value
.Cells(lRow, 6).Value = UserForm5.ComboBox6.Value
.Cells(lRow, 7).Value = UserForm5.TextBox9.Value
.Cells(lRow, 8).Value = UserForm5.TextBox11.Value
.Cells(lRow, 9).Value = UserForm5.TextBox12.Value
.Cells(lRow, 10).Value = UserForm5.TextBox10.Value
.Cells(lRow, 11).Value = UserForm5.TextBox8.Value
End If
End With
End Sub
I am attempting to do the following:
Look at the first row's column B (Event) from entiresalespipeline
Match Column B to a table (Events_and_Activities) that links a date to each event
If that date is in the future, copy the entire row into the next empty row in a third worksheet (CurrentSalesPipeline)
Repeat this process until there are no more filled rows in the first spreadsheet.
I have created the following code, which when I mouse-over the text appears to give correct data, but which gives me the following error:
Run-time error '13': Type mismatch
Sub ShowUpcoming_Click()
Dim rCell As Range
Dim ws As Worksheet
Dim DateConf As Long
For Each rCell In Sheet1.Range("B3:B5000")
Set ws = Worksheets("CurrentSalesPipeline")
DateConf = Application.VLookup(rCell, Worksheets("Events_and_Activities").Range("A2:B12"), 2, False)
Range("A1").Value = DateConf
If CDate(DateConf) >= CDate((Date)) Then
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
With ws
.Cells(iRow, 1).Value = Worksheets("entiresalespipeline").Range("A3:A3").Value
.Cells(iRow, 2).Value = Worksheets("entiresalespipeline").Range("B3:B3").Value
.Cells(iRow, 3).Value = Worksheets("entiresalespipeline").Range("C3:C3").Value
.Cells(iRow, 4).Value = Worksheets("entiresalespipeline").Range("D3:D3").Value
.Cells(iRow, 5).Value = Worksheets("entiresalespipeline").Range("E3:E3").Value
.Cells(iRow, 6).Value = Worksheets("entiresalespipeline").Range("F3:F3").Value
.Cells(iRow, 7).Value = Worksheets("entiresalespipeline").Range("G3:G3").Value
.Cells(iRow, 8).Value = Worksheets("entiresalespipeline").Range("H3:H3").Value
.Cells(iRow, 9).Value = Worksheets("entiresalespipeline").Range("I3:I3").Value
.Cells(iRow, 10).Value = Worksheets("entiresalespipeline").Range("J3:J3").Value
.Cells(iRow, 11).Value = Worksheets("entiresalespipeline").Range("K3:K3").Value
.Cells(iRow, 12).Value = Worksheets("entiresalespipeline").Range("L3:L3").Value
.Cells(iRow, 13).Value = Worksheets("entiresalespipeline").Range("M3:M3").Value
.Cells(iRow, 14).Value = Worksheets("entiresalespipeline").Range("N3:N3").Value
.Cells(iRow, 15).Value = Worksheets("entiresalespipeline").Range("O3:O3").Value
.Cells(iRow, 16).Value = Worksheets("entiresalespipeline").Range("P3:P3").Value
.Cells(iRow, 17).Value = Worksheets("entiresalespipeline").Range("Q3:Q3").Value
.Cells(iRow, 18).Value = Worksheets("entiresalespipeline").Range("R3:R3").Value
.Cells(iRow, 19).Value = Worksheets("entiresalespipeline").Range("S3:S3").Value
.Cells(iRow, 20).Value = Worksheets("entiresalespipeline").Range("T3:T3").Value
End With
End If
Next rCell
End Sub
You're declaring DateConf as a Long:
Dim DateConf As Long
And then assigning it the result of the VLookup call:
DateConf = Application.VLookup(rCell, Worksheets("Events_and_Activities").Range("A2:B12"), 2, False)
That's a lot of assumptions to make: you're relying on VBA to perform an implicit conversion of the returned value to a Long, without knowing if the returned value will be a valid numeric.
What if the VLookup returns an empty string? What if it returned an #N/A error value?
Dim lookupResult As Variant
lookupResult = Application.VLookup(rCell, Worksheets("Events_and_Activities").Range("A2:B12"), 2, False)
If IsNumeric(lookupResult) Then
DateConf = CLng(lookupResult)
...
End If
Reduce the number of assumptions you're making, you'll reduce the number of potential issues by as much.
Thank you everyone for your very thoughtful and extremely helpful input!
After some tinkering, I managed to come up with the following, which does everything I need it to do.
Mat's Mug - your suggestion was perfect!
Tim Williams - I tried your single line solution to the copy/pasting, but it only copied the first line (headings) over every line in the range, so I stuck with what I had.
Sub ShowUpcoming_Click()
Dim lastrow As Long
Dim ws As Worksheet
Dim DateConf As Long
Dim r As Long
Set ws = Worksheets("CurrentSalesPipeline")
Dim lookupresult As Variant
'find last completed row of entire spreadsheet
lastrow = Worksheets("EntireSalesPipeline").Range("B" & Rows.Count).End(xlUp).Row
'From the first completed line (row 3) to last completed row
For r = 3 To lastrow
'lookup conference date from events/activities spreadsheet
lookupresult = Application.VLookup(Worksheets("EntireSalesPipeline").Cells(r, 2).Value, Worksheets("Events_and_activities").Range("A2:B13"), 2, False)
If IsNumeric(lookupresult) Then
DateConf = CLng(lookupresult)
End If
'If vlookup finds a date, then check to make sure it is in the future from when the button was hit.
If CDate(DateConf) >= CDate((Date)) Then
'If it is a future event, then copy that data into the current spreadsheet
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
With ws
.Cells(iRow, 1).Value = Worksheets("entiresalespipeline").Cells(r, 1).Value
.Cells(iRow, 2).Value = Worksheets("entiresalespipeline").Cells(r, 2).Value
.Cells(iRow, 3).Value = Worksheets("entiresalespipeline").Cells(r, 3).Value
.Cells(iRow, 4).Value = Worksheets("entiresalespipeline").Cells(r, 4).Value
.Cells(iRow, 5).Value = Worksheets("entiresalespipeline").Cells(r, 5).Value
.Cells(iRow, 6).Value = Worksheets("entiresalespipeline").Cells(r, 6).Value
.Cells(iRow, 7).Value = Worksheets("entiresalespipeline").Cells(r, 7).Value
.Cells(iRow, 8).Value = Worksheets("entiresalespipeline").Cells(r, 8).Value
.Cells(iRow, 9).Value = Worksheets("entiresalespipeline").Cells(r, 9).Value
.Cells(iRow, 10).Value = Worksheets("entiresalespipeline").Cells(r, 10).Value
.Cells(iRow, 11).Value = Worksheets("entiresalespipeline").Cells(r, 11).Value
.Cells(iRow, 12).Value = Worksheets("entiresalespipeline").Cells(r, 12).Value
.Cells(iRow, 13).Value = Worksheets("entiresalespipeline").Cells(r, 13).Value
.Cells(iRow, 14).Value = Worksheets("entiresalespipeline").Cells(r, 14).Value
.Cells(iRow, 15).Value = Worksheets("entiresalespipeline").Cells(r, 15).Value
.Cells(iRow, 16).Value = Worksheets("entiresalespipeline").Cells(r, 16).Value
.Cells(iRow, 17).Value = Worksheets("entiresalespipeline").Cells(r, 17).Value
.Cells(iRow, 18).Value = Worksheets("entiresalespipeline").Cells(r, 18).Value
.Cells(iRow, 19).Value = Worksheets("entiresalespipeline").Cells(r, 19).Value
.Cells(iRow, 20).Value = Worksheets("entiresalespipeline").Cells(r, 20).Value
End With
End If
'Repeat for next line in existing
Next r
End Sub
This is a great community. Thank you again!
Sarah