Вы находитесь на странице: 1из 3

Public strFileName As String

Public currentWB As Workbook


Public dataWB As Workbook
Public strCopyRange As String
Sub GetData()
Dim strWhereToCopy As String, strStartCellColName As String
Dim strListSheet As String
strListSheet = "List"
On Error GoTo ErrH
Sheets(strListSheet).Select
Range("B2").Select
'this is the main loop, we will open the files one by one and copy their dat
a into the masterdata sheet
Set currentWB = ActiveWorkbook
Do While ActiveCell.Value <> ""
strFileName = ActiveCell.Offset(0, 1) & ActiveCell.Value
strCopyRange = ActiveCell.Offset(0, 2) & ":" & ActiveCell.Offset(0, 3)
strWhereToCopy = ActiveCell.Offset(0, 4).Value
strStartCellColName = Mid(ActiveCell.Offset(0, 5), 2, 1)
Application.Workbooks.Open strFileName, UpdateLinks:=False, ReadOnly:=Tr
ue
Set dataWB = ActiveWorkbook
Range(strCopyRange).Select
Selection.Copy
currentWB.Activate
Sheets(strWhereToCopy).Select
lastRow = LastRowInOneColumn(strStartCellColName)
Cells(lastRow + 1, 1).Select
Selection.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone
Application.CutCopyMode = False
dataWB.Close False
Sheets(strListSheet).Select
ActiveCell.Offset(1, 0).Select
Loop
Exit Sub

ErrH:
MsgBox "It seems some file was missing. The data copy operation is not compl
ete."
Exit Sub
End Sub
Public Function LastRowInOneColumn(col)
'Find the last used row in a Column: column A in this example
'http://www.rondebruin.nl/last.htm
Dim lastRow As Long
With ActiveSheet
lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
End With
LastRowInOneColumn = lastRow
End Function
--------------------------------------------------------------------------------
--------------------------------
'Loop for paste formula in blank cells till data avilable
Range( C2 ).Select
Do Until ActiveCell.Offset(0,-1).Value =
ActiveCell.FormulaR1C1 = =SUM(rc[-2]:rc[-1])
ActiveCell.Offset(1,0).Select
Loop
--------------------------------------------------------------------------------
--------------------------------------
'Loop to delete blank rows from the sheet
Range( C2 ).Select
Do Until ActiveCell. Value = End
If ActiveCell.value = then
Rows(ActiveCell.Row).Delete
Else
ActiveCell.Offset(1,0).Select
End if
Loop
--------------------------------------------------------------------------------
--------------------------------------
Sub MakeCheckerboard()
Dim R As Long, C As Long
For R = 1 To 8
If WorksheetFunction.IsOdd(R) Then
For C = 2 To 8 Step 2
Cells(R, C).Interior.Color = 255
Next C
Else
For C = 1 To 8 Step 2
Cells(R, C).Interior.Color = 255
Next C
End If
Next R
Rows( 1:8 ).RowHeight = 35
Columns( A:H ).ColumnWidth = 6.5
End Sub
--------------------------------------------------------------------------------
------------------------------------------
Sub For_Loop_With_Step()
Dim lCount As Long, lNum As Long

For lCount = 1 To 10 Step 2


lNum = lNum + 1
Next lCount

MsgBox "The For loop made " & lNum & " loop(s). lNum is equal to " & lNum
End Sub

--------------------------------------------------------------------------------
----------------------------------
Sub CountFruit()
' Get the last row with text
Dim LastRow As Long
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
Dim i As Long, Total As Long
' Use LastRow in loop
For i = 2 To LastRow
' Check if cell has text "Orange"
If Cells(i, 1).Value = "Oranges" Then
' Add value in column B to total
Total = Total + Cells(i, 2).Value
End If
Next i
' Print total
Debug.Print "Total oranges sold was:"; Total
End Sub

Вам также может понравиться