+ Reply to Thread
Results 1 to 9 of 9

Click and drag VBA?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-16-2011
    Location
    Abu Dhabi, UAE
    MS-Off Ver
    Excel 2010
    Posts
    234

    Click and drag VBA?

    Hello!

    We have a project that requires inputting names into a schedule. Right now, the users are typing the names in. I have pre-loaded the names into hidden rows above the columns so that the user only has to input enough unique letters to identify the name. However, it is still time-consuming.

    Control-dragging the upper corner of each cell works from the names column works, but it is difficult to always grab the upper corner, and it copies the conditional formatting, too. It also requires another mouse click to select the starting cell.

    We would like a simpler way to copy values from one cell to another. One idea would be to launch a macro, click on a cell, and clicking on a second cell automatically copies values to the second cell. This could continue until the macro is stopped. And the technique must NOT copy formatting to the destination cell.

    Any ideas appreciated!

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: Click and drag VBA?

    Why not just reference cell to be "copied" with something like this, copied down as far as you need...
    =IF(B16="","",B16)
    Then, when you need to enter actual data, just type over it
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Forum Contributor
    Join Date
    04-20-2015
    Location
    Switzerland
    MS-Off Ver
    2010
    Posts
    312

    Re: Click and drag VBA?

    If you want to have a vba solution you can do something like this
    Map Switchmode to a button for activating and deactivating the copy mode (you could also use a cell reference instead a Public variable)
    Then declare the range where your namelist is located in my case in column A

    When someone doubleclicks on anything in column A whole copymode is activated the text in that column will be placed in any cell that is selected outside of coulmn A

    Public Copyname As String
    Public copymode As Boolean
    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If copymode = True And Target.Column = 1 Then
        Copyname = Target.Value
        Cancel = True
    End If
    End Sub
    
    Sub switchmode()
    copymode = Not copymode
    Debug.Print copymode
    End Sub
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If copymode = True And Target.Column <> 1 Then
        Target.Value = Copyname
        Cancel = True
    End If
    End Sub
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    01-16-2011
    Location
    Abu Dhabi, UAE
    MS-Off Ver
    Excel 2010
    Posts
    234

    Re: Click and drag VBA?

    LordLoki - that's awesome! That's exactly the kind of solution we're looking for.

    Can I further refine the namelist range as rectangle of cells rather than a column?

    And is there a way to have the text on the button toggle between "Start Copying" and "Stop Copying"? Otherwise, I can have the macro change the text in an adjacent cell, or a color.
    Last edited by Hambone70; 06-08-2016 at 11:44 AM.

  5. #5
    Forum Contributor
    Join Date
    04-20-2015
    Location
    Switzerland
    MS-Off Ver
    2010
    Posts
    312
    Sure you can change the range to cover every space on the sheet. You could also change it so that it only pastes the names inside a certain area.
    I could change the button text too I will add an update tomorrow cause at the moment I am in the train.

    Greets
    Loki
    Quote Originally Posted by Hambone70 View Post
    LordLoki - that's awesome! That's exactly the kind of solution we're looking for.

    Can I further refine the namelist range as rectangle of cells rather than a column?

    And is there a way to have the text on the button toggle between "Start Copying" and "Stop Copying"? Otherwise, I can have the macro change the text in an adjacent cell, or a color.

  6. #6
    Forum Contributor
    Join Date
    08-10-2009
    Location
    nottingham
    MS-Off Ver
    Excel 2010
    Posts
    110

    Re: Click and drag VBA?

    Quote Originally Posted by LordLoki View Post
    cause at the moment I am in the train.
    Sounds painful

  7. #7
    Forum Contributor
    Join Date
    04-20-2015
    Location
    Switzerland
    MS-Off Ver
    2010
    Posts
    312

    Re: Click and drag VBA?

    Good Morning,

    @inq no clue what you are talking about :D

    @Hambone i made a few Changes to the code all you need to do is put the code on the worksheet where your shedule is then add an ActiveX Button to the sheet
    After that go to the code and put the name of that button in the 3 "buttonname" variables and specify the ranges correctly
    Then it should work as you wanted

    The text Of the Button and the Color will Change
    You can define any Range on the Worksheet as Name Range
    You can Define Any Range on the Sheet as Shedule Range
    And you need to Define one Cell as Reference to the Active Name

    I removed the Public Variables so the Current name is refered by a Cell and the Current Mode (Copy or Not) is Determined by the Color of the Button.

    Have Fun with it

    Private Sub CommandButton21_Click()
    Dim Buttonname As String
    
    'Just insert your Buttonname here and make sure its an ActiveX Button not a Form Button
    Buttonname = "CommandButton21"
    
    
    If ActiveSheet.OLEObjects(Buttonname).Object.BackColor <> vbRed Then
        Me.OLEObjects(Buttonname).Object.Caption = "Stop Copymode" 'change this text if needed
        Me.OLEObjects(Buttonname).Object.BackColor = vbRed
    Else
        Me.OLEObjects(Buttonname).Object.Caption = "Start Copymode" 'change this text if needed
        Me.OLEObjects(Buttonname).Object.BackColor = vbGreen
    End If
    
    End Sub
    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim Namelist As Range
    Dim activename As Range
    Dim Buttonname As String
    
    'Insert The name of the Button again and define the Range where your List of names is located
    'Also Specify a Cell that holds the current active name
    Buttonname = "CommandButton21"
    Set Namelist = Me.Range("A5:B30")
    Set activename = Me.Range("C2")
    
    If Me.OLEObjects(Buttonname).Object.BackColor = vbRed And Not Intersect(Target, Namelist) Is Nothing Then
        activename.Value = Target.Value
        Cancel = True
    End If
    
    End Sub
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Shedulerange As Range
    Dim Buttonname As String
    
    'And once again the name of your Button and activename
    'and the range where it should paste names when selected
    Buttonname = "CommandButton21"
    Set Shedulerange = Me.Range("D5:I20")
    Set activename = Me.Range("C2")
    
    If Me.OLEObjects(Buttonname).Object.BackColor = vbRed And Not Intersect(Target, Shedulerange) Is Nothing Then
        Target.Value = activename.Value
        Cancel = True
    End If
    
    End Sub
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    01-16-2011
    Location
    Abu Dhabi, UAE
    MS-Off Ver
    Excel 2010
    Posts
    234

    Re: Click and drag VBA?

    LordLoki

    Good afternoon! Thank you very much for your help. I am trying to learn how it works.

    I cannot get your last file to work. Do I need to make any modifications to your file to test it?

    I have attached a working copy of our weekly schedule (12-23 Jun). I incorporated your first VBA into the file. I temporarily put the names in Column A, and it works perfectly. However, I want to use the colored names in the 5 columns under each day of the week instead of the names in Column A. I am hoping that I can do this with named ranges.

    Also, the VBA doesn't work when I duplicate the weekly schedule for the following week.

    Thanks again for your help!
    Attached Files Attached Files
    Last edited by Hambone70; 06-09-2016 at 09:39 AM.

  9. #9
    Forum Contributor
    Join Date
    04-20-2015
    Location
    Switzerland
    MS-Off Ver
    2010
    Posts
    312

    Re: Click and drag VBA?

    The File i attached should work out of the box.
    If you put the code in your workbook you would need to change the variables for ranges etc.
    I did not know you copy the sheet for that i would change some references.

    Will check that later and upload a new code.
    Do you get an error using the last uploaded sheet?
    What exactly happens?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Slight Tweak to Click and Drag
    By diggetybo in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-21-2014, 05:57 AM
  2. Click and Drag not Calculating Properly
    By joelbrattin in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 12-20-2012, 06:05 AM
  3. How do I click and drag a formula changing only 1 value?
    By scottghansen79 in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 10-25-2010, 07:34 PM
  4. Click & Drag Formulae Problem
    By stuartbuckle in forum Excel General
    Replies: 4
    Last Post: 02-26-2009, 05:38 PM
  5. Click, Drag, Total- MISSING
    By emaldad in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 10-24-2006, 03:56 PM
  6. click & drag not working
    By Teresa in forum Excel General
    Replies: 2
    Last Post: 02-24-2006, 09:55 PM
  7. [SOLVED] Turn off click and drag in Excel???
    By Kimberly in forum Excel General
    Replies: 2
    Last Post: 01-11-2006, 11:40 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1