Question about the criteria format in match function

luissuarez

New Member
Joined
Mar 17, 2016
Messages
4
Code:
Sub Insertacts()


Dim sheetsch As Worksheet
Set sheetsch = Sheet1


Dim sheeteat As Worksheet
Set sheeteat = Sheet2


colnum = Application.WorksheetFunction.Match(sheeteat.Range("F2"), sheetsch.Range("A1:G1"))
rownum = Application.WorksheetFunction.Match(sheeteat.Range("G2"), sheetsch.Range("A1:A25"))


sheeteat.Range("A2").Copy
sheetsch.Cells(rownum, colnum).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheets("Schedule").Select


End Sub

Hello everyone! I am trying to create a timetable where activities can be added from another sheet. I wrote the above codes and it works smoothly except a few bugs.

The activity cannot be inserted into a correct row when I put the time of 13:00 or 16:00 in Range("G2") in Sheet2. It apprears on the row of 12:00 and 15:00 in Sheet1 respectively instead. Anyone knows why that is the case? Thanks a lot.

<a href=http://www.filedropper.com/timeschedule><img src=http://www.filedropper.com/download_button.png width=127 height=145 border=0/></a><br /><div style=font-size:9px;font-family:Arial, Helvetica, sans-serif;width:127px;font-color:#44a854;> <a href=http://www.filedropper.com >upload files online</a></div>
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
I suspect a number precision problem using the Match() function. You get the same incorrect row return using Match in the worksheets.
Set G2 of your Activities sheet to 13:00
=MATCH(G2,Schedule!A:A) returns 14 (should be 15)
=MATCH(G2,Schedule!A:A,0) returns #N/A (0 switch forces search for exact match)

VBA Find method did not find a result for me either. To work around, I ended up stepping through rows matching the integer of the value *24

Code:
Function RowNum()
Dim toFind As Long
toFind = Int(Sheets("Activities").Range("G2").Value * 24)

For Each c In Sheets("Schedule").Range("A2:A25")
    If Int(c.Value * 24) = toFind Then
        RowNum = c.Row
        Exit Function
    End If
Next c
End Function

You can either modify & use the Function above or alter you change your code but first give it a while to see if someone is able to provide a method that does not require stepping through cells.
 
Upvote 0
I suspect a number precision problem using the Match() function. You get the same incorrect row return using Match in the worksheets.
Set G2 of your Activities sheet to 13:00
=MATCH(G2,Schedule!A:A) returns 14 (should be 15)
=MATCH(G2,Schedule!A:A,0) returns #N/A (0 switch forces search for exact match)

VBA Find method did not find a result for me either. To work around, I ended up stepping through rows matching the integer of the value *24

Code:
Function RowNum()
Dim toFind As Long
toFind = Int(Sheets("Activities").Range("G2").Value * 24)

For Each c In Sheets("Schedule").Range("A2:A25")
    If Int(c.Value * 24) = toFind Then
        RowNum = c.Row
        Exit Function
    End If
Next c
End Function

You can either modify & use the Function above or alter you change your code but first give it a while to see if someone is able to provide a method that does not require stepping through cells.

Thanks for the help. But is it possible to change the format of Range("G2") in the MATCH function to hh:mm and do the searching in Range("A2:A25") with exact match? Perhaps it would work as well?
 
Upvote 0
Yes, Military time would be an option. Down your schedule sheet use
0
100
200
300
400
etc

Select Column A
Choose a custom format 0000
The display in cells with that format will be
0000
0100
0200
0300
0400
etc
Then use the same in G2 of your Activities sheet.

Your Match() in VBA will then return the correct row.

Whether you type, for example, 200 or 0200 in your Activities sheet, match will return the same result.
 
Last edited:
Upvote 0
Just thought of another way:

Set up as above but instead of customs format 0000 try custom format 00\:00
You need to type 1300 into the cell, not 13:00 when using that format
I did not test that using your code but it works with Match() at sheet level so I expect is should also work in VBA.
 
Upvote 0

Forum statistics

Threads
1,216,102
Messages
6,128,853
Members
449,471
Latest member
lachbee

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top