Run Macro From Row Selection ?

JLouis

Active Member
Joined
Jan 1, 2004
Messages
295
Office Version
  1. 365
Platform
  1. Windows
I have this code from Mumps that uses a list of names on one sheet to find the name on another WS and hide the row. I am now trying to modify the code to select a specific range of cells after the row number has been determined from the line:
VBA Code:
    desWS.Rows(dic(v2(i, 1))).Hidden = True

In my test WB, desWS.Rows(dic(v2(i, 1))) returned row 4 as an example. I now want to select the range in row 4 "a4:i4" so I can run a routine on that range.. I've come close to setting a variable for "desWS.Rows(dic(v2(i, 1)))" but I'm not using the right syntax. Am I on the right path here or do I need to start from scratch?
Thanks for looking!

VBA Code:
Sub HideRows()
    Application.ScreenUpdating = False 
    Dim srcWS As Worksheet, desWS As Worksheet, v1 As Variant, v2 As Variant, i As Long, dic As Object
    Set srcWS = Sheets("Sheet2")
    Set desWS = Sheets("Sheet1")
    v1 = desWS.Range("A1", desWS.Range("A" & Rows.Count).End(xlUp)).Value
    v2 = srcWS.Range("A1", srcWS.Range("A" & Rows.Count).End(xlUp)).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v1) To UBound(v1)
        If Not dic.exists(v1(i, 1)) Then
            dic.Add v1(i, 1), i
        End If
    Next i
    For i = LBound(v2) To UBound(v2)
        If dic.exists(v2(i, 1)) Then
            desWS.Rows(dic(v2(i, 1))).Hidden = True
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
This code assigns A4:I4 to the variable rng.
VBA Code:
Sub HideRows()
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet, desWS As Worksheet, v1 As Variant, v2 As Variant, i As Long, dic As Object, rng As Range
    Set srcWS = Sheets("Sheet2")
    Set desWS = Sheets("Sheet1")
    v1 = desWS.Range("A1", desWS.Range("A" & Rows.Count).End(xlUp)).Value
    v2 = srcWS.Range("A1", srcWS.Range("A" & Rows.Count).End(xlUp)).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v1) To UBound(v1)
        If Not dic.exists(v1(i, 1)) Then
            dic.Add v1(i, 1), i
        End If
    Next i
    For i = LBound(v2) To UBound(v2)
        If dic.exists(v2(i, 1)) Then
            Set rng = desWS.Range("A" & dic(v2(i, 1))).Resize(, 9)
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Hi Mumps! Happy new year!

So I added the macro I wanted to run with the code, but it only works on whatever cell I had selected when the code is run. Please review below and advise.

PS: "ARedLine" is just cell formatting of the selected cells starting with:
VBA Code:
   With Selection.Font...

VBA Code:
Sub anewone()
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet, desWS As Worksheet, v1 As Variant, v2 As Variant, i As Long, dic As Object, rng As Range
    Set srcWS = Sheets("Sheet2")
    Set desWS = Sheets("Sheet1")
    v1 = desWS.Range("A1", desWS.Range("A" & Rows.Count).End(xlUp)).Value
    v2 = srcWS.Range("A1", srcWS.Range("A" & Rows.Count).End(xlUp)).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v1) To UBound(v1)
        If Not dic.exists(v1(i, 1)) Then
            dic.Add v1(i, 1), i
        End If
    Next i
    For i = LBound(v2) To UBound(v2)
        If dic.exists(v2(i, 1)) Then
            Set rng = desWS.Range("A" & dic(v2(i, 1))).Resize(, 9)
          '  MsgBox rng
        With rng
        .Application.Run "Book1!ARedLine"
        End With
        End If
     '   MsgBox i
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
I fixed it, I fixed it!

I added the code below and it worked perfectly.

VBA Code:
        If dic.exists(v2(i, 1)) Then
            Set rng = desWS.Range("A" & dic(v2(i, 1))).Resize(, 9)
            rng.Select
        With Selection
      .Application.Run "Book1!ARedLine"
        End With

Thanks again for all your help! You're awesome!
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,126
Members
453,021
Latest member
Justyna P

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