+ Reply to Thread
Results 1 to 12 of 12

Unable to select columns with columns.("D:D").select

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Unable to select columns with columns.("D:D").select

    Hi this is really starting to annoy me now. Everytime I try to select a column in VBA I get runtime error 1004 "Select method of range class faailed" I'm sure I'm just being an idiot but nothing seems work. I've tried different versions of the same thing but they don't work. Am i doing something really stupid? I've included my code just in case. Thanks.

    Private Sub CB_Bakery_Click()
    
    Dim wsM As Worksheet, wsM2 As Worksheet
    Dim LRow2 As String
    
    'sets the variables wsM equal to Main worksheet and wsM2 equal to Main2
    Set wsM = Sheets("Main")
    Set wsM2 = Sheets("Main2")
    
    'sets variable LRow2 equal to the last row in column A of the
    'activesheet that contains data
    LRow2 = ActiveSheet.Range("A65535").End(xlUp).Row
    
    'select the Main worksheet
    wsM.Select
    
    'select all the cells
    wsM.UsedRange.Select
       'autofilter and copy to wsM2
       Selection.AutoFilter Field:=3, Criteria1:="Bakery"
       wsM.Columns("b:b").Copy wsM2.Range("a1")
       wsM.Columns("d:d").Copy wsM2.Range("b1")
       wsM.Columns("f:f").Copy wsM2.Range("c1")
       wsM.Columns("q:q").Copy wsM2.Range("e1")
       wsM.Columns("r:r").Copy wsM2.Range("f1")
       wsM.Columns("g:g").Copy wsM2.Range("g1")
       wsM.Columns("bm:bm").Copy wsM2.Range("h1")
       wsM.Columns("bn:bn").Copy wsM2.Range("i1")
       wsM.Columns("bo:bo").Copy wsM2.Range("j1")
       wsM.Columns("bp:bp").Copy wsM2.Range("k1")
        
    'select Main2 worksheet
    wsM2.Select
    
    
        
        'put a value in column D cell D1
        wsM2.Range("D1").Value = "Due"
            'value in D2 equal to the formula
            wsM2.Range("D2").Value = "=c2-(TODAY()-182)"
            'copy the formula
            wsM2.Range("D3").Copy
            'paste it into the remaining cells for the column up until the last row
            ActiveSheet.Range("D4:D" & LRow2).Select
            Selection.PasteSpecial
            Application.CutCopyMode = Fallse
            
        
    'set conditional formating THE NEXT LINE CAUSES THE ERROR
        Columns("D:D").Select
        Selection.FormatConditions.Delete
        Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
            Formula1:="30"
        Selection.FormatConditions(1).Font.ColorIndex = 2
        Selection.FormatConditions(1).Interior.ColorIndex = 2
        Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
            Formula1:="1", Formula2:="30"
        Selection.FormatConditions(2).Font.ColorIndex = 2
        Selection.FormatConditions(2).Interior.ColorIndex = 37
        Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
            Formula1:="=TODAY()-182"
        Selection.FormatConditions(3).Font.ColorIndex = 2
        Selection.FormatConditions(3).Interior.ColorIndex = 9
        
    wsM2.Select
    'set widths
        Columns("A:A").ColumnWidth = 15
        Columns("B:B").ColumnWidth = 12
        Columns("C:C").ColumnWidth = 7
        Columns("D:D").ColumnWidth = 4
        Columns("E:E").ColumnWidth = 7
        Columns("F:F").ColumnWidth = 7
        Columns("F:F").EntireColumn.AutoFit
        
    End Sub

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Unable to select columns with columns.("D:D").select

    use Range instead of Columns. ;-)
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Unable to select columns with columns.("D:D").select

    Thanks for replying but same problem.

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Unable to select columns with columns.("D:D").select

    Oh-your code is in a worksheet module so if you refer to Range or Columns without specifying a worksheet it refers to the sheet containing the code and you can't select anything on that sheet unless it is active.
    Private Sub CB_Bakery_Click()
    
        Dim wsM As Worksheet, wsM2 As Worksheet
        Dim LRow2            As String
    
        'sets the variables wsM equal to Main worksheet and wsM2 equal to Main2
        Set wsM = Sheets("Main")
        Set wsM2 = Sheets("Main2")
    
        'sets variable LRow2 equal to the last row in column A of the
        'activesheet that contains data
        LRow2 = ActiveSheet.Range("A65535").End(xlUp).Row
    
        'select the Main worksheet
        With wsM2
    
            'select all the cells
            wsM.UsedRange.AutoFilter Field:=3, Criteria1:="Bakery"
            wsM.Columns("b:b").Copy .Range("a1")
            wsM.Columns("d:d").Copy .Range("b1")
            wsM.Columns("f:f").Copy .Range("c1")
            wsM.Columns("q:q").Copy .Range("e1")
            wsM.Columns("r:r").Copy .Range("f1")
            wsM.Columns("g:g").Copy .Range("g1")
            wsM.Columns("bm:bm").Copy .Range("h1")
            wsM.Columns("bn:bn").Copy .Range("i1")
            wsM.Columns("bo:bo").Copy .Range("j1")
            wsM.Columns("bp:bp").Copy .Range("k1")
    
            'put a value in column D cell D1
            .Range("D1").Value = "Due"
            'value in D2 equal to the formula
            .Range("D2").Formula = "=c2-(TODAY()-182)"
            'copy the formula
            .Range("D3").Copy Destination:=.Range("D4:D" & LRow2)
    
    
            'set conditional formating THE NEXT LINE CAUSES THE ERROR
            With .Columns("D:D")
                .FormatConditions.Delete
                .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
                                      Formula1:="30"
                .FormatConditions(1).Font.ColorIndex = 2
                .FormatConditions(1).Interior.ColorIndex = 2
                .FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
                                      Formula1:="1", Formula2:="30"
                .FormatConditions(2).Font.ColorIndex = 2
                .FormatConditions(2).Interior.ColorIndex = 37
                .FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
                                      Formula1:="=TODAY()-182"
                .FormatConditions(3).Font.ColorIndex = 2
                .FormatConditions(3).Interior.ColorIndex = 9
            End With
    
            'set widths
            .Columns("A:A").ColumnWidth = 15
            .Columns("B:B").ColumnWidth = 12
            .Range("C:C,E:F").ColumnWidth = 7
            .Columns("D:D").ColumnWidth = 4
            .Columns("F:F").EntireColumn.AutoFit
        End With
    End Sub

  5. #5
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Unable to select columns with columns.("D:D").select

    Told you I was stupid! does that make a difference?

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Unable to select columns with columns.("D:D").select

    what - being stupid? :-P

  7. #7
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Unable to select columns with columns.("D:D").select

    hehehe cheers. I REALLY appreciate that (and the houmour!) Thanks for your help.
    Its orking great appart from it dosn't seem to put the formula into column D

            'put a value in column D cell D1
            .Range("D1").Value = "Due"
            'value in D2 equal to the formula
            .Range("D2").Formula = "=c2-(TODAY()-182)"
            'copy the formula
            .Range("D3").Copy Destination:=.Range("D4:D" & LRow2)
    I need the formula =c2-today()-182 to be in cells D2 down to last used row but increasing c2 like dragging right corner. Any ideas?

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Unable to select columns with columns.("D:D").select

    sorry - could not resist!
    change the last 3 lines to
    .Range("D2:D" & lrow2).Formula = "=c2-(TODAY()-182)"

  9. #9
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Unable to select columns with columns.("D:D").select

    sorry? I've currently got

        'put a value in column D cell D1
            .Range("D1").Value = "Due"
            'value in D2 equal to the formula
            '.Range("D2").Formula = "=c2-(TODAY()-182)"
            'copy the formula
            .Range("D2:D" & LRow2).Formula = "=c2-(TODAY()-182)"
    which puts the formula into d1 and d2 but thats all.

  10. #10
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Unable to select columns with columns.("D:D").select

    in that case you have the wrong sheet active when this runs
        LRow2 = ActiveSheet.Range("A65535").End(xlUp).Row
    change that to
    LRow2 = wsM2.Range("A65535").End(xlUp).Row
    or better
    LRow2 = wsM2.Cells(wsM2.Rows.Count, "A").End(xlUp).Row

  11. #11
    Forum Contributor
    Join Date
    05-14-2012
    Location
    England
    MS-Off Ver
    Excel 2010/2013
    Posts
    100

    Re: Unable to select columns with columns.("D:D").select

    You genius!!!!! Thanks thats brilliant. I can now get the main part done from there. Thank you again.

  12. #12
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Unable to select columns with columns.("D:D").select

    glad to be of service.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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