Title: Minor adjustment to VBA Module to eliminate redudancy

KP_SoCal

Board Regular
Joined
Nov 17, 2009
Messages
116
Instead of this (from code at the bottom of thread):
Code:
FinalTweeks2 targetRange, "GroupA"
FinalTweeks2 targetRange, "GroupB"
FinalTweeks2 targetRange, "GroupC"
I would like to do something like this:
Code:
FinalTweeks2 targetRange, left("Group,5")
Obviously my attempt does not work, but would appreciate any suggestions. Thanks!


Code:
Sub FinalTweeks1()

Dim targetRange As Range
' change this to where you want to work on
Set targetRange = ActiveSheet.Range("A:A")
FinalTweeks2 targetRange, "CategoryA"
FinalTweeks2 targetRange, "GroupA"
FinalTweeks2 targetRange, "GroupB"
FinalTweeks2 targetRange, "GroupC"
End Sub

Sub FinalTweeks2(targetRange As Range, what As String)

Dim found As Range, first As Range
Set first = targetRange.Find(what, After:=Range("A" & Rows.Count), LookIn:=xlValues, LookAt:=xlWhole)
If Not first Is Nothing Then
first.Resize(2, 1).EntireRow.Insert 'inserts two rows above "first"

    Set found = targetRange.FindNext(first)
    Do While (Not found Is Nothing)
        If (found.Address = first.Address) Then Exit Do
         'found.Clear  '''''clears the cell found
         'found.EntireRow.Clear
         'found.EntireColumn.Clear
         found.Resize(, 20).Clear  '''''changes the size of the range A:E
         
        Set found = targetRange.FindNext(found)
    Loop
End If
End Sub
 
Last edited:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
How doesn't it work?

Just with a quick glance it's not obvious, apart from left("Group,5").

If you want to get the 1st 5 characters try Left("Group,5",5).

If you want to do something else post an explanation of what it is.:)
 
Upvote 0
Sorry, I didn't do a very good job explaining this. Basically, I'm passing 5 different variables through FinalTweeks2. When the string of variables starts with "Group", I'm trying to avoid all the additional lines of code by having to key in:

FinalTweeks2 targetRange, "GroupA"
FinalTweeks2 targetRange, "GroupB"
FinalTweeks2 targetRange, "GroupC"

Instead of just something like, FinalTweeks2 targetRange, like "Group%"

Does this make more sense?
 
Upvote 0
That makes sense, but do you want to run FinalTweeks2 for all Groups e.g. "Group*" or do you want to Run it for 5 specific groups Groups A-E and there could be additional Groups besides the specific 5?

The reason I ask is that you could change FinalTweeks2 to run on all groups or you could send it the names of 5 groups?

This is one way you could loop FinalTweeks2 five times for five specific groups
Code:
Sub FinalTweeks1()

    Dim targetRange As Range, grp As Variant
    ' change this to where you want to work on
    Set targetRange = ActiveSheet.Range("A:A")
    For Each grp In Array("GroupA", "GroupB", "GroupC", "GroupD", "GroupE")
        FinalTweeks2 targetRange, CStr(grp)
    Next grp
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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