Copy specific row and delete blank - excel

Sub vova()
Dim S_path As String
Dim S_name1 As String, S_nameW1 As String
S_path = "S:\"
S_path = Trim(S_path) + Trim(Worksheets("1").Range("G6").Value) + ".xlsx"
Range("A1:N27").Select
Selection.Copy
Workbooks.Add
S_nameW1 = ActiveWorkbook.name
S_name1 = ActiveSheet.name
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Columns("A:A").ColumnWidth = 2
Columns("B:B").ColumnWidth = 10
Columns("C:C").ColumnWidth = 35
Columns("D:D").ColumnWidth = 13
Columns("M:M").ColumnWidth = 15
Columns("N:N").ColumnWidth = 15
Application.CutCopyMode = False
ActiveWorkbook.SaveAs FileName:=S_path, FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
End Sub
Can someone help me, need the macro to delete blank raws(if 1-5 is filled) then 6-15 is deleted and macro creates new workbook with needed raws
created workbook should look like this

You could do something like that:
Columns("N:N").ColumnWidth = 15
Application.CutCopyMode = False
With S_name1
for i = .range("A1048576").end(xlup).row to 9 Step -1
if .cells(i, 2) = "" Then
.rows(i & ":" & i).delete
End if
Next
End With
ActiveWorkbook.SaveAs FileName:=S_path, FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
This will basically loop backward from the last used cell in column A till row 9, and delete the row if there is nothing in column B (which appears to be the case in your screenshot).

Related

Name the Worksheet it's filename

I have been searching for the answer to this question and have found some helpful hints but can't make it work within this code.
I'm copying three tabs and the workbook's name is in Cover!R11 but the other two tabs from that file need the filename as well with an extension (i.e. Filename, Cover, Filename Summary, Filename Estimate). If I reference the cell with the filename when I'm on the second sheet, how can I reference the previous sheet? That's why I thought it easier to use the Filename instead. I tried using: Sheets(SheetName1).Name = FilePath but I can't figure out how to trim it within this code. Can you help?
Here's the code:
Sub CopySheets()
Dim DialogBox As FileDialog
Dim FilePath As String
Dim SheetName As String
Set DialogBox = Application.FileDialog(msoFileDialogFilePicker)
DialogBox.Title = "Select Estimates to copy " & FileType
DialogBox.AllowMultiSelect = True
DialogBox.Filters.Clear
DialogBox.Show
If DialogBox.SelectedItems.Count = 1 Then
FilePath = DialogBox.SelectedItems(1)
End If
For i = 1 To DialogBox.SelectedItems.Count
Application.ScreenUpdating = False
Application.DisplayAlerts = False
FilePath = DialogBox.SelectedItems(i)
SheetName1 = "Cover"
SheetName2 = "Summary"
SheetName3 = "Estimate and Schedule "
Set closedBook = Workbooks.Open(FilePath)
closedBook.Sheets(SheetName1).Copy
After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
Range("B2:Z97").Select
Selection.Copy
Application.CutCopyMode = False
ActiveSheet.Unprotect Password:="zxc"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'The file name I want to use is in this field, but I can't reference it for the other tabs.
Sheets(SheetName1).Name = Range("R11")
closedBook.Sheets(SheetName2).Copy
After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
Range("B5:K39").Select
Selection.Copy
Application.CutCopyMode = False
ActiveSheet.Unprotect Password:="zxc"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("D1").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'This line here didn't work to pull the previous sheet's value in R11
'Sheets(SheetName2).Name = prevname.Range("R11") & "Summary"
closedBook.Sheets(SheetName3).Copy
After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
Range("A3:M70").Select
Selection.Copy
Application.CutCopyMode = False
ActiveSheet.Unprotect Password:="zxc"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
closedBook.Close SaveChanges:=False
Next i
Application.CutCopyMode = False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
If you want to use the value from 'Cover'!R11 later in the code store it in a variable.
'The file name I want to use is in this field, but I can't reference it for the other tabs.
Dim strFilename As String
' other code
strFilename = Sheets("Cover").Range("R11").Value
Sheets(SheetName1).Name = strFilename
' more code
Sheets(SheetName2).Name = strFilename & " Summary"
' even more code
Sheets(SheetName2).Name = strFilename & " Estimate"

Delete blank rows in new workbook

Sub vova()
Dim S_path As String
Dim S_name1 As String, S_nameW1 As String
S_path = "S:\Ãîëîâíèé Áàíê\Öåíòð óïðàâë³ííÿ ãîò³âêîþ\"
S_path = Trim(S_path) + Trim(Worksheets("1").Range("D6").Value) + ".xlsx"
Range("A1:N27").Select
Selection.Copy
Workbooks.Add
S_nameW1 = ActiveWorkbook.name
S_name1 = ActiveSheet.name
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Columns("A:A").ColumnWidth = 2
Columns("B:B").ColumnWidth = 10
Columns("C:C").ColumnWidth = 35
Columns("D:D").ColumnWidth = 13
Columns("M:M").ColumnWidth = 15
Columns("N:N").ColumnWidth = 15
Application.CutCopyMode = False
With S_name1
For i = .Range("A1048576").End(xlUp).Row To 9 Step -1
If .Cells(i, 2) = "" Then
.Rows(i & ":" & i).Delete
End If
Next
End With
ActiveWorkbook.SaveAs FileName:=S_path, FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
End Sub
help solve the problem need macro to delete rows that are not field in range and create new workbook with field raws and columns. Getting this error (in column B there are drop down lists) and formulas in columns C,D,E
created workbook should look like this
If you want to store the name of the sheet to S_name1
Change
With S_name1
to
With WorkBooks(S_NameW1).WorkSheets(S_name1)
Volodymyr, you used a string variable with With keyword. You should use an object variable there.
...
Dim S_name1 as Worksheet
...
Set S_name1 = ActiveSheet
...

How do I fix 'Error: PasteSpecial method of Range class failed'?

I'm trying to write a macro that copies tables (colors, formats etc.) from the sheet for each day (Monday, Tuesday, Wednesday, Thursday and Friday) and pastes to sheets (262 sheets) for the same day. (Monday - Monday etc.) Sheets names I have in sheet "Data".
But I got this error:
Run-time error '1004': Method PasteSpecial class Range Failure.
This is my VBA macro:
Sub copy_paste()
For i = 1 To 262
If 1 = i Mod 5 Then
Worksheets("wednesday").Activate
Cells.Select
Application.CutCopyMode = False
Selection.Copy
' This is the problem part of code (said Debugger)
Sheets(Worksheets("Data").Cells(i, 2).Value).Range("A1").PasteSpecial _
Paste:=x1PasteAllUsingSourceTheme, Operation:=x1None _
, SkipBlanks:=False, Transpose:=False
End If
If 2 = i Mod 5 Then
Sheets("thursday").Select
Application.CutCopyMode = False
Selection.Copy
Sheets(Worksheets("Data").Cells(i, 2).Value).Range("A1").PasteSpecial _
Paste:=x1PasteAllUsingSourceTheme, Operation:=x1None _
, SkipBlanks:=False, Transpose:=False
End If
If 3 = i Mod 5 Then
Sheets("friday").Select
Application.CutCopyMode = False
Selection.Copy
Sheets(Worksheets("Data").Cells(i, 2).Value).Range("A1").PasteSpecial _
Paste:=x1PasteAllUsingSourceTheme, Operation:=x1None _
, SkipBlanks:=False, Transpose:=False
End If
If 4 = i Mod 5 Then
Sheets("monday").Select
Application.CutCopyMode = False
Selection.Copy
Sheets(Worksheets("Data").Cells(i, 2).Value).Range("A1").PasteSpecial _
Paste:=x1PasteAllUsingSourceTheme, Operation:=x1None _
, SkipBlanks:=False, Transpose:=False
End If
If 0 = i Mod 5 Then
Sheets("tuesday").Select
Application.CutCopyMode = False
Selection.Copy
Sheets(Worksheets("Data").Cells(i, 2).Value).Range("A1").PasteSpecial _
Paste:=x1PasteAllUsingSourceTheme, Operation:=x1None _
, SkipBlanks:=False, Transpose:=False
End If
Next i
End Sub
You can use the Worksheets("SheetName").Paste method, instead of the Range.PasteSpecial method.
But really, I'd recommend using a full up worksheet copy if you're literally copying everything:
Worksheets("wednesday").Copy After:=Worksheets(Sheets.Count)
ActiveSheet.Name = Worksheets("Data").Cells(i, 2).Value
After I fixed these two issues, it works.
You have x1 everywhere in your code instead of xl.
– Justyna MK
please check whether it should be Operation:=xlPasteSpecialOperationNone instead of Operation:=x1None
– skkakkar

Pastespecial error on first run

This sub is set up to copy info over from one worksheet and paste the values into a new CSV workbook. I keep getting a runtime error on the pastespecial, however, it's only on the first click after opening the spreadsheet, if I click it again it works perfectly. And even though it gives me an error, when i click end it still pastes the values over.
Sub export_save()
Dim nrows As Integer
Dim norders As Integer
Dim i As String
Dim cell As Range
Dim fname As String
Dim WS As Worksheet
Dim WK As Workbook
Set WK = Workbooks.Add
Dim k As Integer
Application.DisplayAlerts = False
Application.ScreenUpdating = False
k = 2
i = "DO" 'plant to plant movement
'name new file
On Error GoTo canceled
fname = InputBox("Please name the new file, exlude any filename extensions.", "Export Data")
WK.SaveAs Filename:="S:\Active Customers\Teknor Apex\Feeds\Orders\" & fname, _
FileFormat:=xlCSV
MsgBox ("File saved to file path:S:\Active Customers\Teknor Apex\Feeds\dev\" & fname)
'copy info over
Workbooks("Teknor Template dev").Worksheets("REFORMATTED").Activate
nrows = Rows(Rows.Count).End(xlUp).Row
Workbooks("Teknor Template dev").Worksheets("REFORMATTED").Range("A3:AG" & nrows).copy
WK.Activate
Range("A1:AG" & nrows).PasteSpecial xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'remove parentheses
norders = Rows(Rows.Count).End(xlUp).Row
Range("AI2").FormulaR1C1 = "=MID(RC[-14],FIND(""("",RC[-14],1)+1,3)"
Range("AI2").AutoFill Destination:=Range("AI2:AI" & norders), Type:=xlFillDefault
Range("AI2:AI" & norders).copy
Range("U2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Columns("AI:AI").Delete Shift:=xlToLeft
'remove ship paratheses in DO orders
For Each cell In Range("B2:B" & norders)
If cell.Value = i Then
Range("AI" & k).FormulaR1C1 = "=MID(RC[-13],FIND("" ("",RC[-13],1)+1,3)"
Range("AI" & k).copy
Range("V" & k).PasteSpecial xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
k = k + 1
Next cell
'delete extra column used to remove paratheses
Columns("AI:AI").Delete Shift:=xlToLeft
WK.Save
Application.ScreenUpdating = True
Application.DisplayAlerts = True
canceled:
End Sub
For clarity's sake here is a smaller version containing only the error, which is in the pastespecial line.
Workbooks("Teknor Template dev").Worksheets("REFORMATTED").Activate
nrows = Rows(Rows.Count).End(xlUp).Row
Workbooks("Teknor Template dev").Worksheets("REFORMATTED").Range("A3:AG" & nrows).copy
WK.Activate
Range("A1:AG" & nrows).PasteSpecial xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Change:
Range("A1:AG" & nrows).PasteSpecial xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
To:
Range("A1:AG" & nrows).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Your code is missing Paste:=

Saving In new File using macro in VBA

Im using the following code, but when I run it, it only saves the last part (& Day(Now) & "-" & Month(Now) & "-" & Year(Now)) of the name. Is not taking into consideration de string "Asesor". I don't know what part I'm missing:
Sub SaveFile()
Dim Asesor As String
Asesor = Range("B3").Value
Dim DestFile
DestFile = "C:\Users\ST\"
Application.ScreenUpdating = False
Workbooks("Grillas_QA.xlsm").Activate
ActiveSheet.Range("A1:K51").Select
Selection.Copy
Workbooks.Add
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
ActiveWindow.DisplayGridlines = False
Application.DisplayAlerts = False
Here's where I save the file.
ActiveWorkbook.SaveAs Filename:=DestFile & Asesor & " " & Day(Now) & "-" & Month(Now) & "-" & Year(Now), _
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
Thnx in advance for the help.
As follow up from comments, next line
Asesor = Range("B3").Value
should be changed to something like this:
Asesor = ThisWorkbook.Worksheets("Sheet1").Range("B3").Value
Also as a recommendation, I suggest you to read this post: How to avoid using Select/Active statements

Resources