chipsworld
Board Regular
- Joined
- May 23, 2019
- Messages
- 164
- Office Version
- 365
Hoping someone can see the error of my ways in the below.
THis designed to do two things...
1. If the record already exists, I simply updates it (works fine)
2. If the record doesn't exist, write it the historical sheet. (Doesn't work. Gives the "Object variable or With block variable not set" error.
Here is my code:
THis designed to do two things...
1. If the record already exists, I simply updates it (works fine)
2. If the record doesn't exist, write it the historical sheet. (Doesn't work. Gives the "Object variable or With block variable not set" error.
Here is my code:
Code:
Private Sub CommandButton1_Click()
Dim response As Integer
Dim lkup As String
Dim SourceWS As Worksheet, DestWS As Worksheet
Dim SourceRng As Range, DestCell As Range
Dim lloop As Long
Set SourceWS = Sheets("Leave Calculations") ' Source Sheet
Set DestWS = Sheets("Historical") 'Destination Sheet
lkup = Sheets("Formulas").Range("V5").Value
response = MsgBox("Are you ready to print?", vbYesNo, "PRINT SHEET?")
If response = 6 Then
'Application.Dialogs(xlDialogPrinterSetup).Show
'ActiveSheet.PrintOut
With Worksheets("historical")
Dim Rw As Integer
[I][B]Rw = .Range("B:B").Find(lkup, LookIn:=xlValues, LookAt:=xlPart, SearchDirection:=xlPrevious).Row[/B][/I] 'This is where the Debugger highlights
' Copy data to Historical form
Application.ScreenUpdating = 0
With SourceWS
Set DestCell = DestWS.Range("a" & Rows.Count).End(xlUp).Offset(1)
For lloop = 1 To 16 ' number must match total cells in range
Set SourceRng = Choose(lloop, Sheets("Formulas").Range("v4"), Sheets("Formulas").Range("v5"), Sheets("Formulas").Range("v2"), Sheets("Leave Calculations").Range("b6"), Sheets("Leave Calculations").Range("c6"), _
Sheets("Leave Calculations").Range("d6"), Sheets("Leave Calculations").Range("d11"), Sheets("Formulas").Range("v3"), Sheets("Leave Calculations").Range("e15"), Sheets("Leave Calculations").Range("e16"), Sheets("Leave Calculations").Range("e21"), _
Sheets("Formulas").Range("b39"), Sheets("Formulas").Range("b57"), Sheets("Formulas").Range("c57"), Sheets("Formulas").Range("V10"), Sheets("Formulas").Range("B1")) 'adjust the range
SourceRng.Copy
DestCell.Offset(, lloop - 1).PasteSpecial xlPasteValues
Next lloop
End With
With Application
.CutCopyMode = 0
.ScreenUpdating = 0
End With
Else:
With DestWS
.Range("A" & Rw).Value = Sheets("Formulas").Range("V4").Value
.Range("B" & Rw).Value = Sheets("Formulas").Range("V5").Value
.Range("D" & Rw).Value = Sheets("Formulas").Range("V6").Value
.Range("E" & Rw).Value = Sheets("Formulas").Range("V7").Value
.Range("F" & Rw).Value = Sheets("Formulas").Range("V8").Value
.Range("G" & Rw).Value = Sheets("Formulas").Range("v9").Value
.Range("I" & Rw).Value = Sheets("Formulas").Range("V11").Value
.Range("J" & Rw).Value = Sheets("Formulas").Range("V12").Value
.Range("H" & Rw).Value = Sheets("Formulas").Range("V3").Value
.Range("K" & Rw).Value = Sheets("Formulas").Range("V13").Value
.Range("C" & Rw).Value = Sheets("Formulas").Range("V2").Value
.Range("O" & Rw).Value = Sheets("Formulas").Range("V10").Value
End With
End If
End With
If response = 7 Then
Sheets("calc sheet").Select
End If
End If
ThisWorkbook.Save
Sheets("Leave Calculations").Visible = True
Sheets("Leave Calculations").Select
Sheets("calc sheet").Visible = False
Sheet1.CommandButton1.Activate
End Sub
/CODE]
HELP! I am going crazy trying to figure this out