Range Issue

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows
This code is to find the array Transit in column E then replace it but for some reason, the rng variable and FirstAddress remain blank?
I could send the workbook if that helps?

VBA Code:
Option Explicit
Sub Vehicle_Name_Replace()

    Dim FirstAddress As String
    Dim MyArr As Variant
    Dim ws As Worksheet
    Dim rng As Range, DRng As Range
    Dim x As Long
    Dim i As Long
    Dim VModel As ComboBox

Set VModel = Body_And_Vehicle_Type_Form.Model_Type

Set ws = ThisWorkbook.Worksheets("Job Card Master")

    MyArr = Array("Transit")
    
    With ws.Range("E:E")

            For i = LBound(MyArr) To UBound(MyArr)


            Set rng = .Find(What:=MyArr(i), _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlFormulas, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=True)

            If Not rng Is Nothing Then
            
            FirstAddress = rng.Address
                Do
                    
                   Set DRng = rng
                    
                Select Case VModel.Value
     
                    Case ("Sprinter")
                    DRng.Value = "Sprinter"
                    
                    Case ("Master")
                    DRng.Value = "Master Movano NV400"
                    
                    Case ("Movano")
                    DRng.Value = "Master Movano NV400"
                    
                    Case ("NV400")
                    DRng.Value = "Master Movano NV400"
                    
                    Case ("Boxer")
                    DRng.Value = "Boxer Ducato Relay"
                    
                    Case ("Ducato")
                    DRng.Value = "Boxer Ducato Relay"
                    
                    Case ("Relay")
                    DRng.Value = "Boxer Ducato Relay"
                    
                    End Select
            Set rng = .FindNext(rng)
                Loop While Not rng Is Nothing And rng.Address <> FirstAddress
            End If
            
            Next i
            
    End With

End Sub
 
We are getting there the only problem is it replaces the whole line rather than just the Vehicle name.

These are some examples of what I mean
Subframe - See Drawing (Transit)
Topframe - See Drawing (Transit)
Gantry - See Drawing (Transit)
Just need the Transit vehicle name to change to Combobox value

 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
So the Find method issue is solved …​
 
Upvote 0
In order I can give it a try so according to your attachment what is the cell address you are expecting as the first one ?​
 
Upvote 0
According to your attachment this VBA demonstration well works on my side :​
VBA Code:
Sub Demo1()
           Dim Rf As Range
    With Sheet7.UsedRange.Columns(5)
           Set Rf = .Find("Transit", , xlValues, xlPart)
        If Not Rf Is Nothing Then
            Debug.Print Rf.Address
            Application.Speech.Speak "Found in cell " & Rf.Address(False, False)
            Set Rf = Nothing
        End If
    End With
End Sub
 
Upvote 0
Dim Rf As Range With Sheet7.UsedRange.Columns(5) Set Rf = .Find("Transit", , xlValues, xlPart) If Not Rf Is Nothing Then Debug.Print Rf.Address Application.Speech.Speak "Found in cell " & Rf.Address(False, False) Set Rf = Nothing
It now says Rf Range is Nothing??

My Lastest attempt is below.

 
Upvote 0
It well works on my side according to your initial attachment but not in your last one as you have changed the rules :​
look what happens with so badly and not necessary merging cells ‼​
So logically just change the source range for columns "E:F" rather than just column #5 and it works … :rolleyes:
 
Upvote 0
It`s found the first cell with Transit but won`t change the value.
There is the word Transit in various cells in Column E down to the end of the workbook.
 
Upvote 0
Don't worry about the comment above

We are getting there the only problem is it replaces the whole line rather than just the Vehicle name.
These are some examples of what I mean just change Transit to Combobox value not the rest of the values
Subframe - See Drawing (Transit)
Topframe - See Drawing (Transit)
Gantry - See Drawing (Transit)
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,305
Members
449,079
Latest member
juggernaut24

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