最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

實例35-兩表匹配,實例36-根據(jù)輸入值自動填充數(shù)據(jù) Excel表格VBA編程實例 代碼分享

2023-03-11 11:51 作者:凌霄百科_Excel辦公程序  | 我要投稿

實例35-兩表匹配


Private Sub CommandButton匹配1_Click()

'判斷參數(shù)不為空

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

'清除匹配結(jié)果

With ThisWorkbook.Worksheets("匹配結(jié)果") '清除原列表數(shù)據(jù)

.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 '判斷循環(huán)時是否匹配成功

With ThisWorkbook.Worksheets("匹配結(jié)果") '清除原列表數(shù)據(jù)

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("匹配結(jié)果").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("匹配結(jié)果").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("匹配結(jié)果").Activate

End Sub

Private Sub CommandButton匹配2_Click()

'判斷參數(shù)不為空

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

'清除匹配結(jié)果

With ThisWorkbook.Worksheets("匹配結(jié)果") '清除原列表數(shù)據(jù)

.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 '判斷循環(huán)時是否匹配成功

With ThisWorkbook.Worksheets("匹配結(jié)果") '清除原列表數(shù)據(jù)

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("匹配結(jié)果").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("匹配結(jié)果").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("匹配結(jié)果").Activate

End Sub


實例36-根據(jù)輸入值自動填充數(shù)據(jù)


Private Sub Worksheet_Change(ByVal Target As Range)

With ThisWorkbook.Worksheets("出庫表")

If Target.Column = 3 And Target.Row >= 6 And Target.Row <= 10 Then

Dim row1 As Long

row1 = Target.Row

If Target <> "" Then

Dim i

For i = 1 To ThisWorkbook.Worksheets("商品列表").Cells(1000000, 1).End(xlUp).Row

If Target.Value = ThisWorkbook.Worksheets("商品列表").Cells(i, 1) Then

.Cells(row1, 4) = ThisWorkbook.Worksheets("商品列表").Cells(i, 2)

.Cells(row1, 5) = ThisWorkbook.Worksheets("商品列表").Cells(i, 4)

Exit Sub

End If

Next i

MsgBox "未找到對應商品"

Target = ""

.Cells(row1, 4) = ""

.Cells(row1, 5) = ""

Else

.Cells(row1, 4) = ""

.Cells(row1, 5) = ""

End If

End If

End With

End Sub


實例35-兩表匹配,實例36-根據(jù)輸入值自動填充數(shù)據(jù) Excel表格VBA編程實例 代碼分享的評論 (共 條)

分享到微博請遵守國家法律
肥城市| 定远县| 天水市| 宣恩县| 丰宁| 资兴市| 咸宁市| 昭觉县| 元氏县| 长春市| 泽库县| 岐山县| 元朗区| 乐都县| 平昌县| 额尔古纳市| 威海市| 林州市| 天门市| 怀化市| 雅安市| 乡宁县| 浏阳市| 治县。| 延庆县| 墨竹工卡县| 凤山县| 乐陵市| 玉山县| 花垣县| 武冈市| 安龙县| 铁力市| 乌海市| 宜君县| 武川县| 洞头县| 民勤县| 宁安市| 达拉特旗| 白城市|