Hello,
I have some VBA code which copies information from one spreadsheet to another. How can I delete the first 9 rows of the data that gets pasted to the new spreadsheet? I have tried adding this line of code but it doesn't work. Am I missing some sort of linking element?
This is the working code
I have some VBA code which copies information from one spreadsheet to another. How can I delete the first 9 rows of the data that gets pasted to the new spreadsheet? I have tried adding this line of code but it doesn't work. Am I missing some sort of linking element?
VBA Code:
Rows(9).EntireRow.Delete
This is the working code
VBA Code:
Sub copy_data()
Dim data_wb As Workbook
Dim target_wb As Workbook
Dim file_name As Variant
Dim header_range(100) As Range
Dim last_row As Long
Dim col_number As Long
Dim col_letter As String
Dim counter As Long
Dim quantity As Long
'select workbook
file_name = Application.GetOpenFilename(Title:="Choose a target Workbook")
If file_name <> False Then
'create a new target workbook
Set target_wb = Application.Workbooks.Add
'open workbook with the data
Set data_wb = Application.Workbooks.Open(file_name)
'get quantity to create loop
quantity = _
InputBox("How many columns do you want to copy?")
'loop
For counter = 1 To quantity
'select header range
Set header_range(counter) = _
Application.InputBox("Select the HEADER of the " & counter & "º column you want to copy", Type:=8)
'get last row and column letter
col_number = header_range(counter).Column
last_row = Cells(Rows.Count, col_number).End(xlUp).Row
col_letter = Split(Cells(1, col_number).Address(True, False), "$")(0)
'copy from original workbook
Range(header_range(counter), Range(col_letter & last_row)).Copy
'paste in target workbook
target_wb.Sheets("Sheet1").Cells(1, counter).PasteSpecial xlPasteValues
'delete the first nine rows
Rows(9).EntireRow.Delete
Next counter
data_wb.Close
'prompt user to save the file
If Not target_wb.Saved Then
If MsgBox("Do you want to save the file?", vbYesNo, "Save?") = vbYes Then
target_wb.Save
End If
End If
End If
target_wb.Close
End Sub