VBA: FindString needs to be more efficient

gd6noob

Board Regular
Joined
Oct 20, 2017
Messages
170
Office Version
  1. 2016
Platform
  1. Windows
Hi everyone,

I have some code but I think it can be better coded.

Basically I'm looking to find string value "Bereavement", "Provincial Leave", "Medical Waiver" and if not found it will paste the missing string into the next blank cell. I have the FindString= wirtten 3 times for all 3 string values and it is probably not the best way, seeing if someone has a better option. The range would be A1:D1, as I have other data in other columns.

APT v5.0.xlsb
ABCD
1Row LabelsBereavementProvincial LeaveMedical Waiver
212340Not Used
312340Not Used
41234Not Used7
51234Not Used9
61234Not Used7
PL Balances 1


VBA Code:
Sub Find_First()
Dim FindString As String
Dim Rng As Range
Dim LastColumn As Variant
Dim sht As Worksheet

Set sht = ThisWorkbook.Sheets("PL Balances 1")

FindString = "Medical Waiver"
    With Sheets("PL Balances 1").Range("A1:D1")
        Set Rng = .Find(What:=FindString, _
                        After:=.Cells(.Cells.Count), _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False)
        If Not Rng Is Nothing Then

        Else
            With sht
                LastColumn = .Range("A1").End(xlToRight).Column
            End With
                sht.Cells(1, LastColumn).Offset(0, 1).Value = "Medical Waiver"
        End If
    End With

Thank you.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try this:

VBA Code:
Sub find_string()
  Dim arr As Variant
  Dim f As Range
  Dim i As Long
 
  arr = Array("Bereavement", "Provincial Leave", "Medical Waiver")
  For i = 0 To UBound(arr)
    With Sheets("PL Balances 1")
      Set f = .Range("A1:D1").Find(arr(i), , xlValues, xlWhole, , , False)
      If f Is Nothing Then
        .Cells(1, Columns.Count).End(1)(1, 2).Value = arr(i)
      End If
    End With
  Next
End Sub

---
If headers always go in cells B1, C1, and D1, then:
VBA Code:
Sub macro()
  Range("B1:D1").Value = Array("Bereavement", "Provincial Leave", "Medical Waiver")
End Sub
 
Upvote 0
Solution
Try this:

VBA Code:
Sub find_string()
  Dim arr As Variant
  Dim f As Range
  Dim i As Long
 
  arr = Array("Bereavement", "Provincial Leave", "Medical Waiver")
  For i = 0 To UBound(arr)
    With Sheets("PL Balances 1")
      Set f = .Range("A1:D1").Find(arr(i), , xlValues, xlWhole, , , False)
      If f Is Nothing Then
        .Cells(1, Columns.Count).End(1)(1, 2).Value = arr(i)
      End If
    End With
  Next
End Sub

---
If headers always go in cells B1, C1, and D1, then:
VBA Code:
Sub macro()
  Range("B1:D1").Value = Array("Bereavement", "Provincial Leave", "Medical Waiver")
End Sub
Thank you very much for your help.
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,913
Members
449,093
Latest member
dbomb1414

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