Solved

How to modify code to run macro form another sheet?

Posted on 2015-01-30
4
151 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

863 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

24 Experts available now in Live!

Get 1:1 Help Now