VBA InputBox Issue

Anonymous
2021-03-05T18:47:39+00:00

I have a simple macro. To copy values from one workbook to another in this case ML to MCR based on the user input number.

Number format can be e.g M5888-9087, M1990A 600111512, 453564034421 822349224.

Macro works with numbers like M5888-9087 but if I enter M1990A 600111512 or 453564034421 822349224 it copies all the rows instead of filtering M1990A 600111512 or 453564034421 822349224 and copying those selected rows.

Space in between is the culprit. How to overcome this?

Does changing the filter helps? I see other filters in object library.

Sub MCR()

Dim myInp As Variant

myInp = VBA.Interaction.InputBox(Prompt:="Enter the number", Title:="MCR Macro")

Windows("ML.xlsx").Activate

Worksheets("Full-View").Select

Worksheets("Full-View").ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:=myInp

Range("Table1[[#Headers],[Serial]]").Select

ActiveCell.Offset(1, 7).Range("Table1[[#Headers],[Serial]]").Select

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

Selection.Copy

Windows("MCR Macro.xlsm").Activate

Range("A4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

:=False, Transpose:=False

End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

99 answers

Sort by: Newest
  1. Anonymous
    2021-03-19T04:31:03+00:00

    Since your are wiling to understand the data set, I will align the new data set to the code already written and give more clarity. 

    Of course, you are free to ask any question on the data set I am happy to answer.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2021-03-19T04:20:21+00:00

    You understood the steps very well for Scenario 1. Like I said if I walk you through the problem it is much easy.

    Yes, it has to be saved in .xlsx not in .xlsm only template file needs to be .xlsm.

    I can make up a new data set for you again if you really want to understand it. You would need it for Scenario 2 anyway.

    I am new to VBA learned it few weeks ago from online courses. Right of the bat, I cannot write a code without some help.

    Instead of putting you through the pain of making you understand the data set. I feel it is good to walk you through it step by step. That is how I understood the data set in the beginning. 

    Original data set has lot of unnecessary data for writing the macro and might make you feel overwhelmed at first.

    Was this answer helpful?

    0 comments No comments
  3. OssieMac 48,006 Reputation points Volunteer Moderator
    2021-03-18T06:24:44+00:00

    @ExcelFunTension,

    Jeovany is absolutely correct in that it makes it so hard to provide the correct answer to your problem if we have not got all of the information. Thanks Jeovany for backing me up with that because I was hoping for full information from the OP.

    However, lets see if I am understanding correctly. (Edited since initial posting.)

    1. You initially requested code to provide an InutBox for the numbers in the first column of the Source data and search and copy the data from the rows relative to the Input to an Output workbook.
    2. Now you want to replace the InputBox code to automatically create an output workbook for each group of unique values in the range instead of having to enter each number into the InputBox.
    3. After copying and pasting the matching data, perform some formatting on the Output (borders etc)
    4. Then after each unique value group is copied to the Output workbook and formatted, you want to save to an Output workbook with a file name created from 2 columns of the data.
    5. Then start with a clean template output sheet and process the next unique number group and repeat until workbooks have been created for all unique number groups.
    6. However, a question. What type of file to you want the Output saved as. Currently the Output file is a Macro Enabled but I am thinking that you want to save the individual Output files as a standard xlsx (without the macro). Please confirm if this is correct because it makes a difference to the coding required. Note that if a standard xlsx then all macros are removed from the Output file but you won't lose them from the main processing file.

    I have written code up to point 5 except for the type of file and I need an answer to point 6 before continuing.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2021-03-17T16:40:28+00:00

    Hi 

    I read the entire thread and downloaded the files

    When ready to dive into the problem I ran into the same issues our colleague OssieMac mentioned

    The following comments have the sole purpose to clarify and find out the best way to help you to solve the problem. In any way are meant to criticize you or your work. Feel free to express your opinion if you feel otherwise. 

    Also bear in mind, English is not my native language.

    So my apologies in advance.

    Regarding,

    1)"I have trimmed many columns down.

    Like I said the original data set has 40+ columns and 10,000 rows.

    It makes no sense to include them all so I made a sample data set for demonstration..."

    Visiting this forum looking for help/solution to a problem is like visiting the Doctor, but with the great advantage of not having to pay a dime for it.

    Yet it is very important and advisable to provide the best, the accurate, the correct, and sometimes the smallest/silly (from our point of view) details.

    Even though we might correctly-ish.... assume what our symptoms are or what the doctor would prescribe to us

    To the point,

    a) In your case is important and "It does make sense" to provide the real dimension of your real scenario.

    Because our answer/approach to a possible solution depends on it

    b) My gut feeling tells me there is a different approach to solve your problem. One in which some of the brilliant macros given by OssieMac might not be necessary (simply due to the approach)

    c) In addition to the limited information, in the way you comment about your scenario and goals, you unwillingly leading us to take your approach of doing things, making us thinking in the way you envision the solution should be found.

    Regarding,

    2) "...You can just add the additional code to the original code from what you understand.

    I will adjust it to my data set..."

    I think this is the reason all of the above-mentioned is happening.

    Resuming, 

    Kindly suggest you 

    1. Provide us with a fully loaded file (specially column-wise)
    2. Step by step details of the data input process

    You may use WeTransfer for sharing bigger files

    https://www.youtube.com/watch?v=NnXsE0SNuCc&t=14s

    I will read the details you provided again in order to find/understand the order of your data input process

    I will come back to you with more questions if need it

    Regards

    Jeovany

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2021-03-17T08:48:40+00:00

    Yes, you can't run the code directly. I have trimmed many columns down.

    Like I said the original data set has 40+ columns and 10,000 rows. 

    It makes no sense including them all so I made a sample data set for demonstration.

    I was trying to explain the whole process with words from the beginning cuz data set may not make much sense unless you know how they are related. 

    You can just add the addition code to the original code from what you understand. I will adjust it to my data set.

    Using the unique number/numbers. I do a lookup from the wsSource table and save those files  to put it simple. 

    How to keep those range of unique numbers in the memory and process them one at a time with relevant filters and save them.

    It is better we execute it by breaking down step by step instead of trying to understand the dataset.  

    Scenario 1

    We need to adjust the code to store a range of unique numbers in the memory instead of one unique number and pull the data from wsSource one at a time and save them with the unique number and description in any cell of column 2 Databodyrange in wsSource as file name.

    Scenario 2

    is tricky this has to be a separate macro we shall get back to it once we complete Scenario 1.

    Was this answer helpful?

    0 comments No comments