所有工作表中的宏保护,但仅适用于具有某些值的单元格

时间:2018-08-04 16:40:32

标签: excel excel-vba

我需要一些帮助来使我的代码正常工作。我的代码很简单。它遍历了我所有的工作表,并在A:D栏中进行了简单的检查。如果一个单元格中有一些文本,它将被锁定。所有空闲的单元格都将保留,供用户解锁。

它以Workbook_Open作为Call命令从工作表中的其他宏开始。

我一直在每个工作表中一直使用它,但是它不适用于新工作表,因此我决定以某种方式使其在全局和动态范围内都适用于旧工作表和新添加的工作表。

旧代码:

bot.on('voiceStateUpdate', (oldMember, newMember) => {
  let newUserChannel = newMember.voiceChannel
  let oldUserChannel = oldMember.voiceChannel


  if(oldUserChannel === undefined && newUserChannel !== undefined) {

    bot.sendMessage({
           to: "475330828466126848",
           message: "User went form Channel" + oldUserChannel + "to the new" 
           + newUserChannel + "Channel"
    });

  }
})

如您所见,我不得不为每张纸放置一个调用命令并没有那么好

新代码:

var Discord = require('discord.js');
var logger = require("winston");
var auth = require("./auth.json");
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, {
colorize: true});
logger.level = "debug";

// Initialize Discord Bot
var bot = new Discord.Client({
token: auth.token,
autorun: true
});

bot.on("ready", function (evt) {
logger.info("Connected");
logger.info("Logged in as: ");
logger.info(bot.username + " – (" + bot.id + ")");
});

bot.on('voiceStateUpdate', (oldMember, newMember) => {
  let newUserChannel = newMember.voiceChannel
  let oldUserChannel = oldMember.voiceChannel


  if(!oldUserChannel && newUserChannel) {

    bot.channels.get('475330828466126848').send("User went form Channel" + oldUserChannel.name + "to the new" 
           + newUserChannel.name + "Channel");

  }
});

1 个答案:

答案 0 :(得分:3)

在上面我的评论中,尝试类似这样的操作。该代码将自动适用于新添加的工作表

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim strPassword As String: strPassword = "Athens"
    On Error GoTo Whoa

    Application.EnableEvents = False

    If Not Intersect(Target, Columns("A:D")) Is Nothing Then
        With Sh
            .Unprotect strPassword
            Cells.Locked = True
            Range("A:D").SpecialCells(xlCellTypeBlanks).Locked = False
            .Protect strPassword
        End With
    End If

Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub

屏幕截图

将代码放在ThisWorkbook代码区域中。

enter image description here