返回列表 上一主題 發帖

[發問] 價格刪除張數自動刪除

回復  coafort

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
With  ...
samwang 發表於 2022-1-27 10:20


請問大大
如果同一欄
3~23
27~40
需要這個功能
請問該怎麼改呢?
我改成以下,只有3~23有這功能,27~40沒有
謝謝大大
  If .Column = 16 Then
        If .Row < 3 Then Exit Sub
        If .Row > 23 Then Exit Sub
        If .Count > 1 Then Exit Sub
        If .Value = "" Then .Offset(, 1).ClearContents
        End If
    If .Column = 16 Then
        If .Row < 27 Then Exit Sub
        If .Row > 40 Then Exit Sub
        If .Count > 1 Then Exit Sub
        If .Value = "" Then .Offset(, 1).ClearContents
        End If

TOP

請問大大
如果同一欄
3~23
27~40
需要這個功能
請問該怎麼改呢?
我改成以下,只有3~23有這功能, ...
coafort 發表於 2022-1-29 08:56


      If .Column = 16 Then
         If .Row < 3 Then Exit Sub
         If .Row > 40 Then Exit Sub
         If .Row = 26 Then Exit Sub
         If .Count > 1 Then Exit Sub
         If .Value = "" Then .Offset(, 1).ClearContents
      End If

TOP

If .Column = 16 Then
         If .Row < 3 Then Exit Sub
         If .Row > 40 Then Exit  ...
samwang 發表於 2022-1-29 12:21


大大您好
3~40當中
24~26這三列是不需要的
大大的方式好像只有26
還是說另外24 25也是要輸入呢?
謝謝大大

TOP

大大您好
3~40當中
24~26這三列是不需要的
大大的方式好像只有26
還是說另外24 25也是要輸入呢?
...
coafort 發表於 2022-1-29 12:44


If .Row >= 24 And .Row <= 26 Then Exit Sub

TOP

If .Row >= 24 And .Row
samwang 發表於 2022-1-29 13:56


謝謝大大
終於可以了
有大大真好
祝福大大虎年行大運
新年大快樂:D

TOP

請問大大,下列要如何整合呢?
我用分割線分隔
也就是第二第三的工作表是這個
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
With Target
    If .Worksheet.Index = 1 Then Exit Sub
    If .Worksheet.Index > 3 Then Exit Sub
    If .Column = 41 Then
        If .Count > 1 Then Exit Sub
        If .Value = "" Then .Offset(, 1).ClearContents
        End If
    If .Column = 27 Then
        If .Count > 1 Then Exit Sub
        If .Value = "" Then .Offset(, 1).ClearContents
        End If
    If .Column = 1 Then
        If .Count > 1 Then Exit Sub
        If .Value = "" Then .Offset(, 1).ClearContents
        End If
    If .Column = 16 Then
         If .Row < 3 Then Exit Sub
         If .Row > 40 Then Exit Sub
         If .Row >= 24 And .Row <= 26 Then Exit Sub
         If .Count > 1 Then Exit Sub
         If .Value = "" Then .Offset(, 1).ClearContents
      End If
    If .Column = 20 Then
         If .Row < 3 Then Exit Sub
         If .Row > 40 Then Exit Sub
         If .Row >= 24 And .Row <= 26 Then Exit Sub
         If .Count > 1 Then Exit Sub
         If .Value = "" Then .Offset(, 1).ClearContents
      End If


第四第五的第七第八工作表是這個
================================================================
    If .Worksheet.Index = 6 Then Exit Sub
    If .Worksheet.Index > 4 Then Exit Sub
    If .Worksheet.Index > 8 Then Exit Sub
    If .Column = 45 Then
        If .Count > 1 Then Exit Sub
        If .Value = "" Then .Offset(, 1).ClearContents
        End If
    If .Column = 30 Then
        If .Count > 1 Then Exit Sub
        If .Value = "" Then .Offset(, 1).ClearContents
        End If
    If .Column = 1 Then
        If .Count > 1 Then Exit Sub
        If .Value = "" Then .Offset(, 1).ClearContents
        End If
    If .Column = 17 Then
         If .Row < 3 Then Exit Sub
         If .Row > 40 Then Exit Sub
         If .Row >= 24 And .Row <= 26 Then Exit Sub
         If .Count > 1 Then Exit Sub
         If .Value = "" Then .Offset(, 1).ClearContents
      End If
    If .Column = 22 Then
         If .Row < 3 Then Exit Sub
         If .Row > 40 Then Exit Sub
         If .Row >= 24 And .Row <= 26 Then Exit Sub
         If .Count > 1 Then Exit Sub
         If .Value = "" Then .Offset(, 1).ClearContents
      End If
  End With
End Sub
謝謝大大

TOP

本帖最後由 Andy2483 於 2023-7-21 08:46 編輯

回復 26# coafort


    謝謝前輩
後學藉此帖學習到 Workbook.SheetChange 事件
以下是學習方案,請前輩參考

Option Explicit
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim Wi%, Wc%, Wr&
'↑宣告變數
With Target
   If .Count > 1 Then Exit Sub
   '↑任何表只要變更格大於1格就結束程式執行
   Wi = .Worksheet.Index: Wc = .Column: Wr = .Row
   '↑令Wi是 觸發工作表索引號,令Wc是 觸發欄號,令Wr是 觸發列號

'////第二第三的工作表////

   If InStr("/2/3/", "/" & Wi & "/") Then
   '↑如果觸發表索引號是 2或3 ??
      If InStr("/1/27/41/", "/" & Wc & "/") Then
      '↑如果觸發欄是 1,27或41
         If .Value = "" Then .Offset(, 1).ClearContents
      End If
      If InStr("/16/20/", "/" & Wc & "/") Then
         If Wr < 3 Or Wr > 40 Or (Wr >= 24 And Wr <= 26) Then Exit Sub
         If .Value = "" Then .Offset(, 1).ClearContents
         '↑如果觸發列是 3~23 或 27~40
      End If
   End If

'////第四第五的第七第八工作表////
   If InStr("/4/5/7/8/", "/" & Wi & "/") Then
   '↑如果觸發表索引號是 4,5,7或8 ??
      If InStr("/1/30/45/", "/" & Wc & "/") Then
      '↑如果觸發欄是 1,30或45
         If .Value = "" Then .Offset(, 1).ClearContents
      End If
      If InStr("/17/22/", "/" & Wc & "/") Then
         If Wr < 3 Or Wr > 40 Or (Wr >= 24 And Wr <= 26) Then Exit Sub
         If .Value = "" Then .Offset(, 1).ClearContents
      End If
   End If
End With
End Sub
用行動裝置瀏覽論壇學習很方便,謝謝論壇經營團隊
請大家一起上論壇來交流

TOP

回復  coafort


    謝謝前輩
後學藉此帖學習到 Workbook.SheetChange 事件
以下是學習方案,請前輩 ...
Andy2483 發表於 2023-7-21 08:41


真是非常感謝大大幫忙
可以了
讚讚

TOP

回復 28# coafort


    恭喜
後學認為用工作表索引號辨識 風險高(萬一不小心變動順序)
建議還是以工作表名辨識比較好
用行動裝置瀏覽論壇學習很方便,謝謝論壇經營團隊
請大家一起上論壇來交流

TOP

回復  coafort


    恭喜
後學認為用工作表索引號辨識 風險高(萬一不小心變動順序)
建議還是以工作表 ...
Andy2483 發表於 2023-7-21 09:10



真的很好用
真的很感謝會VB的安迪大大

TOP

        靜思自在 : 道德是提昇自我的明燈,不該是呵斥別人的鞭子。
返回列表 上一主題