อาจารย์ครับ ขอรบกวนอาจารย์อีกครับ
ผมเจอปัญหากับช่วงข้อมูลที่ใช้ autofilter ครับ คือจากตัวอย่างไฟล์จะมี 3 ชีท
1. 7New ใช้สำหรับใส่ข้อมูลสินค้าใหม่ (เฉพาะแถวที่ 6)
2. STK เป็นฐานข้อมูลสินค้าเกี่ยวกับจำนวนสินค้า เข้า ออก คงเหลือ
3. Pro เป็นฐานข้อมูลสินค้าในเรื่องการกำหนดราคา
เวลาใส่ข้อมูลสินค้าตัวใหม่ ในชีท 7New เสร็จแล้วกดเพิ่มสินค้า ข้อมูลสินค้าจะไปอยู่ในตารางชีท STK และชีท Pro โดยเรียงลำดับตามกลุ่มสินค้า และรหัสสินค้า แต่ปัญหาเกิดที่ชีท STK ครับ หลังจากเรียงลำดับและใส่ autofilter แล้ว สินค้ารายการล่างสุดจะไม่รวมอยู่ใน autofilter (เลือกจาก drop down list จะมองไม่เห็น) เป็นมาตั้งแต่แรก ลองใส่ autofilter เองด้วยมือก็แล้ว ก็ไม่หาย ผมควรแก้อย่างไรครับ
โค้ด VBA สำหรับปุ่ม เพิ่มสินค้าใหม่ครับ อยู่ใน module1 (เรียงลำดับอยู่ในบรรทัด 155 ครับ)
Code: Select all
Private Sub Product_AddNew()
Dim sH7 As Worksheet, sTK As Worksheet, sPro As Worksheet
Dim er As Integer, FndVal1 As Integer, FndVal2 As Integer, lstRow As Integer
Dim pdGrp As Integer, untPck As Integer
Dim bCode As Variant
Dim intCd As Long
Dim fndRng As Range
Dim untNm As String
Set sH7 = Sheets("7New")
Set sTK = Sheets("STK")
Set sPro = Sheets("Pro")
intCd = sH7.Range("d6").Value
bCode = sH7.Range("g6").Value
er = Application.CountIf(sH7.Range("j3:j8"), "*?")
'-----------------------------------
'ตรวจสอบว่าพบ error หรือไม่
'-----------------------------------
If sH7.Range("b6") = 0 Or intCd = 0 Then
MsgBox "ใส่รายละเอียดให้ครบก่อน"
Exit Sub
End If
If er > 0 Then
MsgBox "โปรดแก้ไขข้อผิดพลาด", vbCritical, "ERROR"
Exit Sub
End If
'-----------------------------------------------
'ตรวจรหัสบาร์โค้ด ไม่ให้ซ้ำกับฐานข้อมูลเก่า
'-----------------------------------------------
Application.ScreenUpdating = False
FndVal1 = Application.CountIf(sTK.Columns("c:c"), intCd)
If bCode <> "" Then
FndVal2 = Application.CountIf(sTK.Columns("M:M"), bCode)
Else
FndVal2 = 0
End If
If FndVal1 + FndVal2 > 0 Then
MsgBox "ข้อมูลซ้ำ! โปรดตรวจสอบรหัสภายในหรือบาร์โค้ดใหม่", vbExclamation
Application.ScreenUpdating = True
Exit Sub
End If
'-----------------
'เริ่มใส่ค่าตัวแปร
'-----------------
With sH7
pdGrp = .Range("b6").Value
untNm = .Range("e6").Value
untPck = .Range("f6").Value
End With
'-------------------------------------------------
'หาแถวสุดท้ายในชีท STK และใส่รายการสินค้า
'-------------------------------------------------
With sTK
.Activate
.Unprotect
If .AutoFilterMode Then .AutoFilterMode = False
lstRow = .Range("b" & Rows.Count).End(xlUp).Offset(1, 0).Row
.Cells(lstRow, 2).Value = pdGrp
.Cells(lstRow, 3).Value = intCd
.Cells(lstRow, 4).Value = untNm
.Cells(lstRow, 5).Value = untPck
.Cells(lstRow, 13).Value = bCode
.Cells(lstRow, 12).NumberFormat = "#,##0.00;[Red]-#,##0.00;0"
.Cells(lstRow, 13).NumberFormat = "0"
.Range(Cells(lstRow, 5), Cells(lstRow, 5).Offset(0, 6)).NumberFormat = "#,##0;[Red]-#,##0;"
.Range(Cells(lstRow, 14), Cells(lstRow, 15)).NumberFormat = "#,##0.00;[Red]-#,##0.00;0"
.Range(Cells(lstRow, 2), Cells(lstRow, 3)).HorizontalAlignment = xlCenter
'-------------
'Add formulas
'-------------
.Range("v5").Copy
.Cells(lstRow, 1).PasteSpecial Paste:=xlPasteFormulas
.Range("w5:ac5").Copy
.Cells(lstRow, 6).PasteSpecial Paste:=xlPasteFormulas
.Range("ad5:ak5").Copy
.Cells(lstRow, 14).PasteSpecial Paste:=xlPasteFormulas
'--------------
'เปลี่ยน font
'--------------
With .Range(Cells(lstRow, 1), Cells(lstRow, 1).Offset(0, 14)).Font
.Name = "BrowalliaUPC"
.Size = 14
.TintAndShade = 0
End With
'--------
'ตีกรอบ
'--------
With .Range(.Cells(lstRow, "a"), .Cells(lstRow, "o")).Borders
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlHairline
End With
'//ใส่สีช่องคงเหลือ//
With .Range(Cells(lstRow, 10), Cells(lstRow, 11)).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 6750207
.TintAndShade = 0
.PatternTintAndShade = 0
End With
'-----------
'เรียงลำดับ
'-----------
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=.Range("b" & lstRow) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Sort.SortFields.Add Key:=Range("c" & lstRow) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .Sort
.SetRange Range("B5:U" & lstRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
If Not .AutoFilterMode Then .Range("a5:u" & lstRow).AutoFilter
.Protect , AllowFiltering:=True
End With
'//Add product to pro sheet
With sPro
.Activate
.Unprotect
.Columns("a:a").EntireRow.Hidden = False
lstRow = .Range("b" & Rows.Count).End(xlUp).Offset(1, 0).Row
.Cells(lstRow, 1).Value = pdGrp
.Cells(lstRow, 2).Value = intCd
.Cells(lstRow, 2).HorizontalAlignment = xlCenter
.Cells(lstRow, 3).Value = untNm
.Cells(lstRow, 4).Value = Format(untPck, "#,##0")
.Cells(lstRow, 6).NumberFormat = "#,##0"
.Cells(lstRow, 6).Locked = False
With .Range(.Cells(lstRow, 1), .Cells(lstRow, 10)).Borders
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
End With
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=.Range("a4") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Sort.SortFields.Add Key:=.Range("b4") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .Sort
.SetRange Range("a4:j" & lstRow)
.Header = xlYes
.Orientation = xlTopToBottom
.MatchCase = False
.Apply
End With
Set fndRng = .Columns("b:b").Find(what:=intCd, lookat:=xlWhole)
fndRng.Offset(0, 4).Select
.Protect
.EnableSelection = xlUnlockedCells
End With
sH7.Range("b6,d6:g6").ClearContents
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
MsgBox "เพิ่มสินค้าใหม่เรียบร้อย"
End Sub
ขอบพระคุณครับ