Vba macro

FGaxha

Board Regular
Joined
Jan 10, 2023
Messages
221
Office Version
  1. 365
Platform
  1. Windows
I have a macro , extracting rows from sheet1 based on name “James” column T2:T7500, if V2:V7500 <=12 column V, range A2:V7500 .
Can i create orher three macros for the same range but names: John, Tim, George?
Also is any limitation on a workbook how many macros can be used?
Thank you.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Can i create orher three macros for the same range but names: John, Tim, George?
Probably yes, but since you did not post your macro code no one can say for sure.

Also is any limitation on a workbook how many macros can be used?
Mostly no. Though there are a few special case situations, mainly having to do with event code.
 
Upvote 0
This is my macro:

Sub test3()
Dim outarr()
inarr = Range("A1:V7500")
ReDim outarr(1 To 7500, 1 To 22)
indi = 1
For i = 2 To 7500
If UCase(inarr(i, 10)) = "OES" And UCase(inarr(i, 20)) = "JAMES" And inarr(i, 22) <= 12 Then
' copy row
For j = 1 To 22
outarr(indi, j) = inarr(i, j)
Next j
indi = indi + 1
End If
Next i
Worksheets.Add
ActiveSheet.Name = "James"
If indi > 1 Then
Range(Cells(1, 1), Cells(indi - 1, 22)) = outarr
End If
End Sub
 
Upvote 0
Can i create orher three macros for the same range but names: John, Tim, George?
While many refinements are possible, the simplest mod is to add some variable parameters to your existing Test3 macro and then call it from other macros.

VBA Code:
Sub TestJames()
   test3 "OES", "James"
End Sub

Sub TestJohn()
   test3 "OES", "John"
End Sub

Sub TestTim()
   test3 "OES", "Tim"
End Sub

Sub TestGeorge()
   test3 "OES", "George"
End Sub

Sub test3(Category As String, FirstName As String)
    Dim outarr()
    Dim inarr As Variant, indi As Long, I As Long, J As Long                                 '<<<< missing variable declarations.

    inarr = Range("A1:V7500").Value
    ReDim outarr(1 To 7500, 1 To 22)
    indi = 1
    For I = 2 To 7500
        If UCase(inarr(I, 10)) = UCase(Category) And UCase(inarr(I, 20)) = UCase(FirstName) And inarr(I, 22) <= 12 Then
            ' copy row
            For J = 1 To 22
                outarr(indi, J) = inarr(I, J)
            Next J
            indi = indi + 1
        End If
    Next I
    Worksheets.Add
    ActiveSheet.Name = FirstName
    If indi > 1 Then
        Range(Cells(1, 1), Cells(indi - 1, 22)) = outarr
    End If
End Sub

(Tip: when posting code, please try to use 'code tags' to format the code as I have done above


as it makes the code easier to read.)
 
Upvote 1
Solution

Forum statistics

Threads
1,215,745
Messages
6,126,627
Members
449,323
Latest member
Smarti1

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