Macro in Excel

Copper Contributor

I’m new to Macro in excel and I was wondering on how to apply macro rules on previous sheets without the rule looking for the sheet name and not the order of sheets.
Example. I use excel for finance, I have sheets dated from 1-6-2019 till 30-6-2019. I’m trying to set a macro rule were it will fetch data from previous work sheet but it is always returning result from the sheet initially set in the recording process.
to clarify further-more, I’m working in sheet 4-6-2019 and I want the rule to look in the previous work sheet labelled 3-6-2019. when i run the rule on sheet 5-6-2019 the macro will still look in sheet 3-6-2019 and return result to sheet 4-6-2019.

Is there a way to solve this issue.

below are the steps and im not sure if they might help you in understanding more the issue.

 

' DS Macro
' ds
'
' Keyboard Shortcut: Ctrl+q
'
ActiveCell.Offset(0, -3).Range("A1:B114").Select
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("4-6-2019").Select                 (i want to set sheets("4-6-2019") as previous sheet so if im sheet 17-6-2019 it will fetch from sheet 16-6-2019)
ActiveCell.Offset(0, -1).Range("A1:A2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("5-6-2019").Select
ActiveCell.Offset(2, -7).Range("A1:A2").Select
ActiveSheet.Paste
Sheets("4-6-2019").Select
ActiveCell.Offset(0, 4).Range("A1:A2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("5-6-2019").Select
ActiveCell.Offset(0, 4).Range("A1:A2").Select
ActiveSheet.Paste
Sheets("4-6-2019").Select
ActiveCell.Offset(0, -3).Range("A1:A2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("5-6-2019").Select
ActiveCell.Offset(0, -3).Range("A1:A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "25726"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "-24384"
ActiveCell.Offset(15, 1).Range("A1:B1").Select

1 Reply

@Taan87 

Hi,

you don't need to select ranges and sheets. For example...

 

Sub GetDataFromSheetBefore()
Dim i As Integer


For i = 2 To Worksheets.Count

Worksheets(i).Range("A1:A2").Value = Worksheets(i - 1).Range("A1:A2").Value

Next i

End Sub

 

Regards

Bernd

www.vba-Tanker.com