返回列表 上一主題 發帖

[發問] 請問VBA 條件判斷式的使用方法?使用方法

[發問] 請問VBA 條件判斷式的使用方法?使用方法

這個是在論壇學習到的程式,但是如果A2資料錯誤,程式就不能執行,請問要怎麼修改?


Private Sub Worksheet_Change(ByVal Target As Range)
  Dim rTar As Range ' 填資料標的儲存格A欄
  
  With Target
    If .Address = "$A$2" Then
      lRow = dShi(.Text)
      Application.EnableEvents = False ' 變更資料中,關閉觸發Change程序'dShi(CStr(.Cells(lRow, 8))) = lRo
        .Offset(, 1) = shDat.Cells(lRow, 2) ' B2
        .Offset(, 2) = shDat.Cells(lRow, 9) ' c2
        .Offset(, 3) = shDat.Cells(lRow, 8)
        .Offset(, 4) = shDat.Cells(lRow, 6)
        .Offset(, 5) = shDat.Cells(lRow, 7)
        .Offset(, 6) = shDat.Cells(lRow, 1)
        .Offset(, 7) = shDat.Cells(lRow, 16)
        .Offset(, 8) = shDat.Cells(lRow, 17)
        .Offset(, 9) = shDat.Cells(lRow, 18)
        .Offset(, 10) = shDat.Cells(lRow, 19)
        .Offset(, 11) = shDat.Cells(lRow, 20)
        
        lShrRow = lShrRow + 1
        Set rTar = shShr.Cells(lShrRow, 1) ' [A末筆欄數]
      
        ' 其餘請自行調整填入
        
        
      Application.EnableEvents = True ' 資料變更完畢,恢復可觸發Change程序
    End If
  End With
End Sub

test05.png (229.65 KB)

test05.png

If .Address = "$A$2" Then
   if not isnumeric(.value) then exit sub '不是有效數值或為錯誤值, 跳出
   if .value<1 or .value>rows.count then exit sub '數值不在"列數"範圍內, 跳出
   lRow = .value

TOP

回復 2# 准提部林


    不好意思,不知道哪裡出錯,還是不行,幫忙看看附件

test abc recv - ANS (2).zip (64.11 KB)

test abc recv - ANS (3).zip (64.15 KB)

TOP

原程式根本不可能執行~~
沒有說明需求流程, 先做個樣:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim xF As Range, Cr
With Target
    If .Address <> "$A$2" Then Exit Sub
    If .Value = "" Then Exit Sub
    Set xF = [Data!H:H].Find(.Value, Lookat:=xlWhole)
    If xF Is Nothing Then Exit Sub
    Application.EnableEvents = False
    Cr = Array(2, 9, 8, 6, 7, 1, 16, 17, 18, 19, 20)
    For j = 1 To 11
        Cells(5, j + 1) = Sheets("Data").Cells(xF.Row, Cr(j - 1)).Value
    Next j
    Application.EnableEvents = True
End With
End Sub

TOP

回復 4# 准提部林


    現在差最後1步,如果我想取到的資料copy 到欄B,現有的btach no 因為B5向下的資料經常不同

我知道要寫For Each cell In Range([b5], [b65536].End(xlUp)),其他就不是很清楚,希望老師可以再次幫忙. ......

複本 test abc recv - ANS (3).zip (60.68 KB)

TOP

回復 5# john2006168

不清不楚, 矇著寫:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim xF As Range, Cr, DNo1&, dNo2&, xE As Range
With Target
    If .Address <> "$A$2" Then Exit Sub
    If .Value = "" Then Exit Sub
   
    Set xF = [F:F].Find(.Value, Lookat:=xlWhole, SearchDirection:=xlPrevious) '取得最後一筆 shipment ref
    If Not xF Is Nothing Then DNo1 = Val(xF(1, -3)) + 1 '得最後一筆 shipment ref 的 batch no +1
   
    Set xF = [Data!H:H].Find(.Value, Lookat:=xlWhole)
    If xF Is Nothing Then Exit Sub
   
    Application.EnableEvents = False
    Cr = Array(9, 2, 9, 8, 6, 7, 1, 16, 17, 18, 19, 20, 21)
    Set xE = Cells(Rows.Count, 3).End(xlUp)(2)
    If xE.Row < 5 Then Set xE = [C5]
    For j = 1 To 13
        xE(1, j) = Sheets("Data").Cells(xF.Row, Cr(j - 1)).Value
    Next j
   
    If Not IsDate(xE(1, 2)) Then Exit Sub
    dNo2 = Format(xE(1, 2), "yymm") * 1000 + 1
    xE(1, 0) = Application.Max(DNo1, dNo2)
   
    Application.EnableEvents = True
End With
End Sub

Xl0000285.rar (24.04 KB)

TOP

回復 6# 准提部林


   只是想有相片內的結果,但是非常感謝,多謝抽時間回答問題,,可以自己慢慢學.

fyi03.png (499.82 KB)

fyi03.png

TOP

        靜思自在 : 對父母要知恩,感恩、報恩。
返回列表 上一主題