Results 1 to 9 of 9

Thread: need help

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    7

    need help

    i totally new to this VB. and also macro in excel.
    i need to do basically searching copy and paste.
    for example
    enter name to active cell then the macro will search for the name and copy all the data.on other sheet then patse to the active sheet
    please any body can help me

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: need help

    Welcome to the Forums.

    To start learning VBA easiest way is to record a macro doing the tasks you need. Then stop recording and check out the generated macro code in the VBA IDE (Alt+F11).

    Your going to want to look at the .Find method.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    7

    Re: need help

    tanks for the head start..
    but rite now i`m stuck..
    here is my plan..
    i`ve stated 1 variable let say name
    so i want macro to search the name in other sheet then if found macro will copy all the data beside the name than transfer to previous sheet..
    the thing is i donnot know how to move the activecell .. can assign active cell as variable

  4. #4
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: need help

    rexxar:

    Is the name on the "other" sheet(s) always in a particular column? Are you moving data from the reference sheet from particular columns to the same columns or to different columns on the active sheet? Are you copying ALL of the data on the row from the reference sheet to the active sheet? What happens if you don't find the selected name on the reference sheet? Please be a little more specific with the format of the data. What you are trying to do is not difficult.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: need help

    Dont use Name as a variable as its a reserved word.

    Post your code.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    7

    Re: need help

    sory man i`ve been away for quite some time..

    this is my code


    VB Code:
    1. Sub DecideUserInput()
    2. Dim bText As String
    3. Dim bsheet As String
    4. Dim rng As Range
    5. Dim rng2 As Range
    6. Dim i As Integer
    7. Dim y As Integer
    8. Dim r As Long
    9. Dim c, d As Integer
    10. Dim c1 As Integer
    11. Dim c2 As Integer
    12. Dim a As Integer
    13. Dim b As Integer
    14.  
    15.  ' here is the INPUTBOX-function :
    16.     bText = InputBox("Insert in a text", "This accepts any input")
    17.     ' here is the INPUTBOX-method :
    18.     ActiveCell.FormulaR1C1 = bText
    19.     Set rng = Sheet1.UsedRange
    20.     'Set rng2 = Sheet1.UsedRange.Rows
    21.    'a = Selection.Column.Value        ' i tried to extract the location of the active cell so that i can get row and column number
    22.    'b = Selection.row.Value
    23.    
    24.     Worksheets("sheet2").Select
    25.      Range("b1").Select
    26.    ' initial state
    27.    c = 4 'colloum
    28.    i = 1 'row
    29.    d = a + 1
    30.    c1 = c + 1
    31.    c2 = c
    32.    Do
    33.    For i = 1 To 60
    34.    If Sheet2.Cells(i, c).Value = bText Then
    35.            Do
    36.              For y = c1 To 8
    37.                 Sheet2.Cells(i, y).Copy
    38.                 Sheet1.Select
    39.                 Sheet1.Cells(b, d).Paste
    40.               Next y
    41.            Loop Until y = 8
    42.            
    43.             Sheet2.Select
    44.             Sheet2.Cells(i, c2).Copy
    45.             Sheet1.Select
    46.             Sheet1.Cells(1, 3).Select
    47.             activeselection.Paste
    48.    End If
    49.    Next i
    50.    Loop Until i = 60
    51.    
    52.    
    53.    End Sub

    it`s not pretty because i try and error.. one thing, when i tried to run this code my excel spreadsheet crash..

  7. #7
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: need help

    If you really want to learn what goes on in VBA, make the very top line of your module "Option Explicit". Setting this option requires that you, the programmer, must properly define and declare each and every variable. If you don't set this option, VBA will make some assumptions and automatically assign data types to variables if you don't. This can lead to some unusual problems, such as if you misspell a variable name ... VBA will just create and use a new variable with the incorrect spelling!

    Also, remember that you can get a lot of helpful code templates by using the Macro Recorder wizard. Just record snippets of operations and look at the code that is produced for 'hints'.

    In the above code, I think there is a problem with variables "Sheet1" and "Sheet2". Here is how I set "Handles" for sheets and copy data:
    Code:
    Dim srcSheet As Worksheet  'Handle for Source Worksheet
    Dim dstSheet As Worksheet  'Handle for Destination Worksheet
    
    'Set the handles for the Source and Destination Sheets
    Set srcSheet = Sheets("Sheet2")
    Set dstSheet = Sheets("Sheet1")
    'Copy a cell from the Source Sheet to the Destination Sheet
    srcSheet.Cells(1, c2).Copy
    dstSheet.Paste (dstSheet.Cells(1, 3))
    
    'At the end of your code, clean up all of the "Set" objects
    'This is not absolutely required, but is a good habit to get into
    Set srcSheet = Nothing
    Set dstSheet = Nothing
    Last edited by Webtest; Mar 28th, 2006 at 08:38 AM.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    7

    Re: need help

    tanx man,,,..
    just one question.. how to extract row and column from range..
    for example range("a2") i want to get the index number. and represent them in variable so that i can manipulate

  9. #9
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: need help

    "Row" returns the first Row Index in a range
    "Rows.Count" returns the number of Rows in a range

    "Column" returns the first Column Index in a range
    "Columns.Count" returns the number of columns in a range
    Code:
    aRow = Range("C5:F17").Row
    aCol = Range("C5:F17").Column
    MsgBox aRow & Chr(10) & aCol
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

Posting Permissions

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



Click Here to Expand Forum to Full Width