Consulting

Results 1 to 11 of 11

Thread: Variable column in code

  1. #1
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location

    Variable column in code

    Hi all and thanks in advance for your help
    My question is. Is there a way i can change this code so that the column it is searching i.e. "A" can be variable because i want to take this column from a textbox in a form?
    Im having a mind blank

    [vba]
    Option Explicit

    Public Sub ProcessData()
    Const TEST_COLUMN As String = "A"
    Dim i As Long
    Dim LastRow As Long
    Dim NextRow As Long
    Dim this As Worksheet
    Dim sh As Worksheet

    With Application

    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    End With

    Set this = ActiveSheet
    With this

    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    For i = 2 To LastRow

    Set sh = Nothing
    On Error Resume Next
    Set sh = Worksheets(.Cells(i, "A").Value)
    On Error Goto 0
    If sh Is Nothing Then

    Worksheets.Add after:=Worksheets(Worksheets.Count)
    Set sh = ActiveSheet
    sh.Name = .Cells(i, "A").Value
    .Rows(1).Copy sh.Range("A1")
    End If

    NextRow = sh.Cells(sh.Rows.Count, "A").End(xlUp).Row + 1
    .Rows(i).Copy sh.Cells(NextRow, "A")
    Next i

    End With

    With Application

    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With
    End Sub [/vba]
    Cheers

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    [VBA]
    Option Explicit

    Public Sub ProcessData()
    Dim IB as string
    IB=Inputbox("Enter Column Letter To Use", "Code Column Adjustment")
    Const TEST_COLUMN As String = IB
    Dim i As Long
    Dim LastRow As Long
    Dim NextRow As Long
    Dim this As Worksheet
    Dim sh As Worksheet

    With Application

    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    End With

    Set this = ActiveSheet
    With this

    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    For i = 2 To LastRow

    Set sh = Nothing
    On Error Resume Next
    Set sh = Worksheets(.Cells(i, "A").Value)
    On Error Goto 0
    If sh Is Nothing Then

    Worksheets.Add after:=Worksheets(Worksheets.Count)
    Set sh = ActiveSheet
    sh.Name = .Cells(i, "A").Value
    .Rows(1).Copy sh.Range("A1")
    End If

    NextRow = sh.Cells(sh.Rows.Count, "A").End(xlUp).Row + 1
    .Rows(i).Copy sh.Cells(NextRow, "A")
    Next i

    End With

    With Application

    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With
    End Sub
    [/VBA]when you run this code now a box will pop up asking you fro which column!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    A trimmed down version because it's a quiet day.

    Be wary about turning you calculations off. If you code bombs out, there is nothing to reset it to automatic, which can cause problems if you don't notice.

    [VBA]
    Public Sub ProcessData()
    Dim IB As String
    Dim i As Long
    Dim ws As Worksheet
    Dim rng As Range
    IB = InputBox("Enter Column Letter To Use", "Code Column Adjustment")
    Set rng = ActiveSheet.Range(Cells(1, IB), Cells(1, IB).End(xlDown))
    For i = 2 To rng.Cells.Count
    On Error Resume Next

    Set ws = Sheets(CStr(rng(i)))
    If ws Is Nothing Then
    Set ws = Worksheets.Add(after:=Worksheets(Sheets.Count))
    With ws
    .Name = rng(i)
    rng(1).EntireRow.Copy .Range("A1")
    rng(i).EntireRow.Copy .Range("A2")
    End With
    End If
    Set ws = Nothing
    Next
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Quote Originally Posted by mdmackillop
    Be wary about turning you calculations off. If you code bombs out, there is nothing to reset it to automatic, which can cause problems if you don't notice.
    Malcolm, the only code I have in personal is to set calculations to on when any workbook is opened and it is for the very reason you cite.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    Quote Originally Posted by mdmackillop
    A trimmed down version because it's a quiet day.

    Be wary about turning you calculations off. If you code bombs out, there is nothing to reset it to automatic, which can cause problems if you don't notice.

    [vba]
    Public Sub ProcessData()
    Dim IB As String
    Dim i As Long
    Dim ws As Worksheet
    Dim rng As Range
    IB = InputBox("Enter Column Letter To Use", "Code Column Adjustment")
    Set rng = ActiveSheet.Range(Cells(1, IB), Cells(1, IB).End(xlDown))
    For i = 2 To rng.Cells.Count
    On Error Resume Next

    Set ws = Sheets(CStr(rng(i)))
    If ws Is Nothing Then
    Set ws = Worksheets.Add(after:=Worksheets(Sheets.Count))
    With ws
    .Name = rng(i)
    rng(1).EntireRow.Copy .Range("A1")
    rng(i).EntireRow.Copy .Range("A2")
    End With
    End If
    Set ws = Nothing
    Next
    End Sub

    [/vba]
    Thanks for this it works well but is there a way to get it to copy all instances in the search column to the new sheet instead of just 1?

    Thanks again

  6. #6
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    I want to put all new instances from a search column into a new sheet named after the instance.

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you explain or post a book showing what you mean. There is nothing to do that in your original code.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    This should clear things up

  9. #9
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    Please help

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Public Sub ProcessData2()
    Dim IB As String
    Dim i As Long
    Dim ws As Worksheet
    Dim rng As Range
    IB = InputBox("Enter Column Letter To Use", "Code Column Adjustment")
    Application.ScreenUpdating = False
    Set rng = ActiveSheet.Range(Cells(1, IB), Cells(1, IB).End(xlDown))
    For i = 2 To rng.Cells.Count
    On Error Resume Next
    Set ws = Sheets(CStr(rng(i)))
    If ws Is Nothing Then
    Set ws = Worksheets.Add(after:=Worksheets(Sheets.Count))
    With ws
    .Name = rng(i)
    rng.AutoFilter 1, rng(i)
    rng.SpecialCells(xlCellTypeVisible).EntireRow.Copy .Range("A1")
    End With
    End If
    Set ws = Nothing
    Next
    rng.AutoFilter
    Sheets(1).Activate
    Application.ScreenUpdating = True
    End Sub[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    Many thanks, worked perfectly

Posting Permissions

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