فهمت عليك ما تريد
العناوين من B5 الى I5 تتغير من خلال قوائم منسدلة
1-اذا كانت الخلية فارغة E2 الماكرو يستخرج حسب E3
2- اذا كانت الخلية فارغة E3 الماكرو يستخرج حسب E2
3-اذا كانت الخليتان فارغتين الماكرو يستخرج كل البيانات
4-اذا كانت الخليتان غير فارغتين الماكرو يستخرج
حسب الشرطين(E2 & E3)
CODE
Option Explicit
Dim Da As Worksheet, Ch As Worksheet
Dim Rg_ch As Range, rg As Range
Dim frst, secd, cnt%, i%, Lr%, tt%, k%, x
'++++++++++++++++++++++++++++++++++++++++++++
Sub insert_data_By_data_val()
Dim arr(7)
Set Da = Sheets("Data"): Set Ch = Sheets("Choise")
'//////////////////////////////////////
Rem Initialize the Headres To array
For i = 2 To 9
Set rg = Da.Range("A1:Q1").Find(Ch.Cells(5, i), lookat:=1)
If Not rg Is Nothing Then
x = Left(rg.Address(0, 0), 1)
arr(i - 2) = x
End If
Next
'//////////////////////////////////////
frst = Ch.Range("E2"): secd = Ch.Range("E3")
Set Rg_ch = Ch.Range("A5").CurrentRegion
If Rg_ch.Rows.Count > 1 Then _
Rg_ch.Offset(1).Resize(Rg_ch.Rows.Count - 1).Clear
'++++++++++++++++++++++++++++++++++++++
Lr = Da.Cells(Rows.Count, 1).End(3).Row
Select Case True
Case frst = vbNullString And secd <> vbNullString
k = 6: GoTo frst_Yes_sec_No
Case frst <> vbNullString And secd = vbNullString
k = 5: GoTo frst_No_sec_yes
Case frst = vbNullString And secd = vbNullString
GoTo Both_no
Case frst <> vbNullString And secd <> vbNullString
GoTo Both_Yes
End Select
'+++++++++++++++++++++++++++++++++++++++
frst_Yes_sec_No:
cnt = 0
For i = 2 To Lr
If Da.Cells(i, 1).Offset(, k) = secd Then
With Ch.Cells(cnt + 6, 2)
.Offset(, -1) = cnt + 1
For tt = LBound(arr) To UBound(arr) - 1
.Offset(, tt) = Da.Cells(i, arr(tt))
Next tt
cnt = cnt + 1
End With
End If
Next i
GoTo Format_range
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++
frst_No_sec_yes:
cnt = 0
For i = 2 To Lr
If Da.Cells(i, 1).Offset(, k) = frst Then
With Ch.Cells(cnt + 6, 2)
.Offset(, -1) = cnt + 1
For tt = LBound(arr) To UBound(arr)
.Offset(, tt) = Da.Cells(i, arr(tt))
Next tt
cnt = cnt + 1
End With
End If
Next i
GoTo Format_range
'++++++++++++++++++++++++++++++++++++++++++++++++++++
Both_no:
cnt = 0
For i = 2 To Lr
With Ch.Cells(cnt + 6, 2)
.Offset(, -1).Select
.Offset(, -1) = cnt + 1
For tt = LBound(arr) To UBound(arr)
.Offset(, tt) = Da.Cells(i, arr(tt))
Next tt
End With
cnt = cnt + 1
Next
GoTo Format_range
'+++++++++++++++++++++++++++++++
Both_Yes:
cnt = 0
For i = 2 To Lr
If Da.Range("F" & i) = frst And _
Da.Range("G" & i) = secd Then
With Ch.Cells(cnt + 6, 2)
.Offset(, -1) = cnt + 1
For tt = LBound(arr) To UBound(arr)
.Offset(, tt) = Da.Cells(i, arr(tt))
Next tt
cnt = cnt + 1
End With
End If
Next
Format_range:
Set Rg_ch = Ch.Range("A5").CurrentRegion
If Rg_ch.Rows.Count > 1 Then
Set Rg_ch = Rg_ch.Offset(1).Resize(Rg_ch.Rows.Count - 1)
With Rg_ch
.Font.Size = 18: .Font.Bold = True
.InsertIndent 1: .Interior.ColorIndex = 35
.Borders.LineStyle = 1
.Value = .Value
Columns(2).NumberFormat = "0000"
End With
End If
End Sub
الملف من جديد