+ Reply to Thread
Results 1 to 13 of 13

Macro for spreadsheets with different number of rows and a constant number of columns

  1. #1
    Registered User
    Join Date
    03-01-2014
    Location
    Ukraine
    MS-Off Ver
    Excel 2007
    Posts
    7

    Macro for spreadsheets with different number of rows and a constant number of columns

    Hello,

    I do not have experience with VBA that's why I'm asking for your help with the following macro. I work on a daily basis with spreadsheets in excel. The number of columns is the same, but every single spreadsheet has a different number of rows. I recorded this macro in a table with 1196 rows and I would like to use this macro also in other tables with a different number of rows. I would be more than grateful if you could help me with this issue. Thank you.

    Sub City2()
    '
    ' City2 Macro
    '

    '
    Selection.Copy
    Cells.Replace What:="POMPANO", Replacement:="Pompano Beach", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Range("B1195").Select
    Application.CutCopyMode = False
    Selection.Copy
    Cells.Replace What:="COCOCRK", Replacement:="Coconut Creek", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Range("B1193").Select
    Application.CutCopyMode = False
    Selection.Copy
    Cells.Replace What:="MARGATE", Replacement:="Margate", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Range("B1192").Select
    Application.CutCopyMode = False
    Selection.Copy
    Cells.Replace What:="OAKPARK", Replacement:="Oakland Park", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Range("B1190").Select
    Application.CutCopyMode = False
    Selection.Copy
    Cells.Replace What:="DEERFLD", Replacement:="Deerfield Beach", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Range("B1187").Select
    Application.CutCopyMode = False
    Selection.Copy
    Cells.Replace What:="TAMARAC", Replacement:="Tamarac", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Range("B1175").Select
    Application.CutCopyMode = False
    Selection.Copy
    Cells.Replace What:="NLAUDER", Replacement:="North Lauderdale", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Range("B1151").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Sheet2").Select
    Cells.Replace What:="LAUDLAKE", Replacement:="Lauderdale Lakes", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Range("B1036").Select
    Application.CutCopyMode = False
    Selection.Copy
    Cells.Replace What:="CORALSPR", Replacement:="Coral Springs", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Range("B963").Select
    Application.CutCopyMode = False
    Selection.Copy
    Cells.Replace What:="FORTLAUD", Replacement:="Fort Lauderdale", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Range("B897").Select
    Application.CutCopyMode = False
    Selection.Copy
    Cells.Replace What:="SUNRISE", Replacement:="Sunrise", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Range("B866").Select
    Application.CutCopyMode = False
    Selection.Copy
    Cells.Replace What:="LAUDHILL", Replacement:="Lauderhill", LookAt:=xlPart _
    , SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Range("B710").Select
    Application.CutCopyMode = False
    Selection.Copy
    Cells.Replace What:="LHPOINT", Replacement:="Lighthouse Point", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Range("B680").Select
    Application.CutCopyMode = False
    Selection.Copy
    Cells.Replace What:="LAUDBSEA", Replacement:="Lauderdale by the Sea", _
    LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _
    False, ReplaceFormat:=False
    Range("B359").Select
    Columns("B:B").ColumnWidth = 20.86
    Range("B290").Select
    Application.CutCopyMode = False
    Selection.Copy
    Cells.Replace What:="PARKLAND", Replacement:="Parkland", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Range("B177").Select
    Application.CutCopyMode = False
    Selection.Copy
    Application.CutCopyMode = False
    Cells.Replace What:="SEARANCH", Replacement:="Sea Ranch Lakes", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Range("B17").Select
    Selection.Copy
    Cells.Replace What:="HILLSBRO", Replacement:="Hillsboro Beach", LookAt:= _
    xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Range("B1").Select
    End Sub

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Macro for spreadsheets with different number of rows and a constant number of columns

    Some additional information is needed.
    From you code we can see the relation between "Range("B1195").Select" and "1196 rows" but what about others range selection.
    To help seriously a file will be welcome

  3. #3
    Registered User
    Join Date
    03-01-2014
    Location
    Ukraine
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Macro for spreadsheets with different number of rows and a constant number of columns

    Hello,

    Please find attached to this message the database. You will see that if you run the macro, the abbreviations from the column B are going to be replaced with the full name of the city. I have also other macros for this database which help me to replace the majority of the abbreviations. It would be awesome if you could tell me the code that I have to insert in the macro and where, because I would like to adapt the other macros too.

    Thank you!
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Macro for spreadsheets with different number of rows and a constant number of columns

    MORACH,
    The macro don't work correctly because there is some selection done which fail.
    The need for column B is to replace abbreviations by full name, there is some more flexible way to do instead to replace cells by cells:
    use formula (inside the code) or correspondance table
    Send a table to do the link between abbreviations and full name, something like:
    Column A Column B
    POMPANO Pompano Beach

  5. #5
    Registered User
    Join Date
    03-01-2014
    Location
    Ukraine
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Macro for spreadsheets with different number of rows and a constant number of columns

    Here's a table containing all the abbreviations that I use in my spreadsheets.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    03-01-2014
    Location
    Ukraine
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Macro for spreadsheets with different number of rows and a constant number of columns

    This is a link to the final version of the database.

    https://www.dropbox.com/s/qhxxkv4t4z...ngleHouse.xlsm

    There are many steps to follow until I obtain this version of the database, as you will see in the additional sheets. That's why it will simplify a lot my work a macro which can help me skip the "find & replace" the abbreviations from the initial database. I hope you can help me out with this issue.

    Thank you very much.

  7. #7
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Macro for spreadsheets with different number of rows and a constant number of columns

    Here we are
    The macro is placed in file "Table of correspondance1.xlsm" which is the toolbox.
    The macro is working for the active sheet: Select a cell where the treatment has to be done to select the right sheet
    There is some names used for each set of data : City_Table for City etc..
    Inside these named range update the correspondance if needed
    Treatment is done today only for column B and is using name City_Table, update the macro for the others columns and names.
    Note: When a value is not found to make the correspondance the cell is painted in red


    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by PCI; 03-03-2014 at 05:51 PM. Reason: Attach file

  8. #8
    Registered User
    Join Date
    03-01-2014
    Location
    Ukraine
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Macro for spreadsheets with different number of rows and a constant number of columns

    Hi,

    Thank you for your help. I tested the macro and it works. I saved the macros for the other tables. I was wondering if you could show me how to define a range of more columns in the macro, because for example for the equipment I have more than 1 column, and if I run this macro it will replace only the abreviations from the first column. I tried to apply it for the F2:S1925, but it didn't work. I'm not sure how to do it. Maybe when you have time you could take a look and try to figure it out. Please find attached the file.

    Thanks again for your help
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Macro for spreadsheets with different number of rows and a constant number of columns

    See here the macro prepared for Equipment
    The range for column F to S has to prepared in :
    Please Login or Register  to view this content.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    03-01-2014
    Location
    Ukraine
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Macro for spreadsheets with different number of rows and a constant number of columns

    The macro is working great! I really appreciate your help

    Thank you.

  11. #11
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Macro for spreadsheets with different number of rows and a constant number of columns

    Good news,
    I don't know if you noticed that when there is no data found in the correspondance table the cells are painted in Red.
    Here after the macro without this option
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    03-01-2014
    Location
    Ukraine
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Macro for spreadsheets with different number of rows and a constant number of columns

    Thanks a lot, I paid more attention to the abbreviations and the to the lines to make sure that the lines do not get scrambled or something like that. I noticed that where I have OTHER or NONE, after I run the macro those words are replaced with 0 instead of blank space. Hm... I don't understand why.

  13. #13
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Macro for spreadsheets with different number of rows and a constant number of columns

    "those words are replaced with 0 instead of blank space." that is because the corresponding value to OTHER or NONE is an empty cell: put a space
    Attached Files Attached Files

+ 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. [SOLVED] if cell above number equals Value, sum all number across all rows and columns
    By Portermt in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-27-2013, 06:20 PM
  2. [SOLVED] Define a range of fixed number of columns, but unlimited number of rows
    By ccopac in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-22-2012, 02:23 PM
  3. Replies: 2
    Last Post: 09-25-2012, 09:08 AM
  4. [SOLVED] Count rows that meet or exceed changeable % and divide by constant number
    By acasper3 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-23-2012, 02:29 PM
  5. [SOLVED] Built-in Excel constant for maximum number of rows, for columns?
    By Howard Kaikow in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-07-2006, 10:20 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