Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

excel - VBA code to iterate through sheets and copy specific data to specific cells

Link to sample workbook:http://www.wikisend.com/download/245286/MIS%20-%20July%20%20(1).xlsm

The workbook has vba code which deletes all sheeets and then creates new sheet by copying sheets from other workbooks.

I want another vba code to automatically generate Executive Summary sheet with all formating and data.Some formulas arent well defined so they will have to be edited manually later. As other worksheets witll change every month manual referencing wud be tedious

My current code:

    Sub WorksheetLoop()

        Sheets.Add After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Select
        Sheets(Sheets.Count).Name = "Executive Summary"


        Sheets("Executive Summary").Select
'Some formatting code on sheet Executive Summary

    Dim ws As Worksheet
       ' Begin the loop.
currentRow=6 'start entering data from row 6
     For Each ws In ThisWorkbook.Worksheets

'Assign cell at row=current row and col B reference to cell G5 at sheet ws
'Assign cell at row=current row and col F reference to cell J15 at sheet ws

'Assign cell at row=current row and col H reference to cell T5 at sheet ws


'increment Current row by 1
    'don't know what to do here    
    Next ws

'select entire region( all filled cells)
'format text, add border etc

    End Sub

Please tell me syntax of referencing

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Solved the problem

Sub test()

    Sheets("Executive Summary").Select
    Range("A6:P23").Select
    Range("P6").Activate
    Selection.ClearContents
    'Dim ws As Worksheet
    Dim Row As Integer
    Row = 6
    For Each ws In ThisWorkbook.Worksheets
     If ws.Name <> "Executive Summary" Then
        Range("A" & Row).Value = "='" & ws.Name & "'!C3"
        Range("B" & Row).Value = "='" & ws.Name & "'!G5"
        Range("C" & Row).Value = "='" & ws.Name & "'!G39"
        Range("D" & Row).Value = "='" & ws.Name & "'!H39"
        Range("E" & Row).Value = "=D" & Row & "-C" & Row
        Range("F" & Row).Value = "=IF(A" & Row & "=" & Chr(34) & "POWER" & Chr(34) & ",+E" & Row & "*B" & Row & ",+E" & Row & "*B" & Row & "/100)"
        Range("G" & Row).Value = "=IF(A" & Row & "=" & Chr(34) & "POWER" & Chr(34) & ",+(J" & Row & "-D" & Row & ")*B" & Row & ",+(J" & Row & "-D" & Row & ")*B" & Row & "/100)"
        '"=(J" & Row & "-D" & Row & ")*B" & Row & "/100"
        Range("H" & Row).Value = "=I" & Row & "-F" & Row & "-G" & Row
        Range("I" & Row).Value = 0
        Range("J" & Row).Value = "='" & ws.Name & "'!I39"
        Range("K" & Row).Value = "='" & ws.Name & "'!L5"
        Range("L" & Row).Value = "='" & ws.Name & "'!K39"
        Range("M" & Row).Value = "=G" & Row
        Range("N" & Row).Value = "=O" & Row & "-L" & Row & "-M" & Row
        Range("O" & Row).Value = "=I" & Row
        Range("P" & Row).Value = "='" & ws.Name & "'!M39"
        Row = Row + 1
     End If

     Next ws

End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...