maciej.wludyka
New Member
- Joined
- Jul 26, 2011
- Messages
- 4
Hi,
I a newbie in VBA coding, but for the purposes of company I work in, I wanted to learn it, to make my life easier. Recently I needed to code a macro gathering certain ranges of data and pasting it to a specific place. My macro looks like:
Sub NAVrecap2011_Maciej_Wludyka()
'
' Makro zwracajace pliki z zewnetrznych plikow
'
Dim a As Double
Dim b As Double
a = 1
Dim zrodlo As String
Dim zrodelko As String
b = Application.InputBox(Prompt:= _
"Wpisz od ktorego wiersza makro ma zaczac dzialac!", Type:=1)
c = Application.InputBox(Prompt:= _
"Wpisz do ktorego wiersza makro ma zaczac dzialac!", Type:=1)
zrodlo = Worksheets("2011 NAV Recap").Cells(b, 93)
zrodelko = Worksheets("2011 NAV Recap").Cells(b, 102)
Do While b < c
If Worksheets("2011 NAV Recap").Cells(3, 102) < Worksheets("2011 NAV Recap").Cells(b, 91) Then
ActiveWindow.Close
Else
End If
Beginning:
On Error GoTo Errortrap
zrodlo = Worksheets("2011 NAV Recap").Cells(b, 93)
zrodelko = Worksheets("2011 NAV Recap").Cells(b, 102)
If zrodlo = Worksheets("2011 NAV Recap").Cells(17, 93) Then
b = b + a
Else
Workbooks.Open Filename:=zrodlo
Sheets("Template 2").Select
Range("F13:H13").Select
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 50).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F16:H16").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 44).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F19:H19").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 77).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F22:H22").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 71).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F25:H25").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F28:H28").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 5).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F31:H31").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 8).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F34:H34").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 11).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F37:H37").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 14).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F40:H40").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 17).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Sheets("Template 1").Select
Range("F13:H13").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 56).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F16:H16").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 59).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F19:H19").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 62).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F22:H22").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 65).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F25:H25").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 74).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F28:H28").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 38).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F31:H31").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 41).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F34:H34").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 53).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F37:H37").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 80).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F40:H40").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 47).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Sheets("Template 3").Select
Range("F13:H13").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 29).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F16:H16").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 32).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Windows(zrodelko).Close (0)
Worksheets("2011 NAV Recap").Cells(b, 1).Select
b = b + a
End If
Loop
Exit Sub
Errortrap:
b = b + a
GoTo Beginning
End Sub
As to simplify, It just takes the data, from the certain xsl's and if there is none, go futher on. I need to convert it, as it would work on 2003 excel, which my boss has.
Thanks for all Your help in advance.
With regards,
Maciej Wludyka
I a newbie in VBA coding, but for the purposes of company I work in, I wanted to learn it, to make my life easier. Recently I needed to code a macro gathering certain ranges of data and pasting it to a specific place. My macro looks like:
Sub NAVrecap2011_Maciej_Wludyka()
'
' Makro zwracajace pliki z zewnetrznych plikow
'
Dim a As Double
Dim b As Double
a = 1
Dim zrodlo As String
Dim zrodelko As String
b = Application.InputBox(Prompt:= _
"Wpisz od ktorego wiersza makro ma zaczac dzialac!", Type:=1)
c = Application.InputBox(Prompt:= _
"Wpisz do ktorego wiersza makro ma zaczac dzialac!", Type:=1)
zrodlo = Worksheets("2011 NAV Recap").Cells(b, 93)
zrodelko = Worksheets("2011 NAV Recap").Cells(b, 102)
Do While b < c
If Worksheets("2011 NAV Recap").Cells(3, 102) < Worksheets("2011 NAV Recap").Cells(b, 91) Then
ActiveWindow.Close
Else
End If
Beginning:
On Error GoTo Errortrap
zrodlo = Worksheets("2011 NAV Recap").Cells(b, 93)
zrodelko = Worksheets("2011 NAV Recap").Cells(b, 102)
If zrodlo = Worksheets("2011 NAV Recap").Cells(17, 93) Then
b = b + a
Else
Workbooks.Open Filename:=zrodlo
Sheets("Template 2").Select
Range("F13:H13").Select
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 50).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F16:H16").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 44).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F19:H19").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 77).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F22:H22").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 71).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F25:H25").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F28:H28").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 5).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F31:H31").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 8).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F34:H34").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 11).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F37:H37").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 14).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F40:H40").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 17).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Sheets("Template 1").Select
Range("F13:H13").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 56).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F16:H16").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 59).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F19:H19").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 62).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F22:H22").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 65).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F25:H25").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 74).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F28:H28").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 38).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F31:H31").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 41).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F34:H34").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 53).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F37:H37").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 80).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F40:H40").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 47).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Sheets("Template 3").Select
Range("F13:H13").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 29).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Range("F16:H16").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Diapason NAV recap 2011.xls").Activate
Worksheets("2011 NAV Recap").Cells(b, 32).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(zrodelko).Activate
Windows(zrodelko).Close (0)
Worksheets("2011 NAV Recap").Cells(b, 1).Select
b = b + a
End If
Loop
Exit Sub
Errortrap:
b = b + a
GoTo Beginning
End Sub
As to simplify, It just takes the data, from the certain xsl's and if there is none, go futher on. I need to convert it, as it would work on 2003 excel, which my boss has.
Thanks for all Your help in advance.
With regards,
Maciej Wludyka