Você está na página 1de 12

Code:

Dim rng As Range, cel As Range

Sheets("Summary").Activate

Set rng = Range([A2], Cells(Rows.Count, "A").End(xlUp))

For Each cel In rng

Sheets(Format(cel, "@")).[A5].Value = cel(1, 2)

Next

Sub sendmultiple()

'updateby Extendoffice 20160506

Dim xOTApp As Object

Dim xMItem As Object

Dim xCell As Range

Dim xRg As Range

Dim xEmailAddr As String

Dim xTxt As String

On Error Resume Next

xTxt = ActiveWindow.RangeSelection.Address

Set xRg = Application.InputBox("Please select the addresses list:", "Kutools for Excel", xTxt, , , , , 8)

If xRg Is Nothing Then Exit Sub

Set xOTApp = CreateObject("Outlook.Application")

For Each xCell In xRg

If xCell.Value Like "*@*" Then

If xEmailAddr = "" Then

xEmailAddr = xCell.Value

Else
xEmailAddr = xEmailAddr & ";" & xCell.Value

End If

End If

Next

Set xMItem = xOTApp.CreateItem(0)

With xMItem

.To = xEmailAddr

.Display

End With

End Sub

Sub EmailAttachmentRecipients()

'updateby Extendoffice 20160506

Dim xOutlook As Object

Dim xMailItem As Object

Dim xRg As Range

Dim xCell As Range

Dim xEmailAddr As String

Dim xTxt As String

On Error Resume Next

xTxt = ActiveWindow.RangeSelection.Address

Set xRg = Application.InputBox("Please select the arresses list:", "Kutools for Excel", xTxt, , , , , 8)

If xRg Is Nothing Then Exit Sub

Set xOutlook = CreateObject("Outlook.Application")

Set xMailItem = xOutlook.CreateItem(0)B6


For Each xCell In xRg

If xCell.Value Like "*@*" Then

If xEmailAddr = "" Then

xEmailAddr = xCell.Value

Else

xEmailAddr = xEmailAddr & ";" & xCell.Value

End If

End If

Next

With xMailItem

.To = xEmailAddr

.CC = ""

.Subject = ""

.Body = ""

.Attachments.Add ActiveWorkbook.FullName

.Display

End With

Set xOutlook = Nothing

Set xMailItem = Nothing

End Sub

Option Explicit

Sub ExcelDiet()
Dim j As Long

Dim k As Long

Dim LastRow As Long

Dim LastCol As Long

Dim ColFormula As Range

Dim RowFormula As Range

Dim ColValue As Range

Dim RowValue As Range

Dim Shp As Shape

Dim ws As Worksheet

Application.ScreenUpdating = False

Application.DisplayAlerts = False

On Error Resume Next

For Each ws In Worksheets

With ws

'Find the last used cell with a formula and value

'Search by Columns and Rows

On Error Resume Next

Set ColFormula = .Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlFormulas, _

LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)

Set ColValue = .Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlValues, _

LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)


Set RowFormula = .Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlFormulas, _

LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)

Set RowValue = .Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlValues, _

LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)

On Error GoTo 0

'Determine the last column

If ColFormula Is Nothing Then

LastCol = 0

Else

LastCol = ColFormula.Column

End If

If Not ColValue Is Nothing Then

LastCol = Application.WorksheetFunction.Max(LastCol, ColValue.Column)

End If

'Determine the last row

If RowFormula Is Nothing Then

LastRow = 0

Else

LastRow = RowFormula.Row

End If

If Not RowValue Is Nothing Then

LastRow = Application.WorksheetFunction.Max(LastRow, RowValue.Row)

End If
'Determine if any shapes are beyond the last row and last column

For Each Shp In .Shapes

j=0

k=0

On Error Resume Next

j = Shp.TopLeftCell.Row

k = Shp.TopLeftCell.Column

On Error GoTo 0

If j > 0 And k > 0 Then

Do Until .Cells(j, k).Top > Shp.Top + Shp.Height

j=j+1

Loop

If j > LastRow Then

LastRow = j

End If

Do Until .Cells(j, k).Left > Shp.Left + Shp.Width

k=k+1

Loop

If k > LastCol Then

LastCol = k

End If

End If

Next
.Range(.Cells(1, LastCol + 1), .Cells(.Rows.Count, .Columns.Count)).EntireColumn.Delete

.Range("A" & LastRow + 1 & ":A" & .Rows.Count).EntireRow.Delete

End With

Next

Application.ScreenUpdating = True

Application.DisplayAlerts = True

End Sub

How to reduce Excel file size using a Macro

I found this VBA script to reduce Excel file size; in my case it reduces the original Excel file from 59.9 MB
to 40 KB… What a shrink J

Private Sub Workbook_BeforeClose(Cancel As Boolean)

Dim myLastRow As Long

Dim myLastCol As Long

Dim wks As Worksheet

Dim dummyRng As Range

For Each wks In ActiveWorkbook.Worksheets

With wks

myLastRow = 0

myLastCol = 0

Set dummyRng = .UsedRange

On Error Resume Next

myLastRow = _
.Cells.Find("*", after:=.Cells(1), _

LookIn:=xlFormulas, lookat:=xlWhole, _

searchdirection:=xlPrevious, _

searchorder:=xlByRows).Row

myLastCol = _

.Cells.Find("*", after:=.Cells(1), _

LookIn:=xlFormulas, lookat:=xlWhole, _

searchdirection:=xlPrevious, _

searchorder:=xlByColumns).Column

On Error GoTo 0

If myLastRow * myLastCol = 0 Then

.Columns.Delete

Else

.Range(.Cells(myLastRow + 1, 1), _

.Cells(.Rows.Count, 1)).EntireRow.Delete

.Range(.Cells(1, myLastCol + 1), _

.Cells(1, .Columns.Count)).EntireColumn.Delete

End If

End With

Next wks

End Sub

View Course

I NEED A SOLUTION

Want faster solutions? Boost your question’s priority today.Go Premium

Status: Solved Priority: Medium Security: Public Views: 3124 Last Modified: 2014-08-18
How to reduce excel file size

Hi All

i have been searching how to reduce an excel file size, as in my company, some of the excel file has
reached over 100mb due to many macro has been added in the excel file, and i found below in some
forum, and any idea how do i do below ?

opy this code into your personal workbook, then run the resetallusedranges macro and see if that helps
after you save the workbook:

Sub ResetAllUsedRanges()

Dim wks As Worksheet

For Each wks In ActiveWorkbook.Worksheets

ResetUsedRange wks

Next wks

End Sub

Sub ResetUsedRange(Optional wks As Worksheet)

Dim lngLastRow As Long, lngLastCol As Long, lngRealLastRow As Long, lngRealLastCol As Long

On Error Resume Next

If wks Is Nothing Then Set wks = ActiveSheet

lngLastRow = 1

lngLastCol = 1

With wks

With .Range("A1").SpecialCells(xlCellTypeLastCell)
lngLastRow = .Row

lngLastCol = .Column

End With

lngRealLastRow = .Cells.Find("*", .Range("A1"), xlFormulas, , xlByRows, xlPrevious).Row

lngRealLastCol = .Cells.Find("*", .Range("A1"), xlFormulas, , xlByColumns, xlPrevious).Column

If lngRealLastRow < lngLastRow Then .Range(.Cells(lngRealLastRow + 1, 1), .Cells(lngLastRow,


1)).EntireRow.Delete

If lngRealLastCol < lngLastCol Then .Range(.Cells(1, lngRealLastCol + 1), .Cells(1,


lngLastCol)).EntireColumn.Delete

Debug.Print .UsedRange.Count

End With

End Sub

enter image description here

'Copy range of interest

Dim r As Range

Set r = Range("B2:D5")

r.Copy

'Open a new mail item

Dim outlookApp As Outlook.Application

Set outlookApp = CreateObject("Outlook.Application")

Dim outMail As Outlook.MailItem

Set outMail = outlookApp.CreateItem(olMailItem)


'Get its Word editor

outMail.Display

Dim wordDoc As Word.Document

Set wordDoc = outMail.GetInspector.WordEditor

'To paste as picture

wordDoc.Range.PasteAndFormat wdChartPicture

alternative is then to paste as an image in Excel, and then cut and paste that image into your e-mail:

'Copy range of interest

Dim r As Range

Set r = Range("B2:D5")

r.Copy

'Paste as picture in sheet and cut immediately

Dim p As Picture

Set p = ActiveSheet.Pictures.Paste

p.Cut

'Open a new mail item

Dim outlookApp As Outlook.Application

Set outlookApp = CreateObject("Outlook.Application")

Dim outMail As Outlook.MailItem

Set outMail = outlookApp.CreateItem(olMailItem)


'Get its Word editor

outMail.Display

Dim wordDoc As Word.Document

Set wordDoc = outMail.GetInspector.WordEditor

'Paste picture

wordDoc.Range.Paste

Você também pode gostar