VBA 'if/then' Code Error

kelsy

New Member
Joined
Jan 11, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello!

Today is my first day exploring VBA coding, and I'm having trouble with the below 'if/then' VBA code (also attached a screenshot).
Private Sub Test()


If Range("D2:D200").Value = "FedEx" Then
Range("E2:E200").Value = "Marketing"


End If

End Sub

In short, what I need is for data in Column E to automatically populate according to what is in Column D. For example, if any rows in Column D read "Fedex", then the corresponding rows in Column E will populate with the word "Marketing."

The error message I keep receiving is "Run-time error: Type '13': Type mismatch", so I thought it would be better to reach out for help rather than continue screwing up on my own.

If it's helpful, I'm currently doing this from a 2019 Macbook Pro, and my version of Excel is 16.68, licensed through Microsoft 365.

Thank you in advance for any help whatsoever!
 

Attachments

  • Screenshot 2023-01-11 at 2.43.44 PM.png
    Screenshot 2023-01-11 at 2.43.44 PM.png
    39.6 KB · Views: 4

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
If you were a bit more advanced I would use something like the autofilter so it didn't loop, but just as a basic try...

VBA Code:
Private Sub Test()
    Dim mycell As Range
    Application.ScreenUpdating = False
   
    For Each mycell In Range("D2:D200")
        If mycell.Value = "FedEx" Then mycell.Offset(, 1).Value = "Marketing"
    Next
   
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
If you were a bit more advanced I would use something like the autofilter so it didn't loop, but just as a basic try...

VBA Code:
Private Sub Test()
    Dim mycell As Range
    Application.ScreenUpdating = False
  
    For Each mycell In Range("D2:D200")
        If mycell.Value = "FedEx" Then mycell.Offset(, 1).Value = "Marketing"
    Next
  
    Application.ScreenUpdating = True
End Sub
That worked! Thanks so much for your help, I really appreciate it.

I have another question, if you have the time––when it comes to uppercase/lowercase (or in the case of the word "FedEx", all of the above), is there a specific code that can be added into something like this to make the case of X word irrelevant? e.g., instead of the code only responding to "FedEx", it would also pick up on and correct "fedex"/"FEDEX"/"feDeX"
 
Upvote 0
Rich (BB code):
Private Sub Test()
    Dim mycell As Range
    Application.ScreenUpdating = False
   
    For Each mycell In Range("D2:D200")
        If LCase(mycell.Value) = "fedex" Then mycell.Offset(, 1).Value = "Marketing"
    Next
   
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,409
Members
448,959
Latest member
camelliaCase

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