Trying to speed up code/improve coding skills based off of oz grid article

  • Hello,


    I'm trying to improve my vba skills and I came out across this article from ozgrid on speeding up macros. I've looked at my own code and cleaned up some things like changing "" into vbNullString, but the two things that I don't know how to use from this article are changing If then into booleans and how not to use "select" with the code that I am running. The code in question is below:




    Thank you for any help!

  • Hi,


    I am not a trained programmer, but have rewritten in the way I would achieve it without .select, it may not be what you are after but you should be able to modify to suit:


    Let me know how you go.


    Cheers

    Justin

  • You are Looping to find TextBox2's Value. This could be replaced with the VBA Find Function. However, your code then checks if the active cell is empty.


    Do you mean if the value is not found then enter the value from TextBox2?

  • Hello gents, thank you for the replies!

    Do you mean if the value is not found then enter the value from TextBox2?

    Yes. If the cell is blank, then insert the value from the textbox. If it's not blank, then move to the next cell to the right and do the same.


    Justin I'll give your code a go and see if I can get it to work!

  • Just noticed that line 26:

    Code
     CorrectRow = Application.Match(DD, ws.Range(MyRnge), 0) - 1

    should be this:


    Code
     CorrectRow = Application.Match(DD, ws.Range(MyRnge), 0) + 1

    and you can remove line 15:

    Code
    MsgBox DD

    HTH

    Justin

  • Roy,


    He appears to calculate the difference in days between a specific date and your chosen date on the form, matches that number of days in column A then fills a range an equivalent number of days into the future from the chosen date in column D (inserting the value in blank cells, and I just noticed inserting the value into the next empty column if a value already exists in D (or whichever is the next blank column)). Whether this occurs on sheet 1,2 or 3 or all three, is determined by the selection of the comboboxes... Is there a way to achieve that without using loops? I did not include the filling of the next column as I missed that he was changing column in the else, will have a look at getting that to work...

  • I Think changing the if function on line 32 to:


    Code
     If c.Value = vbNullString Then
     c.Value = TB2 'checks blank cells
     Else
     ws.Cells(c.Row, c.Column + ws.Range(c.Address).Offset(0, -1).Value).Value = TB2
     End If

    gets it there.

  • I think this is what is required.


  • Hello gents, so my "master" workbook is very involved. I have "groups of guests" arriving on certain dates and leaving on other dates. On my master workbook the above code posts both to a sheet with the same name as the value in the combo box and both to a master dates sheet. The master dates sheet is setup in a similar fashion to the sheets in the examples.


    I have another sheet setup to look like the month of October. On any given day in October I've set that day's cell equal to the respective cell containing the counta on the master dates sheet. So after a team is added they could look at the month of October and see "oh, there are five groups coming from October 1st to October 4th." The point of it posting into a row is for a macro that will show all the teams coming on that day when ran. So not only can the client see at a glance, "oh there are five groups coming from October 1st to October 4th, but if I click this button I can see the particular teams on October 1st, 2nd etc." The different locations come in so not only can I see what teams are coming in, but also where they'll be staying.


    I can give you the "master" if you would like, but it'll be tomorrow at the soonest since I'll have to remove some information.

  • No need from my perspective to post further detail, but it would be good if you could clarify whether you 1. require code to fill every blank cell in a range, stepping across to the next blank column where required and stopping once you match the textbox value, or 2. whether you just need to find the textbox value and fill the first available column in the matched row... The second I believe you can do without a loop ... the first I would be interested to know if you can achieve without a loop.

  • I believe that the intention is to set up something like an appointment. The code that I have posted finds the CurrenttRow then adds two to find the row to write to. It then adds the entry to the next empty cell in that row. No Loops involved.

  • No need from my perspective to post further detail, but it would be good if you could clarify whether you 1. require code to fill every blank cell in a range, stepping across to the next blank column where required and stopping once you match the textbox value, or 2. whether you just need to find the textbox value and fill the first available column in the matched row... The second I believe you can do without a loop ... the first I would be interested to know if you can achieve without a loop.

    The first. My above code finds the respective reference date (I'm matching a number here because its easier to match than a date), then offsets to the column where it should begin pasting. If it sees that there is no value in the respective cell then it'll paste the text box value. If it sees that there is a value, it'll move one column to the right. If it sees that the textbox has the same value, it'll move down to the next row.

  • The code that I posted uses the date not the numbers. I didn't realise if the entry matches to move down to the next row. Why is that, it would be a different date?

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!