VBA Command Error - Beginner's Advice

danhenshy23

New Member
Joined
Oct 3, 2016
Messages
38
Office Version
  1. 2019
Platform
  1. Windows
Hi,

I am using a template for a data entry form that I found online and have added some additional fields. Since adding the new fields I am getting the following error:

"Run-time error '1004'
Method 'Range' of object_Worksheet failed.


Below is my code. Can anyone identify the issue?

Sub UpdateLogWorksheet()


Dim historyWks As Worksheet
Dim inputWks As Worksheet


Dim nextRow As Long
Dim oCol As Long


Dim myRng As Range
Dim myCopy As String
Dim myCell As Range

'cells to copy from Input sheet - some contain formulas
myCopy = "D5,D7,D9,D11,D15,D16,D20,D21,D25,D26,D30,D31,"


Set inputWks = Worksheets("Input")
Set historyWks = Worksheets("PartsData")


With historyWks
nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
End With


With inputWks
Set myRng = .Range(myCopy)


If Application.CountA(myRng) <> myRng.Cells.Count Then
MsgBox "Please fill in all the cells!"
Exit Sub
End If
End With


With historyWks
With .Cells(nextRow, "A")
.Value = Now
.NumberFormat = "mm/dd/yyyy hh:mm:ss"
End With
.Cells(nextRow, "B").Value = Application.UserName
oCol = 3
For Each myCell In myRng.Cells
historyWks.Cells(nextRow, oCol).Value = myCell.Value
oCol = oCol + 1
Next myCell
End With

'clear input cells that contain constants
With inputWks
On Error Resume Next
With .Range(myCopy).Cells.SpecialCells(xlCellTypeConstants)
.ClearContents
Application.GoTo .Cells(1) ', Scroll:=True
End With
On Error GoTo 0
End With
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Does it give you a "Debug" option? If it does, click it and see while line of code it tells you is causing the issues.

Have you tried stepping through your code to find where the error occurs, and that the values are at that point?
 
Upvote 0
Hi,

I have a Debug option and the line of code that is highlighted is

With inputWks
Set myRng = .Range(myCopy)

It looks like it's to do with the additional sections I have added to the form, however the "mycopy" part of the code is referencing the correct cells (see below)

'cells to copy from Input sheet - some contain formulas
myCopy = "D5,D7,D9,D11,D15,D16,D20,D21,D25,D26,D30,D31,"



I'm very new to VBA, so how would I go about stepping through each part of the code?
 
Upvote 0
You have an extra comma at the end of your myCopy string (after D31).
Get rid of it, and that error will go away.
 
Upvote 0
You are welcome.

Hopefully, you now learned a debugging technique you can make use of going forward - use the method described to find which line is causing the error, and focus on that.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top