Hi, I am very new at VBA and have been exploring it to help automate a form I need to compile everyweek. After deleting worksheets indexed 4 and above, the code should reach in the same directory as the activework book and open specific files as outlined by source1, source2, source3, and source4. From there it should copy the first sheet of the workbook, paste it in the activeworkbook, rename it,and close the import file.
I have been slowly working out the kinks but have hit a wall. I am on Excel 2013, Windows 8.
Here is the code, any help is appreciated, this is my first time trying VBA, so I am sure that I could have done things alot differently (easier).
I have been slowly working out the kinks but have hit a wall. I am on Excel 2013, Windows 8.
Here is the code, any help is appreciated, this is my first time trying VBA, so I am sure that I could have done things alot differently (easier).
Code:
Public Sub CommandButton1_Click()
Dim directory As String
Dim fileName As String
Dim sheet As Worksheet
Dim total As Integer
Dim source1 As String
Dim source2 As String
Dim source3 As String
Dim source4 As String
Dim i As Integer
Dim storeno As String
Dim sourceno As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Do While Worksheets.Count > 3
Worksheets(4).Delete
Loop
i = 12
source1 = Worksheets("Fill Info").Range("D1").Value
source2 = Worksheets("Fill Info").Range("G1").Value
source3 = Worksheets("Fill Info").Range("J1").Value
source4 = Worksheets("Fill Info").Range("M1").Value
storeno = Worksheets("Start").Range("B1").Value
directory = ActiveWorkbook.Path
Do While i < 16
sourceno = Worksheets("Start").Cells(i, "A")
total = i - 8
fileName = directory & "\" & storeno & sourceno & ".xlsx"
Workbooks.Open (fileName)
Workbooks(fileName).Worksheets(1).Copy_
after = Workbooks(ActiveWorkbook.Name).Worksheets(4)
ActiveWorkbook.Worksheets(4).Name = sourceno
Workbooks(fileName).Close
i = i + 1
Loop
End Sub