Change one range to two ranges and one worksheet to two worksheets

Instead of looping through one worksheet “RVP Local GAAP” how do I change the below code to loop through two worksheets along with two ranges? Not only “CurrentTaxPerLocalGAAP” but also a range called CurrentTaxPerGroupGAAP in WS2 and then also pasting values into not only RVP Local GAAP but also WS2.

The below works but it checks one range and copies it into one worksheet. I want to change the range to two ranges in different worksheets.

Sub Button4_Click()

Dim strFileName As String
Dim wb1 As Workbook
Dim ws1 As Worksheet
Dim wb2 As Workbook
Dim ws2 As Worksheet
Dim cell As Range
Dim rng As Range
Dim rng2 As Range
Dim RangeName As String
Dim CellName As String
Dim ValueToFind
Dim dstRng As Range

”Set wb2 = ActiveWorkbook
”Set ws2 = wb2.Sheet(“Output”)
”ws2.Range(“D1:D12”).Copy

”Set wb1 = ActiveWorkbook

strFileName = CreateObject(“WScript.Shell”).specialfolders(“Desktop”) & “BAC GVP – Template_Update_121917.xlsm”
 
If Dir(strFileName) <> vbNullString Then
    Set wb1 = Workbooks.Open(strFileName)
Else
MsgBox “Sorry, the file does not exist on your Desktop at this time, please drop a copy to your Desktop from server!”
End If

”Set wb2 = ThisWorkbook
”Set ws2 = wb2.Sheets(“Output”)
”Set ws1 = wb1.Sheets(“RVP Local GAAP”)

”ws2.Range(“D4:D12”).Copy
”ws1.Range(“G13:G21”).PasteSpecial xlPasteValues

  ”RangeName = “myData”
  ”CellName = “G11:G83”
 
  ”Set cell = Worksheets(“RVP Local GAAP”).Range(CellName)
  ”ThisWorkbook.Names.Add Name:=RangeName, RefersTo:=cell
 
  ”RangeName = “NamedRange”
  ”CellName = “C4:C12”
 
 
Set wb2 = ThisWorkbook
Set ws2 = wb2.Sheets(“Output”)
Set ws1 = wb1.Sheets(“RVP Local GAAP”)
Set rng = Range(“CurrentTaxPerLocalGAAPProvision”)
”Set rng2 = Range(“NamedRange”)
”Set rng2 = ValueToFind
”ValueToFind = (“NamedRange”)

‘Loop through all the values in NamedRange
For Each rng In ws2.Range(“NamedRange”)
    Set dstRng = Nothing
    On Error Resume Next
    Set dstRng = ws1.Range(rng.Value)
    On Error GoTo 0
    ‘Check that the range exists in destination sheet
    If Not dstRng Is Nothing Then
        ‘Check that the range exists in the appropriate area
        If Not Intersect(dstRng, ws1.Range(“CurrentTaxPerLocalGAAPProvision”)) Is Nothing Then
           ‘Transfer the value from the next column to the appropriate range in the
           ‘destination sheet
           dstRng.Value = rng.Offset(0, 1).Value
        End If
    End If
Next

End Sub

By: jane

Leave a Reply

Your email address will not be published. Required fields are marked *