VBA Code Run-time error '1004'

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
421
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance for your assistance. Why do I get the error "Run-time error '1004': Application-defined or object-defined error" for the following line of code

Excel Formula:
.Range("B" & i) = "=PROPER(" & CompanyName & ")"

in the following code.

Excel Formula:
Sub PrprCompNm()

Dim i As Long
Dim CompanyName As String
Dim LastRowIndex As Long

With Sheets("Index")
 LastRowIndex = .Cells.Find(What:="*", After:=.Cells(1), _
            SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End With

  'Loop - remove "<empty>" and correct names to make sure not in all caps
    For i = 2 To LastRowIndex
     With Sheets("Index")
        CompanyName = .Range("B" & i)
            
            If CompanyName = "<empty>" Then
                .Range("B" & i) = ""
                CompanyName = .Range("B" & i)
            End If
            
            .Range("B" & i) = "=PROPER(" & CompanyName & ")"
            
     End With
    Next i
    
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
If CompanyName = "<empty>" Then
What is your intention with that line?
If it is to know if the cell is empty, then it could be like this:

VBA Code:
Sub PrprCompNm()
  Dim i As Long
  Dim CompanyName As String
  Dim LastRowIndex As Long
  
  With Sheets("Index")
    LastRowIndex = .Cells.Find(What:="*", After:=.Cells(1), _
             SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  End With

  'Loop - remove "<empty>" and correct names to make sure not in all caps
  For i = 2 To LastRowIndex
    With Sheets("Index")
      CompanyName = .Range("B" & i).Value
      If CompanyName <> "" Then
        .Range("B" & i) = "=PROPER(""" & CompanyName & """)"
      End If
    End With
  Next i
End Sub
 
Upvote 0
If you are populating a formula into the cell, the string companyname has to be in quotes. The error is because your formula is invalid.

Rich (BB code):
.Range("B" & i) = "=PROPER(""" & CompanyName & """)"
 
Upvote 0
Solution
What is your intention with that line?
If it is to know if the cell is empty, then it could be like this:

VBA Code:
Sub PrprCompNm()
  Dim i As Long
  Dim CompanyName As String
  Dim LastRowIndex As Long
 
  With Sheets("Index")
    LastRowIndex = .Cells.Find(What:="*", After:=.Cells(1), _
             SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  End With

  'Loop - remove "<empty>" and correct names to make sure not in all caps
  For i = 2 To LastRowIndex
    With Sheets("Index")
      CompanyName = .Range("B" & i).Value
      If CompanyName <> "" Then
        .Range("B" & i) = "=PROPER(""" & CompanyName & """)"
      End If
    End With
  Next i
End Sub
As always Thank you @DanteAmor, but I am not having issues with that line. I am having issues with the following line:

Excel Formula:
.Range("B" & i) = "=PROPER(" & CompanyName & ")"

where @6StringJazzer recommended the following which worked:

Excel Formula:
.Range("B" & i) = "=PROPER("[B]""[/B] & CompanyName & "[B]""[/B])"
 
Upvote 0
but I am not having issues with that line
I understand that you have no problems with that line, only that I like to review all the code and make improvements in all the code. Also in my code I corrected the reported problem, but as an extra plus, I corrected all your code.
 
Upvote 0
I understand that you have no problems with that line, only that I like to review all the code and make improvements in all the code. Also in my code I corrected the reported problem, but as an extra plus, I corrected all your code.
Oh I apologize @DanteAmor. I see that. My bad.
 
Upvote 0
I understand that you have no problems with that line, only that I like to review all the code and make improvements in all the code. Also in my code I corrected the reported problem, but as an extra plus, I corrected all your code.
That's nice although it would be helpful to point that out when posting the code.
 
Upvote 0

Forum statistics

Threads
1,214,791
Messages
6,121,611
Members
449,038
Latest member
apwr

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