Loop with an if Statement

MrPink1986

Active Member
Joined
May 1, 2012
Messages
252
Hi,
At the min I have created the below sub which creates the data in the format which I require overall. However I would now like to split this data up in the loop based on criteria below into 4 different tabs based on the outcome of the statement. At the min the entire data is pasted on the Output tab.

I would like to add a new formula in the text tab for this purpose =MID(B3,20,6)
There are 4 outcomes in this formula

INTRAA
INTRAB
CLOSEA
CLOSEB

If the outcome is INTRAA these results should go into the tab of the same name and so forth.
Is it possible to include this in the code below to add the formula and then based on the value returned apply the copy and paste loop?


Code:
Sub TransposeData()   Dim Ws As Worksheet
   Dim Cl As Range
   Dim LR As Long
    
   Application.ScreenUpdating = False
    
   LR = Worksheets("Input").Range("A" & Rows.Count).End(xlUp).Row
   With Worksheets("macro")
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
        If LR > 2 Then .Range("A3").Resize(LR - 2, 25).ClearContents
        LR = Worksheets("Input").Range("A" & Rows.Count).End(xlUp).Row
        .Range("A2").Resize(LR - 1, 25).FillDown
        .Calculate
   End With
         
   Set Ws = Sheets("Text")
   Sheets("macro").UsedRange.Offset(1).Copy
   Ws.Range("A2").PasteSpecial xlPasteValues
   With Sheets("Output")
      For Each Cl In Ws.Range("A3", Ws.Range("A" & Rows.Count).End(xlUp))
         Range(Cl, Cl.End(xlToRight)).Copy
         .Range("A" & Rows.Count).End(xlUp).Offset(2).PasteSpecial , , , True
      Next Cl
   End With
   
   Sheets("Output").Select
   x = Sheets("macro").Range("R1")
   
   Cells(Rows.Count, "A").End(xlUp).Offset(2).Select
   ActiveCell.FormulaR1C1 = "EOD-OF-DATA|" & x
   
   
   Application.ScreenUpdating = True
   Range("A1").Select
   
   ActiveCell.FormulaR1C1 = "VERSION=1.0"
   ActiveCell.Offset(1).Select
   ActiveCell.FormulaR1C1 = "START-OF-DATA"
   Rows("3:3").Select
   Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
   Range("A1").Select
     
    Call DeleteEmptyTenors
   
   MsgBox "This is now complete!" & vbNewLine & "There have been " & x & " records created."
      
End Sub
 
Last edited:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,224,822
Messages
6,181,165
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