Solved

How to modify code to run macro form another sheet?

Posted on 2015-01-30
4
149 Views
Last Modified: 2015-01-30
Sub Button4_Click()

' ****** Copies the frame contents from HPSM to Excel
Dim IE As Object
Dim shellWins As New ShellWindows

For Each IE In shellWins
    If IE.LocationName = "HP Service Manager" Then
        On Error Resume Next
        IE.ExecWB OLECMDID_SELECTALL, OLECMDEXECOPT_DODEFAULT
        IE.ExecWB OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT
        Dim MyData As DataObject
        Set MyData = New DataObject
        MyData.GetFromClipboard
        Columns("A:E").Select
        Selection.ClearContents
        Cells(1, 1).Select
        ActiveSheet.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:= _
                False, NoHTMLFormatting:=True
    End If
Next

Set shellWins = Nothing
Set IE = Nothing

On Error Resume Next

' ****** Sorts results by FST login
    Columns("A:A").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:A1000")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

' ****** Clears unneeded colums from results
    Columns("B:E").Select
    Selection.ClearContents
    Cells(1, 1).Select
    
    Dim i As Integer
    i = 1

' ****** Breaks apart copied FST & count into 2 columns
' BEFORE: Assigned: Benjamin.Pyne (5 items)
' AFTER: Benjamin.Pyne | 5
    Do While Cells(i, 1).Value <> ""
        If Left(Cells(i, 1).Value, 8) <> "Assigned" Then
            Cells(i, 1).ClearContents
        Else
            Dim uName As String
            Dim qCount As Double
            
            Cells(i, 1).Value = Replace(Cells(i, 1).Value, "Assigned: ", "")
            uName = Split(Cells(i, 1).Value, " (")(0)
            qCount = Replace(Split(Cells(i, 1).Value, " (")(1), " items)", "")
            Cells(i, 1) = uName
            Cells(i, 2) = Val(qCount)
        End If
        i = i + 1
    Loop

' ****** Adds header Row
    Range("A1:B1").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("A1").Value = "FST"
    Range("B1").Value = "Count"

End Sub

Open in new window

0
Comment
Question by:kbay808
  • 2
  • 2
4 Comments
 
LVL 28

Expert Comment

by:omgang
ID: 40580722
The code is being called from a button click.  Do you want to add a button to a different worksheet and call the same code?  Or do you want the code to process a different worksheet?  It's explicitly referencing Sheet1 now.
OM Gang
0
 

Author Comment

by:kbay808
ID: 40580809
I want to assign the macro to a button on a different sheet.  The problem is that the macro will run on that sheet an not on the target sheet.
0
 
LVL 28

Accepted Solution

by:
omgang earned 500 total points
ID: 40580882
OK
So change the code so that it references a specific sheet instead of the current one

Change
        Columns("A:E").Select
        Selection.ClearContents
        Cells(1, 1).Select
        ActiveSheet.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:= _
                False, NoHTMLFormatting:=True

To
        Sheets("Sheet1").Activate
        ActiveSheet.Columns("A:E").Select
        Selection.ClearContents
        ActiveSheet.Cells(1, 1).Select
        ActiveSheet.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:= _
                False, NoHTMLFormatting:=True

Then when you call the macro from a button on another sheet it will still process on Sheet1.
OM Gang
0
 

Author Closing Comment

by:kbay808
ID: 40580963
It worked!!!  Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now