Delete blank rows in new workbook - 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("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
...

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"

Copy specific row and delete blank

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).

Pastespecial pasting only the first cell value across entire range

I'm trying to paste the formula results from one column to the next. I need to perform this through a macro without using a loop as it slows down the tool due to large number of records. The following methods are not working -
a) range("G5:G10").value = range("H5:H10").value
b) Selection.PasteSpecial paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
The issue is that excel copies the formula result of the first cell ("G5") to all the cells at the destination ("H5:H10"). The formula in the source is an array formula. Could anyone please share how to resolve the issue.
Edit :
Sub paste()
'
' paste Macro
'
'
Dim ColName As String, sheetname1 As String, lookup_col As String, lastrow_range As Long
ColName = "A"
sheetname1 = "Sheet1"
lookup_col = "C"
lastrow_range = 8
Worksheets("Sheet1").Range("F5").Select
ActiveCell.FormulaArray = _
"=IF(ISERROR(MATCH(TRUE,EXACT($" & ColName & ActiveCell.Row & "," & sheetname1 & "!$" & lookup_col & "$5:$" & lookup_col & "$" & lastrow_range & "),0)) , ""-" & ColName & """&"" is Invalid. This should match with any one of the values in column "" &""" & lookup_col & """&"" of "" & """ & sheetname1 & """&"". Please refer ""&""" & sheet_name & """ ,""It Matches"")"
Worksheets("Sheet1").Range("F5").Select
Selection.AutoFill Destination:=Range("F5:F8")
Range("F5:F8").Select
Selection.Copy
Range("G5").Select
Selection.PasteSpecial paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
Copy and paste special values of range
Range("H5:H10").Copy
Range("G5:G10").PasteSpecial (xlPasteValues)
Application.CutCopyMode = False
Copy and paste range
Range("H5:H10").Copy
Range("G5:G10").PasteSpecial (xlPasteAll)
Application.CutCopyMode = False
Or copy and paste the entire column
Range("H:H").Copy
Range("G:G").PasteSpecial (xlPasteAll)
Application.CutCopyMode = False
Thanks for the input.
I found that the calculation method had to be changed to 'Automatic' from 'Manual' :
Application.Calculation = xlAutomatic

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:=

Worksheet Paste values/Save as then return the original file using vba

I have a workbook which performs several Excel-functions that depend on one variable and fills out itself. I have a loop to create those forms and save in a directory but before that I want to copy all and paste special so that formulas will be gone.
Sub SaveAs1()
For i = 172 To 225
Dim SaveName As Integer
SaveName = ActiveWorkbook.Sheets(1).Range("bi1").value
Application.ActiveWorkbook.SaveAs "C:\" & SaveName
Range("bi1") = i + 1
Next
End Sub
I figure that out finally using trial and error method
Sub Save()
Rem kaydetmece dongusu
For i = 172 To 180 Step 1
Application.DisplayAlerts = False
Workbooks.Open Filename:="C:\"
Range("bi1") = i + 1
Dim SaveName As Integer
SaveName = ActiveWorkbook.Sheets(1).Range("bi1").value
Range("A1:BE63").Select
Range("a1").Activate
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Application.ActiveWorkbook.SaveAs "C:\" & SaveName
Columns("BE:BU").Select
Selection.Delete Shift:=xlToLeft
Sheets("CAL").Select
ActiveWindow.SelectedSheets.Delete
Sheets("sahadan").Select
ActiveWindow.SelectedSheets.Delete
ActiveWorkbook.Close True
Next
End Sub
If you're asking how to paste values, this is how you do it. Replace "A1" with the actual range you need to use.
Range("A1").Copy
Range("A1").PasteSpecial xlPasteValues

Resources