Close

Results 1 to 12 of 12
  1. #1
    DF VIP Member
    Gazzr's Avatar
    Join Date
    Dec 2004
    Location
    Manchesta!
    Posts
    317
    Thanks
    2
    Thanked:        3
    Karma Level
    251

    Newbie Excel Macro basics

    Hi all,
    I am new to macros and would hope that somebody could assist.
    I have an exported csv file from a database that i need to reformat and would like to create a macro to aid this.

    The part of the task that i am struggling with is the following:
    (This example has only 20 rows of data)
    I insert a new column between A and B
    I insert a formula into B1 (="Item No: "&a1)
    I copy this down to B20
    I then copy B1:B20 and use paste special>values into A1:A20
    I then delete Column B

    If i record a macro, it obviously only works up to row 20, but the spreadsheet "grows" each time i need to do this and i would like the macro to automatically perform the action for all the populated rows present in column A.
    I have had a look at the VBA code and can see each time it refers to the cell range(s). I just don't know what i need to insert in place of these absolute references.

    Ta in advance.

  2. #2
    DF VIP Member cassy34's Avatar
    Join Date
    Nov 2004
    Location
    Lytham
    Posts
    1,453
    Thanks
    246
    Thanked:        181
    Karma Level
    360

    Default Re: Excel Macro basics

    Range("A65536").End(xlup).Row will give you the number of the last used row in col A

  3. #3
    DF VIP Member
    Gazzr's Avatar
    Join Date
    Dec 2004
    Location
    Manchesta!
    Posts
    317
    Thanks
    2
    Thanked:        3
    Karma Level
    251

    Default Re: Excel Macro basics

    Thanks for the info. Could you please assist me in grafting that code into the following example:

    Sub Macro2()
    '
    ' Macro2 Macro
    '

    '
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "=""Item No: ""&RC[-1]"
    Range("B1").Select
    Selection.AutoFill Destination:=Range("B1:B20"), Type:=xlFillDefault
    Range("B1:B20").Select
    Selection.Copy
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Columns("B:B").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Range("B1").Select
    End Sub

  4. #4
    DF VIP Member akimba's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    2,846
    Thanks
    1,034
    Thanked:        783
    Karma Level
    369

    Default Re: Excel Macro basics

    I know this is a bit old now but when recording macros instead of highlighting the range with the cursor Range(B1:B20)

    Hold down ctrl and shift and press down, this will give you the following code in your macro instead

    Range(Selection, Selection.End(xlDown)).Select

    Which will then work with however many rows you have ;-)
    Adverts Removed - please contact admin if you require advertising prices

  5. #5
    DF VIP Member
    Gazzr's Avatar
    Join Date
    Dec 2004
    Location
    Manchesta!
    Posts
    317
    Thanks
    2
    Thanked:        3
    Karma Level
    251

    Default Re: Excel Macro basics

    Akimba, thanks. I never did solve this problem.

    I have had another look at this and used your advice. I now have the following. My only problem is highlighted in red:

    Range("B1").Select
    ActiveCell.FormulaR1C1 = "=""Item: ""&RC[-1]"
    Range("B1").Select
    Selection.AutoFill Destination:=Range("B1:B20"), Type:=xlFillDefault
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Range("B1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.ClearContents

    End Sub

    I need to copy the formula down as many rows as column A has. In this case, there were 20 rows in A so i copied from B1:B20. I need to automatically ascertain how many rows A has. I think Cassiy34 has the answer but i do not know how to use the code he has written to achieve what i require.

    Thanks for the help.

  6. #6
    DF VIP Member Over Carl's Avatar
    Join Date
    Apr 2006
    Location
    London
    Posts
    13,125
    Thanks
    3,975
    Thanked:        1,690
    Karma Level
    1252

    Default Re: Excel Macro basics

    Haven't messed with these for a good few years but I'm guessing below may be what you need?

    Sub Macro2()
    '
    ' Macro2 Macro
    '

    '
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "=""Item No: ""&RC[-1]"
    Range("B1").Select
    Selection.AutoFill Destination:=Range("A65536").End(xlup).Row,Type:=xlFillDefault
    Range("B1:B20").Select
    Selection.Copy
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Columns("B:B").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Range("B1").Select
    End Sub

    Thanks to Over Carl

    ap0c (21st September 2011)  


  7. #7
    DF VIP Member ap0c's Avatar
    Join Date
    Sep 2007
    Location
    UK
    Posts
    937
    Thanks
    132
    Thanked:        38
    Karma Level
    259

    Default Re: Excel Macro basics

    My attempt:

    Range("B1").Select
    ActiveCell.FormulaR1C1 = "=""Item: ""&RC[-1]"
    Range("B1").Select
    Selection.AutoFill Destination:=Range("B1:B" & Range('A65536').End(xlUp).Row), Type:=xlFillDefault
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Range("B1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.ClearContents

    End Sub
    Last edited by ap0c; 21st September 2011 at 02:41 PM.
    Never take friendship personal.

    Thanks to ap0c

    Over Carl (21st September 2011)  


  8. #8
    DF VIP Member
    Gazzr's Avatar
    Join Date
    Dec 2004
    Location
    Manchesta!
    Posts
    317
    Thanks
    2
    Thanked:        3
    Karma Level
    251

    Default Re: Excel Macro basics

    Sorry fella's - neither work...!

    Basically what i wish to do is convert a column of numbers:
    Column A
    (A1) 125
    (A2) 237
    (A3) 328
    (A4) 764

    into the following column of text and original numbers {in the same column}
    Column A
    (A1) Item number: 125
    (A2) Item number: 237
    (A3) Item Number: 328
    (A4) Item Number: 764

  9. #9
    DF VIP Member ap0c's Avatar
    Join Date
    Sep 2007
    Location
    UK
    Posts
    937
    Thanks
    132
    Thanked:        38
    Karma Level
    259

    Default Re: Excel Macro basics

    I think I have sussed what you were after:

    macro.zip
    Last edited by ap0c; 23rd September 2011 at 11:24 AM.
    Never take friendship personal.

    Thanks to ap0c

    Gazzr (3rd November 2011)  


  10. #10
    DF VIP Member ap0c's Avatar
    Join Date
    Sep 2007
    Location
    UK
    Posts
    937
    Thanks
    132
    Thanked:        38
    Karma Level
    259

    Default Re: Excel Macro basics

    New version with better error checking!

    Bored at work sorry

    macro.zip
    Never take friendship personal.

  11. #11
    DF VIP Member akimba's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    2,846
    Thanks
    1,034
    Thanked:        783
    Karma Level
    369

    Default Re: Excel Macro basics

    Hi

    Ap0c seems to have a good answer there

    I use a Function I wrote called LastRow t opredict the last row of data, I have copied it just incase you need it in the future


    'AlanKimber 18/06/2010
    'Works out the last row of data in a column of a worksheet
    'if there is a blank row in the data then this will be picked up
    'sColumn = the column you want to look in
    'iStart = the Starting cell i.e. might not want to start at 1 (A1 for instance)
    'sWorksheet = worksheet you want to search in
    Function LastRow(sColumn, iStart, sWorksheet) As Integer
    Dim lrn
    lrn = iStart
    Do Until Worksheets(sWorksheet).Range(sColumn & Trim(Str(lrn))) = ""
    lrn = lrn + 1
    Loop
    LastRow = lrn - 1
    End Function

    Add this to your VBA and call it in the Sub like so

    Selection.AutoFill Destination:=Range("B1:B" & CSTR(LastRow("A",1,"Sheet1")), Type:=xlFillDefault
    Adverts Removed - please contact admin if you require advertising prices

  12. #12
    DF VIP Member
    Gazzr's Avatar
    Join Date
    Dec 2004
    Location
    Manchesta!
    Posts
    317
    Thanks
    2
    Thanked:        3
    Karma Level
    251

    Default Re: Excel Macro basics

    Thanks a lot fellas. I now have the following:
    macro2.rar
    I export a csv from my database, copy and paste the first three columns into the "macro.xls" - click the button and hey presto! All are converted. (Text added, columns resized) I then resave as a csv, delete all the rows that i do not need then send the csv to a label printer. Brilliant.

    Thanks again.

Similar Threads

  1. excel for fee
    By bobbobb in forum Programming
    Replies: 8
    Last Post: 7th March 2005, 11:22 PM
  2. upper in excel
    By ceasar in forum PC Software
    Replies: 2
    Last Post: 29th March 2003, 02:02 PM
  3. DVD player £44.42 + pnp (multi/macro off)
    By biggy7 in forum Cheapskates Corner
    Replies: 63
    Last Post: 20th March 2003, 03:59 PM
  4. Excel graph problem
    By marktb in forum PC Problems
    Replies: 2
    Last Post: 9th October 2002, 04:42 PM
  5. EvoX Dash Basics
    By oveR_Ride in forum Microsoft Consoles
    Replies: 4
    Last Post: 20th September 2002, 08:40 AM

Social Networking 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
  •