AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1562772
Accepted
Kenny
Kenny
Asked: 2020-06-22 16:50:39 +0800 CST2020-06-22 16:50:39 +0800 CST 2020-06-22 16:50:39 +0800 CST

通过鼠标左键单击并拖动获取 Excel VBA Application.InputBox Range - 仅绑定到一列?

  • 772

我已经编辑了这篇文章以使其更清晰。

我有代码提示用户通过鼠标左键单击并拖动来选择一个范围,然后将不同工作表上一个单元格的内容复制到该范围内的每个单元格。

我现在只需要将宏绑定到 A 列,这样如果用户在任何其他列中选择任何范围或单元格,则宏将不会运行并给出错误消息,例如“您选择了无效区域,请重试”,然后再次显示选择框,因此用户只能在 A 列中进行选择

Public Sub SelectRange()
  Dim aRange As Range
  Dim cel As Range
      
  On Error Resume Next
    Sheets("Sheet2").Select
    Columns("A2:A").Select
  Set aRange = Application.InputBox(prompt:="Enter range - Click And Drag To Select", Type:=8)
        aRange.Formula = "=Sheet1!A2"
    If aRange Is Nothing Then
        MsgBox "Operation Cancelled"
  Else
    aRange.Select
  End If
End Sub
microsoft-excel vba
  • 3 3 个回答
  • 785 Views

3 个回答

  • Voted
  1. Best Answer
    FlexYourData
    2020-06-23T05:44:32+08:002020-06-23T05:44:32+08:00

    你在正确的轨道上。但是您需要决定是否要:

    a)用另一张表中的单元格中的值填充所选范围(如您在描述中所说),或者

    b)将范围的公式设置为指向另一张表中的单元格(这是您的代码正在执行的操作)

    无论如何,您可以使用以下内容。只需适当地注释/取消注释 Else 块中的相应代码即可。

    Public Sub SelectRange()
        Dim aRange As Range
        Dim msgresult As Integer
        
        
    TryAgain:
        On Error Resume Next 'go to the next line if the inputbox is nothing (X or Cancel)
        Set aRange = Application.InputBox(prompt:="Select a range of cells in column A", Type:=8, Title:="SuperUser")
        On Error GoTo 0 'resets the onerror action
        
        'if they haven't selected something
        'if they've selected more than one column
        'if they've selected any column other than the first column
        If aRange Is Nothing Or aRange.Columns.Count > 1 Or aRange.Column > 1 Then
          
            If MsgBox("You must only select cells in column A!" & vbCrLf & _
                    "Do you want to try again?" _
                    , vbYesNo _
                    , "SuperUser") = vbYes Then
                GoTo TryAgain
            Else
                Exit Sub
            End If
                    
        
        Else 'they selected a valid range
          
            aRange = Sheet2.Range("A2") 'fill the selected range with the value from this cell
            'OR:
            'aRange.Formula2 = "=Sheet2!$A$2" 'set the formula of the selected to point towards this cell
          
        End If
    End Sub
    
    • 1
  2. Gary's Student
    2020-06-23T10:18:50+08:002020-06-23T10:18:50+08:00

    试试这个:

    Public Sub SelectRange()
        Dim aRange As Range, s As String
        Dim Intersection As Range
        
        Sheets("Sheet2").Select
        On Error GoTo errr
        Set aRange = Application.InputBox(prompt:="Enter range - Click And Drag To Select", Type:=8)
        Set Intersection = Intersect(aRange, Range("A:A"))
        
        s = "=Sheet1!A2"
        
        If aRange Is Nothing Then
            MsgBox "Operation Cancelled"
            Exit Sub
        End If
        If Intersection Is Nothing Then
            MsgBox "Operation Cancelled"
            Exit Sub
        End If
        
        Intersection.Formula = s
        Exit Sub
    errr:
        On Error GoTo 0
        MsgBox "Operation Cancelled"
        Exit Sub
    End Sub
    
    • 1
  3. Rajesh Sinha
    2020-06-23T21:40:38+08:002020-06-23T21:40:38+08:00

    此宏限制用户仅从 A 列复制数据,在选择错误的情况下,提示并允许从 A 列重新选择数据,最后从特定工作表的单个单元格复制数据并粘贴到上一个 A 列中的选定范围床单。

    Public Sub SelectAndCopyRange()
       
    Dim aRange As Range
       
    OperationCancelled:
    On Error Resume Next
       
       Set aRange = Application.InputBox(prompt:="Select from column A only ", Type:=8)
       
       If aRange Is Nothing Or aRange.Columns.Count > 1 Or aRange.column > 1 Then
       If MsgBox("Operation cancelled, Invalid selection!,," & vbCrLf & _
                    "Like to select again?" _
                    , vbYesNo _
                    , "New Data Selection") = vbYes Then
                    
                GoTo OperationCancelled
                Else
                Exit Sub
            End If
            
       Else
          
            aRange = MySheet.Range("A1")
       
       End If
      
      Application.CutCopyMode = False
    
    End Sub
    
    • 1

相关问题

  • 如何对整列使用 Excel 的 LENGTH 函数?

  • Excel 数组(2 个变量)

  • 如何从 WSL 打开 office 文件

  • VBA根据文件名重命名工作表

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve