+ Reply to Thread
Results 1 to 26 of 26

insert column into a macro

  1. #1
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    487

    insert column into a macro

    Hello to all.
    This macro is for finding or replacing codes in sheets.
    Works in all cells.
    Is it possible to change it because it works only for column O?
    Thank you
    john

    Please Login or Register  to view this content.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: insert column into a macro

    Hi there,

    I haven't looked at your code in detail, but you really should NOT begin a routine with an open-ended:

    Please Login or Register  to view this content.

    Doing this means that you have greatly reduced the control you have on how your code behaves if an error is encountered.


    Regards,

    Greg M

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,475

    Re: insert column into a macro

    I don't see how the code will only work for column O

  4. #4
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    487

    Re: insert column into a macro

    is it better to remove?

    on error resume next

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: insert column into a macro

    Hi again,

    Yes indeed, it is DEFINITELY better to remove it in this case. It's quite possible that after doing this your code will show an error message when you run it, but at least you'll have some indication of what the error is, and where it occurred. At present your code might be generating several errors, but you have no idea whether or not it is.


    In many circumstances the "On Error Resume Next" statement can be very useful, but it should always be paired with an "On Error GoTo 0" statement to ensure that error handling is re-enabled as soon as possible.

    The following routine shows how disabling error handling and then re-enabling it (almost immediately afterwards) can be used to detect whether or not a specific worksheet exists:

    Please Login or Register  to view this content.

    Hope this helps.

    Regards,

    Greg M

  6. #6
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    487

    Re: insert column into a macro

    Thanks greg, I take off on error resume next.

  7. #7
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    487

    Re: insert column into a macro

    Hello to all.
    Has anyone figured out how to fix the problem

  8. #8
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,475

    Re: insert column into a macro

    Refer to post #3

  9. #9
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    487

    Re: insert column into a macro

    Hi davesexcel.
    But an alternative code is not possible?

  10. #10
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,475

    Re: insert column into a macro

    Nowhere in the code does it only work for Column O, I tested the code and it works for the entire sheet..

    If it only works for column O on your sheet, you will have to supply the sample WB.

  11. #11
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    487

    Re: insert column into a macro

    The macro post #1 works on all columns
    I ask if it is possible to change by column O

  12. #12
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,598

    Re: insert column into a macro

    Please Login or Register  to view this content.
    **English to Italian via DuckDuckGo search engine
    Last edited by protonLeah; 09-09-2021 at 05:56 PM.
    Ben Van Johnson

  13. #13
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    487

    Re: insert column into a macro

    Hi protonleah work well, thank you.
    I added lines to see how many codes were found, but it doesn't work well
    Please Login or Register  to view this content.

  14. #14
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,598

    Re: insert column into a macro

    "...Doesn't work well..." Isn't enough information. In your latest code, you set the desired search column to "E". But, in the countif line, you are searching the entire sheet: CountIf(Cells, TrovatoSu). Do you want to count the whole sheet or just the search column ("E")?

    Also, you are testing for numeric value from the inputbox but I don't know if you are searching for numeric or text values...

    IsNumeric(TrovatoSu) will fail if TrovatoSu is a text value

  15. #15
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    487

    Re: insert column into a macro

    Hi protonleath,
    must search for both numbers and text and search column ("E")
    Last edited by john_cash; 09-12-2021 at 12:56 AM.

  16. #16
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    487

    Re: insert column into a macro

    Hi to all.
    I corrected it but it doesn't work the message correctly
    result_1 = Application.WorksheetFunction.CountIf(Columns(Col), TrovatoSu)

    Please Login or Register  to view this content.
    example:
    clik in button all column cells
    FINDS COLUMNS = I
    FIND = CIAO
    result = 2
    REPLACE = CIAO2

    the resut it must be 7
    Attached Files Attached Files
    Last edited by john_cash; 09-14-2021 at 07:04 AM.

  17. #17
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,598

    Re: insert column into a macro

    In column I, you have:

    CIAO1O
    CIAO1O
    CIAO1O99
    CIAO1O44
    CIAO1O55
    CIAO1O15
    CIAO1O23
    You search for the string: CIAO and the first instance is at row 8, (CIAO1O).

    Your COUNTIF function is then counting instances of CIAO1O not CIAO --> 2 vs. 7
    So, try a wildcard count:

    Please Login or Register  to view this content.
    Last edited by protonLeah; 09-14-2021 at 11:15 PM.

  18. #18
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    487

    Re: insert column into a macro

    Hi I added the change but it doesn't change, it always results in quantity 1 .
    Example:

    button "tutte le celle colonna" = N
    button "trova" = ABCD
    result = 1

    exact result = 11
    Attached Files Attached Files

  19. #19
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,598

    Re: insert column into a macro

    Maybe you should count the original search value,e.g. ABCD:
    result_1 = Application.WorksheetFunction.CountIf(Columns(Col), VecchioValore & "*")

  20. #20
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,598

    Re: insert column into a macro

    You are searching for both numeric and text values. The cells with numeric values, F8=148, are formatted as general. But the value from the inputbox is a string so when passed to the COUNTIF function will return 0. So I added code to test if the source data is numeric and convert it to string for the count..
    Please Login or Register  to view this content.

  21. #21
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    487

    Re: insert column into a macro

    Thank you protonleath, the modification works fine.

  22. #22
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    487

    Re: insert column into a macro

    edit thread
    Last edited by john_cash; 09-16-2021 at 01:43 PM.

  23. #23
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    487

    Re: insert column into a macro

    I tried to modify it to find all numbers like macro post #20
    It doesn't work well.

    button "tutte le celle"
    button "trova" = 51
    result = 16

    exact result = 20

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by john_cash; 09-16-2021 at 01:45 PM.

  24. #24
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    487

    Re: insert column into a macro

    Hi

    result_1 = Application.WorksheetFunction.CountIf(Cells, VecchioValore) '<<< work
    Else
    result_1 = Application.WorksheetFunction.CountIf(Cells, "*" & VecchioValore & "*") 'not work

    51 'found
    51 'found
    51 'found
    51 'found
    51 'found
    51 'found
    51 'found
    51 'found
    51 'found
    a-51 'not found
    51 'found

    51 'found

    51-abcd 'not found
    51 'found

    123456-51 'not found
    51 'found
    51 'found
    51 'found
    51 'found

  25. #25
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,475

    Re: insert column into a macro

    Please Login or Register  to view this content.
    What is this for?

    You are changing the variable value. The fist one it finds is 51, so it will count 51 only.
    Last edited by davesexcel; 09-18-2021 at 10:23 AM.

  26. #26
    Forum Contributor
    Join Date
    10-31-2018
    Location
    venezia
    MS-Off Ver
    2007
    Posts
    487

    Re: insert column into a macro

    Please Login or Register  to view this content.
    Hi,
    if in inputbox "trova" = value not present in the sheet
    the macro goes into debug

+ 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. Replies: 3
    Last Post: 10-04-2016, 08:19 AM
  2. Macro Needed for contains word in column a insert mark in column b
    By melisaball in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-09-2015, 10:45 AM
  3. Macro for extracting data from a long column and insert it in its own column with a header
    By ChemistryStudent in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-07-2014, 04:41 AM
  4. [SOLVED] Macro to Insert column and include a VLOOKUP array for entire column
    By bdouglas1011 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-29-2014, 06:28 PM
  5. [SOLVED] Copy Column as Selection and Insert to the next Empty Column MACRO
    By kaurka in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 04-22-2013, 12:50 PM
  6. [SOLVED] Macro is inserting cells in one column, need it to insert in 2nd column as well
    By MR22 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-01-2012, 10:23 AM
  7. Macro to search column with last value and insert TOTAL column to the left
    By sbara906 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-02-2009, 11:34 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