Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to modify code to run macro form another sheet?

Posted on 2015-01-30
4
Medium Priority
?
158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

704 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