VBA 」标签的归档

Find month from a date in English

常常需要將某個日子的英文獲取出來,當然最蠢的辦法是用select case語句將12個月份都hard code進來,不過下面的語句就好聰明地利用了format的作用進行轉換.

Dim myDate as Date
myDate=#4/5/2011#
Debug.Print Format(DateSerial(2011, VBA.Month(myDate), 1), "mmm")

'format(____, "mmm"): Jan, Feb,etc
'format(____, "mmmm"): January, February,etc
article clipper remember Find month from a date in English
 

Visible Range

Worksheet中可見Range的定位問題,可通過以下語句獲取Top,Left.

Dim rng As Range
Set rng = ActiveWindow.VisibleRange
'Top: rng.Top
'Left: rng.Left
'center:
cTop = rng.Top + rng.Height / 2
cWidth = rng.Left + rng.Width / 2
article clipper remember Visible Range
 

Copy from a closed workbook

'You must set a reference to the
'Microsoft ActiveX Data Objects 2.5 library in the VBA editor
'http://en.allexperts.com/q/Excel-1059/Copy-closed-workbook.htm
Option Explicit

Sub GetData_ClosedWorkBook()
   Dim sh As Worksheet
   Dim rDest As Range
   Dim SaveDriveDir As String
   Dim sPath As String
   Dim FName As Variant
   Dim N As Long
   Dim lNum As Long

   SaveDriveDir = CurDir
   sPath = Application.DefaultFilePath    'or use "C:\Data"
  ChDrive sPath
   ChDir sPath

   FName = Application.GetOpenFilename(filefilter:="Excel Files,*.xls", _
                                       MultiSelect:=True)
   If IsArray(FName) Then

       ' Sort the Array
      FName = Array_Sort(FName)

       Application.ScreenUpdating = False
       'Add worksheet to the Activeworkbook and use the Date/Time as name
      'You can change this to suit
      Set sh = ActiveWorkbook.Worksheets.Add
       sh.Name = Format(Now, "dd-mm-yy h-mm-ss")

       'Loop through all files you select in the GetOpenFilename dialog
      For N = LBound(FName) To UBound(FName)

           'Find the last row with data
          lNum = LastRow(sh)

           'create the destination cell address
          Set rDest = sh.Cells(lNum + 1, 1)

           ' For testing Copy the workbook name in Column E
          sh.Cells(lNum + 1, 5).Value = FName(N)

           'Get the cell values and copy it in the rDest
          'Change the Sheet name and range as you like
          GetData FName(N), "Income", "A1:K100", rDest, False, False

       Next
   End If

   ChDrive SaveDriveDir
   ChDir SaveDriveDir
   Application.ScreenUpdating = True
End Sub

Now add another module and copy & paste these Functions

Option Explicit

Public Sub GetData(SourceFile As Variant, SourceSheet As String, _
                  sourceRange As String, TargetRange As Range, _
                  Header As Boolean, UseHeaderRow As Boolean)
   Dim rsData As ADODB.Recordset
   Dim szConnect As String
   Dim szSQL  As String
   Dim lCount As Long

   If Header = False Then
       ' Create the connection string.
      szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                   "Data Source=" & SourceFile & ";" & _
                   "Extended Properties=""Excel 8.0;HDR=No"";"
   Else
       ' Create the connection string.
      szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                   "Data Source=" & SourceFile & ";" & _
                   "Extended Properties=""Excel 8.0;HDR=Yes"";"
   End If

   szSQL = "SELECT * FROM [" & SourceSheet$ & "$" & sourceRange$ & "];"
   On Error GoTo SomethingWrong
   Set rsData = New ADODB.Recordset
   rsData.Open szSQL, szConnect, adOpenForwardOnly, _
               adLockReadOnly, adCmdText

   ' Check to make sure we received data and copy the data
  If Not rsData.EOF Then
       If Header = False Then
           TargetRange.Cells(1, 1).CopyFromRecordset rsData
       Else
           'Add the header cell in each column if the last argument is True
          If UseHeaderRow Then
               For lCount = 0 To rsData.Fields.Count - 1
                   TargetRange.Cells(1, 1 + lCount).Value = _
                   rsData.Fields(lCount).Name
               Next lCount
               TargetRange.Cells(2, 1).CopyFromRecordset rsData
           Else
               TargetRange.Cells(1, 1).CopyFromRecordset rsData
           End If
       End If
   Else
       MsgBox "No records returned from : " & SourceFile, vbCritical
   End If

   ' Clean up our Recordset object.
  rsData.Close
   Set rsData = Nothing
   Exit Sub
SomethingWrong:
   MsgBox "The file name, Sheet name or Range is invalid of : " & SourceFile, _
          vbExclamation, "Error"
   On Error GoTo 0
End Sub


Function LastRow(sh As Worksheet)
   On Error Resume Next
   LastRow = sh.Cells.Find(What:="*", _
                           After:=sh.Range("A1"), _
                           Lookat:=xlPart, _
                           LookIn:=xlFormulas, _
                           SearchOrder:=xlByRows, _
                           SearchDirection:=xlPrevious, _
                           MatchCase:=False).Row
   On Error GoTo 0
End Function


Function Array_Sort(ArrayList As Variant) As Variant
   Dim aCnt As Integer, bCnt As Integer
   Dim tempStr As String
   For aCnt = LBound(ArrayList) To UBound(ArrayList) - 1
       For bCnt = aCnt + 1 To UBound(ArrayList)
           If ArrayList(aCnt) > ArrayList(bCnt) Then
               tempStr = ArrayList(bCnt)
               ArrayList(bCnt) = ArrayList(aCnt)
               ArrayList(aCnt) = tempStr
           End If
       Next bCnt
   Next aCnt
   Array_Sort = ArrayList
End Function
article clipper remember Copy from a closed workbook
 

Find Last Row of Workbook

Function LastRow(sh As Worksheet)
    On Error Resume Next    
    LastRow = sh.Cells.Find(What:="*", _
                            After:=sh.Range("A1"), _
                            Lookat:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Row
    On Error GoTo 0
End Function
article clipper remember Find Last Row of Workbook
 

Sport the differences

編程時,常常不會一次性地把所有代碼都寫好,假若沒有形成良好的版本管理,就會出現不知道各個版本之間的分別在哪幾行代碼.

有鑑於此, JKP-ADS 研發了一個Tool – Export VBA Projects,可以把workbook中的所有代碼導出到一個txt中,然後利用免費軟件ExamDiff 比較兩者的分別.

Export VBA Projects使用方法:

  • Step 1

image thumb Sport the differences

  • Step 2

image thumb 3 Sport the differences

article clipper remember Sport the differences
 

密码保护:VBA Crack

这是一篇受密码保护的文章。您需要提供访问密码:


article clipper remember VBA Crack
 

密码保护:Crack VBA

这是一篇受密码保护的文章。您需要提供访问密码:


article clipper remember Crack VBA
 

How to Fax from Microsoft Excel – by Yoav Ezer

We have written previously about how it is possible to email from within Microsoft Excel . Using a VBA macro we can email based on cell contents or send messages to a contact list. What about people for whom we only have fax numbers for? Well, you are in luck. Here is how to send a fax from within your Excel spreadsheet!

Faxing With Excel

If you want to try this solution out you can download our example spreadsheet. Our example file contains a list of recipients. Some of them have fax numbers instead of email addresses, and the macro sends those via fax.

faxing from excel How to Fax from Microsoft Excel – by Yoav Ezer

继续阅读

article clipper remember How to Fax from Microsoft Excel – by Yoav Ezer
 

Align Shapes using VBA

'Following is the code to align 2 shapes. Just paste the following code
'and call AlignShapes function to align shapes with each other

Option Explicit

Enum enmAlignTo
    aln1Left = 1
    aln2Right = 2
    aln3Top = 3
    aln4Bottom = 4
    aln5VerticalCenter = 5
    aln6HorizontalCenter = 6
End Enum

Sub AlignShapes(shpAlignMe As Shape, shpAlignWith As Shape, enmAlign As enmAlignTo)
    Select Case enmAlign
    Case aln1Left
        shpAlignMe.Left = shpAlignWith.Left
    Case aln2Right
        shpAlignMe.Left = shpAlignWith.Left + (shpAlignWith.Width - shpAlignMe.Width)
    Case aln3Top
        shpAlignMe.Top = shpAlignWith.Top
    Case aln4Bottom
        shpAlignMe.Top = shpAlignWith.Top + (shpAlignWith.Height - shpAlignMe.Height)
    Case aln5VerticalCenter
        shpAlignMe.Top = shpAlignWith.Top + ((shpAlignWith.Height - shpAlignMe.Height) / 2)
    Case aln6HorizontalCenter
        shpAlignMe.Left = shpAlignWith.Left + ((shpAlignWith.Width - shpAlignMe.Width) / 2)
    End Select
End Sub

Source: http://excelexperts.com/Align-Shapes-using-VBA

学习点:利用Enum定义一个类型,将无意义的1,2,3,4,5,6转换为该类型的一个属性,更利于理解.

article clipper remember Align Shapes using VBA