Solved

How to modify code to run macro form another sheet?

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

820 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