Change destination between 2 sheets

KlausW

Active Member
Joined
Sep 9, 2020
Messages
379
Office Version
  1. 2016
Platform
  1. Windows
Hi, I have a challenge.

I use these two codes. The first to transfer data between the sheet Tilmelding and the sheet Tid.

The other code to show what data there is in the sheet Tid. This is shown in the sheet Tilmelding.

It works as it should

What I would like is for data from the sheet Tilmelding to be displayed from cell CW3 instead of cell M3 as it does now in the sheet Tid.

Any help will be appreciated.

Best regards

Klaus W

VBA Code:
Sub Code_1()

Dim DatRng, Dest As Range
Dim TidCol, TidRow, c  As Integer

 With Sheets("Tilmelding")
    Set DatRng = .Range("C4:C10")
 On Error GoTo Ooops
    TidCol = Application.Match(.Range("A2"), Sheets("Tid").Range("1:1"), 0)
    TidRow = Application.Match(.Range("B4"), Sheets("Tid").Range("C:C"), 0)
 End With
 
For c = 0 To 2
    Set Dest = Sheets("Tid").Cells(TidRow, TidCol).Offset(0, c).Resize(7, 1)
    Dest.Value = DatRng.Offset(0, 2 * c).Value
 Next c
 
Ooops:
 If Not Err.Number = 0 Then MsgBox " Not able to match Initial or Date  -- Please check and try again"
On Error GoTo 0

Code_2

End Sub

VBA Code:
Sub Code_2()

'If Target.Cells.Count > 1 Then Exit Sub

'If Intersect(Target, Range("A2", "G2")) Is Nothing Then Exit Sub


Dim WkRng, DestRng, SrcRng As Range

Dim TidCol, TidRow, c  As Integer


 With Sheets("Tilmelding")

    Set WkRng = .Range("B4:B10")  'Dates for week number

    Set DestRng = .Range("C4:C10")  'Required qty range

   

    On Error GoTo Ooops  'Error handler

    'TidCol = first column of initial

    'TidRow = first row of week number

       TidCol = Application.Match(.Range("A2"), Sheets("Tid").Range("1:1"), 0)

       TidRow = Application.Match(.Range("G2"), Sheets("Tid").Range("B:B"), 0)

 End With

Application.EnableEvents = False  'Stop this change event code triggereing itself and looping forever

'change the dates to match week number

WkRng.Value = Sheets("Tid").Cells(TidRow, 3).Resize(7, 1).Value


'Loop using offset to get 3 sets of data from Tid to cols C E G

For c = 0 To 2

    Set SrcRng = Sheets("Tid").Cells(TidRow, TidCol).Offset(0, c).Resize(7, 1)

    DestRng.Offset(0, 2 * c).Value = SrcRng.Value

Next c

 

Ooops:  'Error message if there is error.

 If Not Err.Number = 0 Then MsgBox " Not able to match Initial or Week Number  -- Please check and try again"


On Error GoTo 0  'set error handling back to default

Application.EnableEvents = True  're-enable events handling


End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
What I would like is for data from the sheet Tilmelding to be displayed from cell CW3 instead of cell M3 as it does now in the sheet Tid.
Is the change only in code1?


Try (The changes are highlighted in blue):
Rich (BB code):
Sub Code_1()
  Dim DatRng, Dest As Range
  Dim TidCol, TidRow, c  As Integer, col As Long
  
  With Sheets("Tilmelding")
    Set DatRng = .Range("C4:C10")
    On Error GoTo Ooops
    TidCol = Application.Match(.Range("A2"), Sheets("Tid").Range("1:1"), 0)
    TidRow = Application.Match(.Range("B4"), Sheets("Tid").Range("C:C"), 0)
  End With
   
  col = Columns("CW").Column
  For c = 0 To 2
    Set Dest = Sheets("Tid").Cells(TidRow, col).Offset(0, c).Resize(7, 1)
    Dest.Value = DatRng.Offset(0, 2 * c).Value
  Next c
   
Ooops:
  If Not Err.Number = 0 Then MsgBox " Not able to match Initial or Date  -- Please check and try again"
  On Error GoTo 0
  
  Call Code_2
End Sub
 
Upvote 0
Solution
Is the change only in code1?


Try (The changes are highlighted in blue):
Rich (BB code):
Sub Code_1()
  Dim DatRng, Dest As Range
  Dim TidCol, TidRow, c  As Integer, col As Long
 
  With Sheets("Tilmelding")
    Set DatRng = .Range("C4:C10")
    On Error GoTo Ooops
    TidCol = Application.Match(.Range("A2"), Sheets("Tid").Range("1:1"), 0)
    TidRow = Application.Match(.Range("B4"), Sheets("Tid").Range("C:C"), 0)
  End With
  
  col = Columns("CW").Column
  For c = 0 To 2
    Set Dest = Sheets("Tid").Cells(TidRow, col).Offset(0, c).Resize(7, 1)
    Dest.Value = DatRng.Offset(0, 2 * c).Value
  Next c
  
Ooops:
  If Not Err.Number = 0 Then MsgBox " Not able to match Initial or Date  -- Please check and try again"
  On Error GoTo 0
 
  Call Code_2
End Sub
Good morning DanteAmor it works as it should, thank you very much. Have a nice day from Denmark Klaus W
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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