Solved

VBA to format

Posted on 2016-07-22
3
86 Views
Last Modified: 2016-07-23
I get some data from the web which i need to format it. I created below code as new to vb i might have taken an lengthy approach.

this is what it does

Once i copy paste data in sheet 1

1.Delete Column A
2.Delete Blank Rows based on column C
3.Add a New sheet with Name "saffolla" - Can i add current day to the file name
4.Copy paste values from Sheet1 to newsheet ("Saffolla 23-07-2016")
5.Change Col to "MM/DD/YYYY" format
6. Autofit all columns in new sheet
7.Update Headings
8. delete sheet1

The challenge that i the macro have to be performed for every new sheet (current day)
and deleting sheet1

Sub FormatIt()
Columns(1).EntireColumn.Delete
 
 Application.ScreenUpdating = False
Columns("C:C").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Application.ScreenUpdating = True

Dim rng As Range
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Saffola"
Set rng = Worksheets("Sheet1").Range("A1:F2000")
Worksheets("Saffola").Range("A1").Resize(rng.Rows.Count, rng.Columns.Count).Cells.Value = rng.Cells.Value
 
    Dim ws As Worksheet
    Set ws = Worksheets("Saffola")
    Columns(1).NumberFormat = "MM/DD/YYYY"
    
       For Each ws In ActiveWorkbook.Worksheets
        With ws.Rows(1).Font
            .Bold = True
        End With
        Application.Goto [A1]
    Next ws
    Worksheets("Saffola").Columns("A:I").AutoFit
    Sheet1.Delete
End Sub

Open in new window

Book1.xlsx
0
Comment
Question by:Nirvana
  • 2
3 Comments
 
LVL 28

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41725547
Please try this.....
Option Explicit

Sub FormatIt()
Dim sws As Worksheet, dws As Worksheet
Dim lr As Long

Application.ScreenUpdating = False
Set sws = ActiveSheet

sws.Columns(1).Delete
sws.Columns("C").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
lr = sws.Cells.SpecialCells(xlLastCell).Row

Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Saffola " & Format(Date, "dd-mm-yyyy")
Set dws = ActiveSheet
sws.Range("A1:F" & lr).Copy
dws.Range("A1").PasteSpecial xlPasteValues

dws.Columns(1).NumberFormat = "MM/DD/YYYY"
dws.Rows(1).Font.Bold = True
dws.Columns.AutoFit
dws.Range("A1").Select
    
Application.DisplayAlerts = False
sws.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

Open in new window

1
 

Author Closing Comment

by:Nirvana
ID: 41726006
great and quick
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41726169
Thanks for the feedback. :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now