I am actually a vba beginner and created my first code, but I need do it more flexible ... I would like to select more excel sheets, when I use the makro. Actually I use only the sheet called "sector_share_se", but how could I run the same makro for selected sheets e.g. "sheet_1", "sheet_5" and other? Furthermore is there a way to count the filled rows in a cloumn? Then I could create a flexible range instead of Range("A400") and Range("A4:A400").
What did i do here?
I want to set the yearly data on a monthly scala. I create 11 empty rows between the filled rows and copy the monthly scale from the sheet called "MSCI". The last step is to fill the empty cells with NA's (in a way 11 NA's for each row with a value)
Sub Year_to_Month()
' emtpy space in selected sheet
Sheets("sector_share_se").Select
Dim i As Long
i = sector_share_se
For i = Range("A400").End(xlUp).Row To 5 Step -1
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Next i
' Create time frequency
Sheets("MSCI").Range("A4:A400").Copy
Sheets("sector_share_se").Range("A4:A400").PasteSpecial
' replace empty with NA
Range("A4:AI310").Select
Selection.Replace What:="", Replacement:="NA", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
Thanks in advance René
Edit with Information of @MatthewD:
' I understand iIndex as number of the excel sheet and it is possible to
' adress the correct sheet with it but how can I use here more sheets that I
' can enter only the Number (or Name) of the sheet e.g. iIndex = 1,2,3,7,9,15
Dim lRow As long
Dim iIndex As long
For iIndex = 1 To ActiveWorkbook.Worksheets.count
Set ws = Worksheets(iIndex)
For ws = Range("A400").End(xlUp).Row To 5 Step -1
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Cells(i, 1).EntireRow.Insert
Next i
' I hope that I set the maximum lenght with "ws.UsedRange.Rows.count"
' and copy it row by row
For lRow = 1 To ws.UsedRange.Rows.count
Sheets("MSCI").Range("1:lRow").Copy
Sheets("banking_sector").Range("1:lRow").PasteSpecial
Next lRow
' I think that this is quiet okay (I recorded it only), it should
' select the hole data and replace the NA's
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Replace What:="", Replacement:="NA", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False