>即時新聞-熱門

显示标签为“試算表VBA”的博文。显示所有博文
显示标签为“試算表VBA”的博文。显示所有博文

2012年9月20日星期四

EXCEL VBA 去除"$"的方法 replace

Sub 快速走勢圖()


Dim Sht As Worksheet

Set Sht = Worksheets(1)

Set Rng1 = Sht.UsedRange

Set Rng2 = Rng1.SpecialCells(xlCellTypeLastCell)

x = Rng2.Address



Set Rng = Selection

mystart = Rng.Address

mystart = Replace(mystart, "$", "", 1) ' 去除此之外符號成為相對公式

Range("P2").Select

Range("$P$2").SparklineGroups.Add Type:=xlSparkLine, SourceData:=mystart

走勢圖參數

ActiveCell.FormulaR1C1 = "q"

Range("P2").Select

Selection.AutoFill Destination:=Range("P2:P109")

Range("P2:P109").Select

Selection.ClearContents

Range("P2").Select





Set Rng1 = Nothing

Set Rng2 = Nothing

Set Sht = Nothing





End Sub

Public Sub 走勢圖參數()

Selection.SparklineGroups.Item(1).SeriesColor.ThemeColor = 5

Selection.SparklineGroups.Item(1).SeriesColor.TintAndShade = -0.499984740745262

Selection.SparklineGroups.Item(1).Points.Negative.Color.ThemeColor = 6

Selection.SparklineGroups.Item(1).Points.Negative.Color.TintAndShade = 0

Selection.SparklineGroups.Item(1).Points.Markers.Color.ThemeColor = 5

Selection.SparklineGroups.Item(1).Points.Markers.Color.TintAndShade = - _

0.499984740745262

Selection.SparklineGroups.Item(1).Points.Highpoint.Color.ThemeColor = 5

Selection.SparklineGroups.Item(1).Points.Highpoint.Color.TintAndShade = 0

Selection.SparklineGroups.Item(1).Points.Lowpoint.Color.ThemeColor = 5

Selection.SparklineGroups.Item(1).Points.Lowpoint.Color.TintAndShade = 0

Selection.SparklineGroups.Item(1).Points.Firstpoint.Color.ThemeColor = 5

Selection.SparklineGroups.Item(1).Points.Firstpoint.Color.TintAndShade = _

0.399975585192419

Selection.SparklineGroups.Item(1).Points.Lastpoint.Color.ThemeColor = 5

Selection.SparklineGroups.Item(1).Points.Lastpoint.Color.TintAndShade = _

0.399975585192419

End Sub

2011年6月2日星期四

EXCEL VBA 每隔5秒更新

Private Sub Worksheet_Activate()




Application.OnTime Now + TimeValue("00:00:05"), "loaddata"



End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

Application.OnTime Now + TimeValue("00:00:05"), "loaddata"

End Sub

另外寫一個副程式

Sub loaddata()



ActiveCell.Offset(1, 0).Select

ActiveCell.Value = "網路資料"

End Sub

2011年5月18日星期三

EXCEL VBA

Sub 資料分割()


流水號

排序

資料搬移

End Sub

Sub 流水號()



Dim mystop



Range("A1").Select

mystop = Selection.End(xlDown).Row



Columns("A:A").Select

Selection.Insert Shift:=xlToRight

Range("A1").Value = "編號"

Range("A2").Value = 1

Range("A2").Select

Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _

Step:=1, Stop:=mystop - 1, Trend:=False

End Sub

Sub 排序()

Dim mystop, newrng, newname



Range("A1").Select

mystop = Selection.End(xlDown).Row



Range("B1").Select

newrng = "A1:G" & mystop

Range(newrng).Sort Key1:=Range("B1"), Order1:=xlAscending, Header:= _

xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _

SortMethod:=xlStroke, DataOption1:=xlSortNormal



Range("B2").Select





For i = 1 To mystop - 1



If ActiveCell.Value = ActiveCell.Offset(1, 0).Value Then



newname = ActiveCell.Value

ActiveCell.Offset(1, 0).Select





Else







Sheets.Add

Sheets(Sheets.Count - 1).Name = newname



Sheets(Sheets.Count).Select

ActiveCell.Offset(1, 0).Select





End If

Next



End Sub

Sub 資料搬移()

'

For i = 1 To Sheets.Count - 1



Sheets(Sheets.Count).Select

Range("A1").Select

Selection.AutoFilter

Selection.AutoFilter Field:=2, Criteria1:=Sheets(i).Name

Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select

Selection.Copy

Sheets(i).Select

Range("A1").Select

ActiveSheet.Paste

Application.CutCopyMode = False

Sheets(Sheets.Count).Select

Range("A1").Select

Selection.AutoFilter



Next

End Sub

2011年5月15日星期日

合併與刪除

資料內容如下

十一   消防工程


a          滅火器

1         ABC乾粉滅火器,10型滅火效能A-3.B-

2         ABC乾粉滅火器,20型滅火效能A-5.B-

          30.C

3         ABC乾粉滅火器,50型滅火效能A-8.B-

          30.C

b         室內消防栓箱及採水系統

1         消防泵,額定馬力50HP以下,水量

           1680LPM以上,揚程70M以上

           採水泵,額定馬力50HP以下,水量

           2200LPM以上,揚程56M以上


程式如下


Sub 合併與刪除()


Dim i, j

Dim myrng As Range

Set myrng = Sheets(1).UsedRange

j = myrng.Rows.Count



Range("A1").Select



MsgBox j

For i = 1 To j



If ActiveCell.Offset(1, 0).Value = Empty Then

ActiveCell.Offset(0, 1).Value = ActiveCell.Offset(0, 1).Value & ActiveCell.Offset(1, 1).Value

ActiveCell.Offset(1, 0).Select

Selection.EntireRow.Delete



Else

ActiveCell.Offset(1, 0).Select





End If





Next



Range("A1").Select



End Sub

2011年1月20日星期四

EXCEL _VBA(檔案列表)

Sub 檔案列表()


Dim myPath As String

Dim myFileName As String

Dim i As Long

myPath = ThisWorkbook.Path & "\"
myFileName = Dir(myPath, 0)

i = 1

Do While Len(myFileName) > 0



Cells(i, 1) = myFileName

myFileName = Dir()

i = i + 1

Loop

k = 1

For j = 1 To i

If Right(Cells(j, 1), 3) = "xls" Then

Cells(k, 2) = Cells(j, 1)



k = k + 1



End If

Next

Columns(1).Delete



End Sub


Sub 指定檔案()

Dim myFs As FileSearch

Dim myPath As String

Dim myClc As New Collection

Dim i As Long

Set myFs = Application.FileSearch

myPath = ThisWorkbook.Path
With myFs

.NewSearch

.LookIn = myPath

.FileType = msoFileTypeAllFiles

.Filename = "*.xls"

.SearchSubFolders = True

If .Execute(SortBy:=msoSortByFileName) > 0 Then

For i = 1 To .FoundFiles.Count

If .FoundFiles(i) Like myPath & "*" Then

On Error Resume Next

myClc.Add .FoundFiles(i), .FoundFiles(i)

On Error GoTo 0

End If

Next

End If

End With

Set myFs = Nothing
With myClc

If .Count > 0 Then

For i = 1 To .Count


Cells(i, 1) = Right(.Item(i), Len(.Item(i)) - Len(myPath) - 1)

Next

Else


End If

End With

Set myClc = Nothing
End Sub

2011年1月17日星期一

EXCEL VBA 工作表重新命名

以下用於更新工作表2010為2011的命名
Sub 重新命名()


For i = 1 To Sheets.Count

x = Sheets(i).Name

s = Len(x)

x1 = Left(x, 3) & "1" & Right(x, s - 4)

Sheets(i).Name = x1

Next

End Sub

2010年12月13日星期一

EXCEL _ VBA資料拆解

Sub 資料比對()


Dim my_rng, my_end

Sheets.Add

Workbooks.Open Filename:="T2 Digit Code.xls"

my_end = ActiveCell.SpecialCells(xlLastCell).Row

my_rng = "C4:C" & my_end

Range(my_rng).Select

Selection.Copy

Windows("FW11 2nd CUST FCST - 021210.xls").Activate

ActiveSheet.Paste

Application.CutCopyMode = False

Windows("T2 Digit Code.xls").Close

刪除空白

End Sub



Sub 刪除空白()

Dim my_start, my_end

my_end = ActiveCell.SpecialCells(xlLastCell).Row



For i = 1 To my_end



If ActiveCell.Value <> Empty Then

ActiveCell.Offset(1, 0).Select

Else





Selection.Delete Shift:=xlUp



End If

Next



Range("A1").Select



End Sub



Sub 合併篩選()

Dim my_rng, i, y

Sheets(1).Select

y = ActiveCell.SpecialCells(xlLastCell).Row

For i = 1 To y

my_rng = "A" & i

' 資料移轉

Sheets(Sheets.Count).Select

Range("A1").Select

Selection.AutoFilter

Selection.AutoFilter Field:=2, Criteria1:=Sheets(1).Range(my_rng).Value

Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select

Selection.Copy

'新活頁簿

Workbooks.Add

ActiveSheet.Paste

Application.CutCopyMode = False

If Range("A2").Value <> Empty Then



'儲存活頁簿

x = Range("c2").Value

ActiveWorkbook.SaveAs Filename:= _

x & ".xls", FileFormat:= _

xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _

, CreateBackup:=False

'關閉活頁簿

ActiveWindow.Close

Else

Application.DisplayAlerts = False

ActiveWorkbook.Close

End If





' 結束篩選

Sheets(Sheets.Count).Select

Range("A1").Select

Selection.AutoFilter



Next



End Sub

2010年12月1日星期三

快速分類

Sub 流水號()




Columns("A:A").Select

Selection.Insert Shift:=xlToRight

Range("A1").Select

ActiveCell.FormulaR1C1 = "no"

Range("A2").Select

ActiveCell.FormulaR1C1 = "1"

Range("A2").Select

Selection.AutoFill Destination:=Range("A2:A246"), Type:=xlFillSeries

Range("A2:A246").Select

Range("N2").Select

End Sub

Sub 排序()



Range("A1:U246").Sort Key1:=Range("N2"), Order1:=xlAscending, Header:= _

xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _

SortMethod:=xlStroke, DataOption1:=xlSortNormal

End Sub

Sub 新工作表()

Dim x, sht_name

Sheets(Sheets.Count).Select

Range("N2").Select



While (ActiveCell.Value <> Empty)

If ActiveCell.Value = ActiveCell.Offset(1, 0).Value Then



ActiveCell.Offset(1, 0).Select



Else



sht_name = ActiveCell.Value

ActiveCell.Offset(1, 0).Select



Sheets.Add

x = Sheets.Count - 1

Sheets(x).Name = sht_name

Sheets(Sheets.Count).Select



End If



Wend





End Sub



Sub 資料比對()

Dim x



Sheets(Sheets.Count).Select

Range("A1").Select



For i = 1 To Sheets.Count - 1



x = Sheets(i).Name

Selection.AutoFilter

Selection.AutoFilter Field:=14, Criteria1:=x

Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select

Selection.Copy

Sheets(x).Select

ActiveSheet.Paste

Application.CutCopyMode = False

Sheets(Sheets.Count).Select

Range("A1").Select

Selection.AutoFilter



Next

End Sub



Sub 資料分類()

流水號

排序

新工作表

資料比對

End Sub

2010年11月25日星期四

VBA ADIDAS

Sub 開檔()




Workbooks.Open Filename:= _

"C:\Users\alexmysir\Desktop\07.FW11\2.xls"

Sheets("Comparison").Select

Range("A4:D53").Select

Selection.Copy

Windows("new.xls").Activate



Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _

xlNone, SkipBlanks:=False, Transpose:=False





Application.CutCopyMode = False

Selection.End(xlToRight).Select

ActiveCell.Offset(0, 2).Select



Windows("2.xls").Activate

ActiveWindow.Close

End Sub

Sub 調整()

'

' 調整 Macro

' alexmysir 在 11/26/2010 錄製的巨集

'



'

Cells.Select

Cells.EntireColumn.AutoFit

Range("A1").Select

End Sub

Sub 開檔二()



Workbooks.Open Filename:= _

"C:\Users\alexmysir\Desktop\07.FW11\1.xls"

Sheets("Comparison").Select

Range("C4:D53").Select

Selection.Copy

Windows("new.xls").Activate



Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _

xlNone, SkipBlanks:=False, Transpose:=False





Application.CutCopyMode = False



Windows("1.xls").Activate

ActiveWindow.Close

End Sub











Sub 整合()

開檔

開檔二



調整





End Sub



Sub 儲存()

'

' 儲存 Macro

' alexmysir 在 11/26/2010 錄製的巨集

'



'

ActiveWorkbook.SaveAs Filename:="C:\Users\alexmysir\Desktop\07.FW11\21.xls", _

FileFormat:=xlNormal, Password:="", WriteResPassword:="", _

ReadOnlyRecommended:=False, CreateBackup:=False

End Sub

2010年8月20日星期五

VBA季

Private Sub Worksheet_Change(ByVal Target As Range)
x = Left(Target.Address, 2)
If x = "$D" Then
Select Case Target.Value
Case 1 To 3
ActiveCell.Offset(-1, 1).Value = "第一季"

Case 4 To 6
ActiveCell.Offset(-1, 1).Value = "第二季"

Case 7 To 9
ActiveCell.Offset(-1, 1).Value = "第三季"

Case 10 To 12
ActiveCell.Offset(-1, 1).Value = "第四季"


End Select
End If


End Sub

VBA - 工作表

Private Sub Worksheet_Change(ByVal Target As Range)
x = Left(Target.Address, 2)
If x = "$a" Then
Select Case Target.Value
Case 1
ActiveCell.Offset(-1, 1).Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="板橋,中和,永和"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.IMEMode = xlIMEModeNoControl
.ShowInput = True
.ShowError = True
End With
Case 2
ActiveCell.Offset(-1, 1).Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="aa,jj,kk,ll,mm,nn"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.IMEMode = xlIMEModeNoControl
.ShowInput = True
.ShowError = True
End With

End Select
End If


End Sub

刪除空白列

Sub 刪除空白()
Range("A65530").Select
Selection.End(xlUp).Select
x = ActiveCell.Row

Range("A1").Select

For y = 1 To x

If ActiveCell.Value = Empty Then

x1 = ActiveCell.Row
z = x1 & ":" & x1
Rows(z).Select
Selection.Delete Shift:=xlUp

Else

ActiveCell.Offset(1, 0).Select

End If


Next
Range("A1").Select


End Sub

2010年8月19日星期四

篩選

Sub 篩選()

x = InputBox("請輸入需要的資料內容")

Selection.AutoFilter
Selection.AutoFilter Field:=2, Criteria1:=x
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy
Sheets.Add
ActiveSheet.Paste
Application.CutCopyMode = False
Range("A1").Select
Sheets(2).Select
Selection.AutoFilter
Range("A1").Select
End Sub

合併

Sub 合併工作表()
mydate = Now()
If Month(Now()) < 10 Then
mydate1 = "0" & Month(Now())
newname = Year(mydate) & mydate1 & Day(mydate)
Else
newname = Year(mydate) & Month(mydate) & Day(mydate)
End If


y = InputBox("請輸入合併工作表數量", "世新")

For x = 1 To y

Sheets(x + 1).Select
If x = 1 Then

Range("A1").Select
Else
Range("A2").Select

End If
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy
Sheets(1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Next
Range("A1").Select
Sheets(1).Name = newname
End Sub

合併工作表

Sub 合併工作表()

For x = 1 To 3

Sheets(x + 1).Select
Range("A1").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy
Sheets(1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.End(xlDown).Select

Next

End Sub

新工作表

Sub 新工作表()
x = InputBox("請輸入工作表數量", "世新")

Sheets(1).Select

For i = 1 To x Step 1
Sheets.Add
Next
End Sub

自動流水號

Sub 流水號()
i = ActiveCell.Value
x = InputBox("請輸入終止值")

If i = Empty Then

ActiveCell.Value = 1

Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
Step:=1, Stop:=x, Trend:=False
Else
Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
Step:=1, Stop:=x + i, Trend:=False

End If


End Sub

2010年7月25日星期日

EXCEL VBA 身份證檢查

1 工具 / 巨集 / VB編輯程式
2 對工作表SHEET1 按左鍵二下
3 於右側視窗輸入
Private Sub Worksheet_Change(ByVal Target As Range)
Dim x
Dim T0, A0, A1, A2, A3, A4, A5, A6, A7, A8, CK, SUM1 As Integer
x = Left(Target.Address, 2)
If x = "$A" Then
If (Len(Target.Value) <> 10) Then
Target.Value = "重新輸入"

Else



T0 = Asc(UCase(Left(Target.Value, 1)))
A1 = (((Val(Right(Target.Value, 9)) \ 100000000) Mod 10) * 8) Mod 10
A2 = (((Val(Right(Target.Value, 8)) \ 10000000) Mod 10) * 7) Mod 10
A3 = (((Val(Right(Target.Value, 7)) \ 1000000) Mod 10) * 6) Mod 10
A4 = (((Val(Right(Target.Value, 6)) \ 100000) Mod 10) * 5) Mod 10
A5 = (((Val(Right(Target.Value, 5)) \ 10000) Mod 10) * 4) Mod 10
A6 = (((Val(Right(Target.Value, 4)) \ 1000) Mod 10) * 3) Mod 10
A7 = (((Val(Right(Target.Value, 3)) \ 100) Mod 10) * 2) Mod 10
A8 = (((Val(Right(Target.Value, 2)) \ 10) Mod 10) * 1) Mod 10
CK = Val(Right(Target.Value, 1))
Select Case T0
Case 65, 77, 87
A0 = 0
Case 75, 76, 89
A0 = 1
Case 74, 86, 88
A0 = 2
Case 72, 85
A0 = 3
Case 84, 71
A0 = 4
Case 70, 83
A0 = 5
Case 69, 82
A0 = 6
Case 68, 81, 79
A0 = 7
Case 67, 80, 73
A0 = 8
Case 66, 78, 90
A0 = 9
Case Else
' ActiveCell.Formula = "重新輸入"
Target.Value = "重新輸入"
End Select
SUM1 = 9 - ((A0 + A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8) Mod 10)
If CK <> SUM1 Then

Target.Value = "重新輸入"
End If

End If


End If
End Sub

4 於A欄輸入身份字號 , 就知到答案了

2010年6月11日星期五

儲存格設計清單

Private Sub Worksheet_Change(ByVal Target As Range)

x = Left(Target.Address, 2)
If x = "$A" Then
Select Case Target.Value
Case 2
Target.Value = "台北"
Case 3
Target.Value = "桃園"
Case 4
Target.Value = "台中"
Case 5
Target.Value = "嘉義"
Case 6
Target.Value = "台東"
Case 7
Target.Value = "高雄"

End Select


End If

End Sub
Private Sub Worksheet_Change(ByVal Target As Range)

x = Left(Target.Address, 2)
If x = "$C" Then
Select Case Target.Value
Case 1
Target.Value = ActiveCell.Offset(0, -1).Value + ActiveCell.Offset(0, -2).Value

Case 2
Target.Value = (ActiveCell.Offset(0, -1).Value + ActiveCell.Offset(0, -2).Value) / 2
Case 3
Target.Value = ActiveCell.Offset(0, -1).Value - ActiveCell.Offset(0, -2).Value


End Select


End If


End Sub




Private Sub Worksheet_Activate()
Range("b6").FormulaR1C1 = "=SUM(R[-4]C:R[-1]C)"
X = ActiveCell.Value2

ActiveCell.Value = X




End Sub

Private Sub OK_Click() ' 表單元件


ActiveCell.Offset(0, 0).Value = num1.Value
ActiveCell.Offset(0, 1).Value = num2.Value
ActiveCell.Offset(0, 2).Value = num3.Value
ActiveCell.Offset(0, 3).Value = num4.Value
ActiveCell.Offset(1, 0).Select
num1.Value = Empty
num2.Value = Empty
num3.Value = Empty
num4.Value = Empty
num1.SetFocus

End Sub

中華工程 - 新增工作表

Sub 方法一()
Dim mySht As Worksheet
Dim x, y
x = InputBox("請輸入增加工作表的數量", "增加工作表")
For y = 1 To x Step 1
Set mySht = Worksheets.Add(After:=Sheets(Sheets.Count))
With mySht
.Name = y '設定表名
End With
Next y
Set mySht = Nothing '物件的釋放
End Sub

Sub 方法二()
Dim x, y
x = InputBox("請輸入增加工作表的數量", "增加工作表")
With Worksheets
.Add Count:=x '指定張數來新增

End With
End Sub

 
妹咕數位學園歡迎網友們來信指教 妹咕信箱