Looping macro

Beach Boy

New Member
Joined
May 25, 2018
Messages
5
I have this macro that works on the first sheet (activesheet) that I would like to work on sheets 1 and 2 the same. Could you please help advise the best way to accomplish this?

Code:
Option Explicit
 Dim cXref As Collection
 Dim rSource As Range
 Dim stDestination As String
 Dim wDestination As Workbook
 Dim sDestination As Worksheet
 Dim rDestination As Range
 Dim i As Integer, j As Integer
 Dim sPW As String

 Sub UpdateDNS()
    Set cXref = New Collection
    Call buildXREF
    stDestination = Application.GetOpenFilename()
    sPW = InputBox("Password?", "DNS Cross-Reference")
    Workbooks.Open stDestination, , , , sPW
    ActiveSheet.AutoFilterMode = False
    Range("A2").Select
    ActiveCell.CurrentRegion.Select
    Selection.Offset(1, 4).Select
    Selection.Resize(Selection.Rows.Count - 1, 1).Select
    Set rDestination = Selection

    j = rDestination.Rows.Count
    On Error Resume Next
    For i = 2 To j + 1
        If Cells(i, 9).Value = 0 Then Cells(i, 9).Value = cXref(Cells(i, 5).Value)
    Next
    On Error GoTo 0
    Err = 0

    ActiveCell.CurrentRegion.AutoFilter
    Range("A1").Select
 End Sub

 Sub buildXREF()
    Range("A2").Select
    ActiveCell.CurrentRegion.Select
    Selection.Offset(1, 0).Select
    Selection.Resize(Selection.Rows.Count - 1, 2).Select
    Set rSource = Selection
    j = rSource.Rows.Count
    For i = 2 To j + 1
        cXref.Add Cells(i, 2).Value, Cells(i, 1).Value
    Next
 End Sub
 
Last edited by a moderator:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Errant post...
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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