Excel表格数据库导入
这次我们通过一个简单的案例,示范一下把既基础又标准的excel文件怎么导入数据库中去。先看一下数据的长相:这种格式非常标准。A列虽然看起来是YYYY-MM只到月份的形式,但是我们点击A2单元格,看一下...
2024.09.20Private Sub CommandButton匹配1_Click()
‘判断参数不为空
Dim mc1 As Long
Dim mc2 As Long
With ThisWorkbook.Worksheets("操作界面")
If .Cells(2, "C").Value "" Then
mc1 = .Cells(2, "C").Value
Else
MsgBox "请输入表1匹配列"
Exit Sub
End If
If .Cells(6, "C").Value "" Then
mc2 = .Cells(6, "C").Value
Else
MsgBox "请输入表2匹配列"
Exit Sub
End If
End With
‘清除匹配结果
With ThisWorkbook.Worksheets("匹配结果") ‘清除原列表数据
.UsedRange.ClearFormats
.UsedRange.ClearContents
End With
‘获取表1表2最大列号行号
Dim cmax1 As Long
Dim cmax2 As Long
cmax1 = ThisWorkbook.Worksheets("表1").UsedRange.Cells(ThisWorkbook.Worksheets("表1").UsedRange.Count).Column
cmax2 = ThisWorkbook.Worksheets("表2").UsedRange.Cells(ThisWorkbook.Worksheets("表2").UsedRange.Count).Column
Dim rmax1 As Long
Dim rmax2 As Long
rmax1 = ThisWorkbook.Worksheets("表1").UsedRange.Cells(ThisWorkbook.Worksheets("表1").UsedRange.Count).Row
rmax2 = ThisWorkbook.Worksheets("表2").UsedRange.Cells(ThisWorkbook.Worksheets("表2").UsedRange.Count).Row
Dim i, j
Dim addrow As Long
addrow = 1
Dim matchtext1 As String
Dim matchtext2 As String
Dim a1 As Integer ‘判断循环时是否匹配成功
With ThisWorkbook.Worksheets("匹配结果") ‘清除原列表数据
For i = 1 To rmax2
a1 = 0
With ThisWorkbook.Worksheets("表2")
If .Cells(i, mc2) "" Then
matchtext2 = .Cells(i, mc2)
.Range(.Cells(i, 1), .Cells(i, cmax2)).Copy ThisWorkbook.Worksheets("匹配结果").Cells(addrow, 1)
With ThisWorkbook.Worksheets("表1")
For j = 1 To rmax1
If .Cells(j, mc1) "" Then
matchtext1 = .Cells(j, mc1)
If matchtext1 = matchtext2 Then
.Range(.Cells(j, 1), .Cells(j, cmax1)).Copy ThisWorkbook.Worksheets("匹配结果").Cells(addrow, cmax2 + 1)
a1 = 1
addrow = addrow + 1
End If
End If
Next j
End With
If a1 = 0 Then
addrow = addrow + 1
End If
End If
End With
Next i
End With
ThisWorkbook.Worksheets("匹配结果").Activate
End Sub
Private Sub CommandButton匹配2_Click()
‘判断参数不为空
Dim mc1 As Long
Dim mc2 As Long
With ThisWorkbook.Worksheets("操作界面")
If .Cells(2, "C").Value "" Then
mc1 = .Cells(2, "C").Value
Else
MsgBox "请输入表1匹配列"
Exit Sub
End If
If .Cells(6, "C").Value "" Then
mc2 = .Cells(6, "C").Value
Else
MsgBox "请输入表2匹配列"
Exit Sub
End If
End With
‘清除匹配结果
With ThisWorkbook.Worksheets("匹配结果") ‘清除原列表数据
.UsedRange.ClearFormats
.UsedRange.ClearContents
End With
‘获取表1表2最大列号
Dim cmax1 As Long
Dim cmax2 As Long
cmax1 = ThisWorkbook.Worksheets("表1").UsedRange.Cells(ThisWorkbook.Worksheets("表1").UsedRange.Count).Column
cmax2 = ThisWorkbook.Worksheets("表2").UsedRange.Cells(ThisWorkbook.Worksheets("表2").UsedRange.Count).Column
Dim rmax1 As Long
Dim rmax2 As Long
rmax1 = ThisWorkbook.Worksheets("表1").UsedRange.Cells(ThisWorkbook.Worksheets("表1").UsedRange.Count).Row
rmax2 = ThisWorkbook.Worksheets("表2").UsedRange.Cells(ThisWorkbook.Worksheets("表2").UsedRange.Count).Row
Dim i, j
Dim addrow As Long
addrow = 1
Dim matchtext1 As String
Dim matchtext2 As String
Dim a1 As Integer ‘判断循环时是否匹配成功
With ThisWorkbook.Worksheets("匹配结果") ‘清除原列表数据
For i = 1 To rmax1
a1 = 0
With ThisWorkbook.Worksheets("表1")
If .Cells(i, mc1) "" Then
matchtext1 = .Cells(i, mc1)
.Range(.Cells(i, 1), .Cells(i, cmax1)).Copy ThisWorkbook.Worksheets("匹配结果").Cells(addrow, 1)
With ThisWorkbook.Worksheets("表2")
For j = 1 To rmax2
If .Cells(j, mc2) "" Then
matchtext2 = .Cells(j, mc2)
If matchtext1 = matchtext2 Then
.Range(.Cells(j, 1), .Cells(j, cmax2)).Copy ThisWorkbook.Worksheets("匹配结果").Cells(addrow, cmax1 + 1)
a1 = 1
addrow = addrow + 1
End If
End If
Next j
End With
If a1 = 0 Then
addrow = addrow + 1
End If
End If
End With
Next i
End With
ThisWorkbook.Worksheets("匹配结果").Activate
End Sub
实例36-根据输入值自动填充数据Private Sub Worksheet_Change(ByVal Target As Range)
With ThisWorkbook.Worksheets("出库表")
If Target.Column = 3 And Target.Row >= 6 And Target.Row
这次我们通过一个简单的案例,示范一下把既基础又标准的excel文件怎么导入数据库中去。先看一下数据的长相:这种格式非常标准。A列虽然看起来是YYYY-MM只到月份的形式,但是我们点击A2单元格,看一下...
2024.09.20前言:在职场报表中,EXCEL的魔法,可以说无处不在,是一项每个人必须掌握的技能,但是在EXCEL所有的函数之中,有一种函数,称之为【数据库函数】,高效,运行速度快,专门为大数据提供,却很少为人知 收...
2024.09.19有兴趣的条友可以看一下,不精通EXCEL,却是摸索后的综合技巧要复制几万条的区域内容单单靠“复制”“粘贴”能让人小拇指搞到抽筋。如下:举个例子,“原表”中每一个规格需要对应“制作表”中的6行内容。复制...
2024.09.17在日常使用Excel填充序列号的方式有很多,大多数人可能都使用的是下拉的方式。这种方式没错,如果表格内容比较短其实还好。但,如果你有上万条数据,那工作量就太大了。下拉都要下拉半天。这就教大家两个简单的...
2024.09.20大家在日常工作中一定遇到过这样的问题,就是在Excel中,你需要在第二张表格中利用到第一张表格的信息,那么怎么把第一张表格的信息自动对应到第二张呢?下面我们就一起来看看吧!首先在工作簿1建立好自己需要...
2024.09.14