马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 wp231957 于 2023-1-13 14:39 编辑
专辑三 戳这里
专辑二 戳这里
专辑四 请戳这里前进
1、获取倒数第二行的行号:Function get_row()
get_row = Range("A65536").End(xlUp).Offset(-1, 0).Row
End Function
Sub test()
Row = get_row
MsgBox Row
End Sub
Range("A65536").End(xlUp).Row+1 '获取第一个无数据行
Cells(行号, 256).End(xlToLeft).Column ' 获取第一个无数据列
brr = Range("a1:a" & Range("A" & Rows.Count).End(xlUp).Row)
2、使用变量 获取range区域
Range("m3:" & "p" & Row).Select
3、删除空行,如果有连续空行,会出现一些问题,所以从下往上删除
Sub test2()
Row = get_row
Range("b3:" & "b" & Row).Select
row2 = Selection.Rows.Count
For i = row2 + 2 To 1 + 2 Step -1
If (Cells(i, 2) = "") Then
Rows(i).Delete
End If
Next
End Sub
4、校验原始表中是否包含合并单元格Sub pd()
Row = get_row
Range("b3:" & "t" & Row).Select
For Each cc In Selection
If cc.MergeCells = True Then
MsgBox "表中包含合并单元格,无法进行排序"
End
End If
Next
End Sub
5、获取某行最右侧包含数据的列Sub test()
MsgBox Cells(125, 256).End(1).Column '获取125行的最右侧包含数据的列,返回的是一个数值(例如:9 表示I列)
End Sub
6、检索某列重复值(去重)使用了数据库以及CopyFromRecordset方法Sub wp()
Set Cnn = CreateObject("ADODB.Connection")
Set rst = CreateObject("ADODB.Recordset")
Cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName
Sql = "select distinct * from [Sheet2$a1:a16]"
Sheets("sheet2").Range("h" & 2).CopyFromRecordset Cnn.Execute(Sql)
End Sub
自动填充数列Sub wp()
Set Cnn = CreateObject("ADODB.Connection")
Set rst = CreateObject("ADODB.Recordset")
Cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName
Sql = "select distinct * from [Sheet1$a2:c2000]"
Sheets("sheet2").Range("a1:aa20000").Clear
Sheets("sheet2").Range("a" & 1).CopyFromRecordset Cnn.Execute(Sql)
Sheets("sheet2").Range("A1") = 100001
Sheets("sheet2").Range("A2") = 100002
Sheets("sheet2").Range("A1:A2").Select
Selection.AutoFill Destination:=Sheets("sheet2").Range("A1:A1000"), Type:=xlFillDefault
End Sub
多个相同结构表的拼接,插入表头,记录之间密切结合Sub wp()
Set cnn = CreateObject("ADODB.Connection")
Set rst = CreateObject("ADODB.Recordset")
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.Path & "" & "对账单A.xlsx"
Sql = "select * from [mm$]"
Windows(ThisWorkbook.Name).Activate
Sheets("mm").Activate
'插入header
Set rs = cnn.Execute(Sql)
For i = 1 To rs.Fields.Count
Cells(1, i) = rs.Fields(i - 1).Name
Next i
rs.Close
Sheets("mm").Range("a2").CopyFromRecordset cnn.Execute(Sql)
Sql = "select * from [rx$]"
Windows(ThisWorkbook.Name).Activate
Sheets("rx").Activate
'插入header
Set rs = cnn.Execute(Sql)
For i = 1 To rs.Fields.Count
Cells(1, i) = rs.Fields(i - 1).Name
Next i
rs.Close
Sheets("rx").Range("a2").CopyFromRecordset cnn.Execute(Sql)
Sql = "select * from [加工单$]"
Windows(ThisWorkbook.Name).Activate
Sheets("加工单").Activate
'插入header
Set rs = cnn.Execute(Sql)
For i = 1 To rs.Fields.Count
Cells(1, i) = rs.Fields(i - 1).Name
Next i
rs.Close
Sheets("加工单").Range("a2").CopyFromRecordset cnn.Execute(Sql)
row1 = Sheets("mm").Range("A65536").End(xlUp).Row + 1
row2 = Sheets("rx").Range("A65536").End(xlUp).Row + 1
row3 = Sheets("加工单").Range("A65536").End(xlUp).Row + 1
cnn.Close
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.Path & "" & "对账单B.xlsx"
Sql = "select * from [mm$]"
Windows(ThisWorkbook.Name).Activate
Sheets("mm").Range("a" & row1).CopyFromRecordset cnn.Execute(Sql)
Sql = "select * from [rx-lucuku$]"
Windows(ThisWorkbook.Name).Activate
Sheets("rx").Range("a" & row2).CopyFromRecordset cnn.Execute(Sql)
Sql = "select * from [加工单$]"
Windows(ThisWorkbook.Name).Activate
Sheets("加工单").Range("a" & row3).CopyFromRecordset cnn.Execute(Sql)
row1 = Sheets("mm").Range("A65536").End(xlUp).Row + 1
row2 = Sheets("rx").Range("A65536").End(xlUp).Row + 1
row3 = Sheets("加工单").Range("A65536").End(xlUp).Row + 1
cnn.Close
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.Path & "" & "对账单C.xlsx"
Sql = "select * from [mm$]"
Windows(ThisWorkbook.Name).Activate
Sheets("mm").Range("a" & row1).CopyFromRecordset cnn.Execute(Sql)
Sql = "select * from [rx-lucuku$]"
Windows(ThisWorkbook.Name).Activate
Sheets("rx").Range("a" & row2).CopyFromRecordset cnn.Execute(Sql)
Sql = "select * from [加工单$]"
Windows(ThisWorkbook.Name).Activate
Sheets("加工单").Range("a" & row3).CopyFromRecordset cnn.Execute(Sql)
cnn.Close
End Sub
关于列合并相加计算的sql语法一则Sub wp()
Set cnn = CreateObject("ADODB.Connection")
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName
Sql = "select 编码,[名称/规格/型号],sum(数量) from [记录簿$] group by 编码,[名称/规格/型号]"
Sheets("sheet1").Range("a2").CopyFromRecordset cnn.Execute(Sql)
cnn.Close
End Sub
Sub wp2()
Set cnn = CreateObject("ADODB.Connection")
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName
Sql = "select 货号,[名称/型号/规格],订单DD01+订单DD02+订单DD03+订单DD04+订单DD05+订单DD06+订单DD07+订单DD08+订单DD09 as 订单 from [货物表$] "
Sheets("sheet1").Range("f2").CopyFromRecordset cnn.Execute(Sql)
cnn.Close
End Sub
7、关于listbox textbox split ubound 的部分用法Private Sub CommandButton1_Click()
x = ListBox1.Text
ListBox1.RemoveItem (ListBox1.ListIndex) '删除列表选中项
TextBox1.Text = x
End Sub
Private Sub UserForm_Initialize()
s = Range("c1").Value
arr = Split(s, Chr(10)) '按换行符 拆分
For i = 1 To UBound(arr) 'ubound 探测数组长度
ListBox1.AddItem arr(i)
Next
End Sub
Sub wp()
arr = Range("c2:c11")
For x = 1 To UBound(arr)
tmp = Split(arr(x, 1), " ") ' tmp = Split(Application.Trim(arr(x, 1)), " ") 可以解决多个空格的问题
If UBound(tmp) > 0 Then
s = ""
t = ""
For Z = 0 To UBound(tmp)
If InStr(s, Split(tmp(Z), "/")(1)) = 0 Then
s = s + Split(tmp(Z), "/")(1) + " "
End If
If InStr(t, Split(tmp(Z), "/")(0)) = 0 Then
t = t + Split(tmp(Z), "/")(0) + " "
End If
Next
Range("e" & x + 1) = s
Range("f" & x + 1) = t
Else
Range("e" & x + 1) = Split(arr(x, 1), "/")(1)
Range("f" & x + 1) = Split(arr(x, 1), "/")(0)
End If
Next
End Sub
8、创建一个长度为0的文本文件Sub wp()
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFilePath = Excel.ThisWorkbook.Path & "" & "1" & ".txt"
With oFSO
Set oTextStream = oFSO.CreateTextFile(sFilePath, False)
End With
End Sub
|