VBA code to find colunn number of specific data

raven75

New Member
Joined
Nov 5, 2019
Messages
4
Im using this code to color an specific row, from the starting column to the end column for a 4 weeks timeline graphic (not a grant chart) but since I'm using dates as headers the columns are not corresponding with the dates

can you please help me to find the column number (StartColumn) based on the date I need?
e.g.
11/05/2019 = column 5

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Sub coloring(EvenName As String, StartColumn As Integer, EndColumn As Integer, Row As Integer, EventColor As Long)
Static X As Integer
Cells(Row, StartColumn).Value = EentName
For X = StartColumn To EndColumn
Cells(Row, X).Interior.Color = EventColor
Next[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]End Sub[/FONT]
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Welcome to the Board!

I think your question would be much clearer if we could see what your data looks likes, and see exactly what you are trying to do.

You cannot upload files to this site. But there are tools you can use to post screen images. They are listed in Section B of this link here: http://www.mrexcel.com/forum/board-a...forum-use.html. Also, there is a Test Here forum on this board that you can use to test out these tools to make sure they are working correctly before using them in your question.
 
Upvote 0

Book1
BCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAE
14 week Period Timeline
2Start Date:10/27/2019Period 31
3
4week 1week 2week 3week 4Expected:
5oct    nov nov      nov      nov      
6sunmontuewedthufrisatsunmontuewedthufrisatsunmontuewedthufrisatsunmontuewedthufrisat
7Event:27282930311234567891011121314151617181920212223
8Hurricane SeasonHurricane seasonCheck mails / see Disaster recovery plan
Timeline Period 3
Cell Formulas
RangeFormula
K2=MATCH(C2,C7:AD7,0)
K5=IF(TEXT(K7,"mmm")=TEXT(J7,"mmm"),"",LOWER(TEXT(K7,"mmm")))
K6=LOWER(TEXT(K7,"aaa"))
K7=J7+1
C5=LOWER(TEXT(C7,"mmm"))
C6=LOWER(TEXT(C7,"aaa"))
C7=C2
D5=IF(TEXT(D7,"mmm")=TEXT(C7,"mmm"),"",LOWER(TEXT(D7,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XT(AC7,"aaa"))
AC7=AB7+1
AD5=IF(TEXT(AD7,"mmm")=TEXT(AC7,"mmm"),"",LOWER(TEXT(AD7,"mmm")))
AD6=LOWER(TEXT(AD7,"aaa"))
AD7=AC7+1
 
Upvote 0
I think you need to build the actual date, and then you can work off of that.
You could always do it in row 3, and change the font color to white if you want it "hidden".

If you are using American-style dates, you could place this formula in cell C3:
Code:
=DATEVALUE(C5 &" "&C7&", "&YEAR(TODAY()))
Then, you could put the following in D3 and copy across:
=C3+1

Just note that things could get a little tricky as you cross over years (since you don't seem to be entering/saving the year portion of the date anywhere).
 
Upvote 0
i was trying this:
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]StartDate = InputBox("Enter stating date", "Start Date")[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]StartColumn = WorksheetFunction.Match(StartDate, Range("C7:D7"), 0)

but it shows this error message:
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]unable to get the match property of the worksheetfunction class[/FONT]<strike></strike>
[/FONT]
<strike></strike>
 
Upvote 0
But you don't actually have a date in cell C7, you just have a number (27).
You want to make sure you are comparing "apples-to-apples" here.

Note that you should also first declare the variable type in your code to make sure it is taking the input as a date and not text or a number, i.e.
Code:
Dim StartDate as Date
 
Upvote 0
here is my code
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Sub Column()
Dim StartDate, PeriodStartDate As Date, StartColumn As Integer, Data As Integer[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]PeriodStartDate = Range("c2").Value
StartDate = InputBox("Enter stating date", "Start Date")
StartColumn = WorksheetFunction.Match(StartDate, Range("C7:D7"), 0)[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]
End Sub
and on the sheet the range from c7 to d7 have formulas to sum 1 day each cell from the starting date dates but the format is to show only the day
[/FONT]
 
Upvote 0
Dim StartDate, PeriodStartDate As Date, StartColumn As Integer, Data As Integer
Note that this does NOT declare "StartDate" as Date, but rather as Variant (since you do not have " As Date" after it; each variable needs to be declared explicitly).
So that will allow StartDate to be Text entries, which you do not want if you are comparing to dates.
 
Upvote 0
Cross posted https://www.excelforum.com/excel-programming-vba-macros/1295451-help-with-function.html

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,616
Latest member
intern444

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