Combining 3 separate macros into 1 macro

miva0601

New Member
Joined
Mar 31, 2015
Messages
33
Hello, how can I combine these three macros so they run one after the other instead of me running them one at a time? the first macro, results in a new tab called "New". Once the first macro is ran, and the "New" tab is created, I have this tab active and run the other two macros one at a time.

VBA Code:
Sub Rearange_Order()
'
    Sheets("variable colums").Select
    i = Sheets("variable colums").Index
    Sheets.Add
    Sheets(i).Name = "New"
    Last_Col_Fixed = Sheets("fixed columns").Range("IV1").End(xlToLeft).Column
    Last_Col_Variable = Sheets("variable colums").Range("IV1").End(xlToLeft).Column
    I_Col_New = 1
    For i = 1 To Last_Col_Fixed
        Search_Header = Sheets("fixed columns").Cells(1, i)
        Sheets("variable colums").Select
            Set C = Range(Cells(1, 1), Cells(1, Last_Col_Variable)).Find(Search_Header, LookIn:=xlValues)
            If (Not (C) Is Nothing) Then
                Cells(1, C.Column).EntireColumn.Copy Sheets("New").Cells(1, I_Col_New)
                I_Col_New = I_Col_New + 1
            End If
    Next i
End Sub

2nd macro:


VBA Code:
Sub StringsCheck()
 Dim rng As Range
  For Each rng In Range("A1", Cells(1, Columns.Count).End(1))
   If InStr(rng.Value, "Rate") Or InStr(rng.Value, "percentage") Then
    With Range(Cells(2, rng.Column), Cells(Rows.Count, rng.Column).End(3))
     .Value = Evaluate("=" & .Address & " / 100")
     .Style = "Percent"
     .NumberFormat = "0.00%"
    End With
   End If
  Next rng
End Sub

3rd macro:

VBA Code:
Sub replace_on_two_columns_2()
  Dim a As Variant
  Dim i As Long
  Application.ScreenUpdating = False
  With ActiveSheet.Range("BK2:BL" & ActiveSheet.Range("BL" & Rows.Count).End(3).Row)
    a = .Value
      ReDim b(1 To UBound(a, 1), 1 To 2)
      For i = 1 To UBound(a)
        If a(i, 1) <> "" Then
          b(i, 1) = finaldata_2(replacedata(a(i, 1)))
        End If
        If a(i, 2) <> "" Then
          b(i, 2) = finaldata_2(replacedata(a(i, 2)))
        End If
      Next
    .Value = b
  End With
End Sub

Function replacedata(data)
  data = Replace(Replace(data, "_GUAR", "", , , vbTextCompare), ",", " ")
  data = Replace(data, "HEALTHMART", "HM", , , vbTextCompare)
  replacedata = Replace(data, "HEALTH MART", "HM", , , vbTextCompare)
End Function

Function finaldata_2(data)
  Dim a As Variant, itm As Variant
  Dim valores As New Collection
  Dim bex As Boolean
  Dim i As Long

  For Each itm In Split(data, " ")
    bex = False
    For i = 1 To valores.Count
      Select Case StrComp(valores(i), itm, vbTextCompare)
        Case 0
          bex = True
          Exit For
        Case 1
          bex = True
          valores.Add itm, itm, Before:=i
          Exit For
      End Select
    Next
    If bex = False Then valores.Add itm, itm
  Next
  ReDim a(1 To valores.Count)
  For i = 1 To valores.Count
    a(i) = valores(i)
  Next
  finaldata_2 = Join(a, " ")
End Function
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
The simplest way would be to create a new procedure and call the others one after the other.

VBA Code:
Sub DoSomething()
   Call Rearange_Order()
   Call StringsCheck()
   Call replace_on_two_columns_2()
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,262
Members
449,075
Latest member
staticfluids

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