I have created a userform to track service requests and I am having trouble with this last part.
There are 2 columns in question the first "A" is service ID the second is "B" Date. I want "B" to be today's date and "A" to start at 1 and count up, when I add a new request tomorrow I want "A" to start at 1 again.
Here is the code for column "A", when a new record is created "A" goes up but when the date changes it does not start back at 0. Any ideas?
'Service ID
'check row above
'if "Date" then out put "1"
'if yesterday then out put "1"
'if today then ouput + 1
If (Sheet1.Cells(Rows.Count, "B").End(xlUp).Value) = "Date" Then
serviceorder = "1"
ElseIf (Sheet1.Cells(Rows.Count, "B").End(xlUp).Value) < Date Then
serviceorder = "1"
ElseIf (Sheet1.Cells(Rows.Count, "B").End(xlUp).Value) = Date Then
serviceorder = (Sheet1.Cells(Rows.Count, "A").End(xlUp).Value) + 1
Else
End If
Thanks in advance.
Why not just make column A a COUNTIF formula to show the number of times the date in the corresponding column B cell shows up in all of column B? That way when the date changes, it will auto reset to 1 and each new time a request is entered the same day, it the ID will increment by 1. The VBA formula would be:
With Sheet1.Cells(Rows.Count, "A").End(xlUp).Offset(1)
.Formula = "=COUNTIF(B:B,B" & .Row & ")"
'Uncomment this next line if you want column A to contain values instead of formulas
'If you do uncomment it, make sure you have already put the date in the B column for this entry
'.Value = .Value
End With
I think the general problem in your functions is the Sheet1.Cells(Rows.Count, "B").End(xlUp). Rows.Count means - as it says - the count of rows in your sheet and is 1048576. In my opinion you take the very last cell of the column and End(xlUp) won't make it better. I think Sheet1.Cells(1, "B").End(xlUp) should work better. Sorry if I'm wrong for I can't reproduce your scenario here.
Related
I would like to change the range in the counfif-formula by using vba. By clicking a button the Range A3:A3 changes to A3:A4, then clicking the button again A3:A4 changes to A3:A5, and so on... I managed to create a constant vba-formula, but I do not know how to proceed. Anybody? :)
Excel
VBA
I think you need a dummy cell/value to achieve, The dummy cell will "count" how many times you have clicked the button. My solution will require a dummy cell.
Sub Countif_function()
Dim start_value As Long
start_value = Cells(1, "D").Value 'Take the value from cell D1
If start_value <= 3 Or Cells(1, "D").Value = "" Then start_value = 3 ' If the value is less than 3 (since you start at row 3) or if the value is empty then set the minimum start value to 3 (otherwise countif will fail).
Cells(3, "D").Value = Application.WorksheetFunction.countif(Range(Cells(3, "A"), Cells(start_value, "A")), "A") 'Using the inbuild function in VBA to retrieve the countif
Cells(3, "E").Value = "=COUNTIF(A3:A" & start_value & ",""A"")" 'Just for view purpose of the final formula result, can be ignored in the code
Cells(1, "D").Value = Cells(1, "D").Value + 1 'Add +1 row. This will be used as the start value for next time the code is executed.
End Sub
I used "A" as criteria since you used it in your example code, but could be change to "C3" or some other cell.
Result:
The alternate if you don't want a dummy cell to count as per the previous answer is to read the formula in the cell into a string variable via range.formula, split it on the comma, read the last digit of the first entry in the split array, increment by 1. (If the digit is a 9, then check whether the preceding character is a number or letter, if letter, make the 9 a 10, if number, increment that by one and make the 9 a 0, if the preceding character is also a 9, recursive call). So, yeah, unless you really cannot have the dummy cell, use that solution, it's easier.
I have spreadsheets that list the dates in column A, ex 12/1/2016 1:45:00 AM. I reformat all the cells in this column so they display Day, Month #, Year. Example Thursday, December 01, 2016. The Find function works if I click on the actual find button and search for the values, however I am struggling to code this. I want to delete all the rows that contain "Saturday" and "Sunday". The latest code I have tried is as follows;
Last = Cells(Rows.Count, "D").End(xlUp).Row
For i = Last To 1 Step -1
If (Cells(i, "D").Value) = "*Saturday*" Then
Cells(i, "A").EntireRow.Delete
End If
Next i
I have also tried the following code as well;
Set Find = Range("A:A").Find("Saturday", LookIn:=xlValues)
Do Until Find Is Nothing
Find.EntireRow.Delete
Set Find = Range("A:A").FindNext
Loop
Set the LookAt Parameter to xlPart
Find("Saturday", , , xlPart)
Change
If (Cells(i, "D").Value) = "*Saturday*" Then`
to
If (Cells(i, "D").Text) Like "*Saturday*" Then
Using Text instead of Value will access the displayed value, rather than the underlying value (which is a date), and using Like instead of = will allow wildcards to work.
Your question is confusing as to which column the dates are in. Some of your code (and your question) suggests they are in column A, but other parts of your code suggest column D. If they are in column A, change the code above to use "A" instead of "D".
I've got an amount of data copied from a table in a .pdf that when pasted into excel puts it all into one column. There are actually multiple pages each with it's own table (the data is one continuous long table split over multiple pages more accurately) and at the top of each page is a series of lines that I'm not interested in (the same unwanted data is at the top of each page). What I am interested in is re-sorting the data under the headers as it is in the table on the original .pdf document, removing the headers in the process. The data as it has been pasted into one column essentially is a list of items in plain text for x rows, followed by a list of start dates for x rows, and then a list of end dates for x rows, repeated every page.
I've figured out how to count the number of lines I don't want by getting a macro to look for the first piece of data I'm interested in ("AAAA") starting at cell (B2);
Cells(2, 2).Select
For i = 1 To 50
If ActiveCell = "AAAA" Then
Exit For
End If
ActiveCell.Offset(1, 0).Select
Next i
Cells(2, 3) = i
If i = 51 Then
Range("B3") = "Cannot find data"
End If
Which starts a search at cell (B2) looking downwards until it finds "AAAA" it then prints how many rows it has moved downwards to find it in cell (C2).
I now wish to be able to start at the cell it has just found [(B34) in this case] and count downwards until it finds the first cell containing a date.
Ultimately I'll need to then count down the same number of cells to find the associated end date and print them all in one row, continuing for the entire column of data.
If anybody could help me with being able to start at the first cell "AAAA" and then count downwards until a date is found, that would be really helpful.
My biggest challeng is to understand what you want to be true. I tryed to make a list of the things what you want.
You have a PDF that when paste in Excel it transform all the
document in one column.
There is a header in each of the Excel pages that you want to delete.
After you find a header you want to find two dates, and they have the same distance from the header.
How I would do it:
For iCounter = 1 to Cells(1048576, 1).End(xlUp).Row
If Cells(iCounter,1) = "YOUR HEADER HERE" then
For kCounter = iCounter to Cells(1048576, 1).End(xlUp).Row
If IsDate(Cells(kCounter,1)) = true then
initialDate = Cells(kCounter,1)
endDate = Cells(2*kCounter-iCounter,1)
End if
Next kCounter
End if
Next iCounter
The following piece of code starts in cell A1 and searches downward until it finds a cell containing a date value. The code only searches until it reaches the last record in the first column (to avoid searching all the way down to the bottom of the sheet if no date is found).
Sub FindFirstDate()
Dim i As Long
For i = 1 To ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
If IsDate(ActiveSheet.Cells(i, 1).Value) = True Then Exit For
Next i
MsgBox "The first cell with a date is " & ActiveSheet.Cells(i, 1).Address
End Sub
In this example the address of the cell with the first date in returned in a MsgBox.
When recording a macro, i want to change the current value of a cell to a new value dependant on the original value. not set it to a "recorded" value.
Example : value in cell needs to change from .1234 (text) to 0,1234 (number). And
.56789 to 0,56789
My work method is:
record macro
"F2" : to change value,
"home" : go to beginning,
"del",
"del",
"0",
",",
"return",
stop record macro
but when i use the macro on other fields, the value is changed to 0,1234 even when original value is .5678 for example.
This can be done in VBA but the most simple solution (if all your values begin with ".") is to use the built in "Text to Columns" Excel option (by simply selecting your entire column then ALT+A, +E, +F). If this has to be done with VBA please tell. Hope this alone helps.
Edit I've wrote this code to solve your problem (based on the rule of the cell values are starting with "." and then switching to numbers is made by adding a "0" to the initial text). Then "number" format is picked up by Excel as implicit format.
Sub ChangeFormat()
For i = 1 To 2 'This is the row index (it's now set to the 1st 2 rows.)
For j = 1 To 2 'This is the column Index (i.e. column 2 is B, 1 is A, etc.) (it's now set to the A and B columns)
If Left(ActiveSheet.Cells(i, j), 1) = "." Then
ActiveSheet.Cells(i, j).Value = "0" & ActiveSheet.Cells(i, j).Value
End If
Next j
Next i
End Sub
I keep finding answers to select last in COL but what i am looking for is how to make cell show last active cell in ROW
For example you set dates of when someone bought something and you dont want to look all the way to last active cell but have that one cell which updates with last active cells input and you know immediatly when that customer was last time contacted
http://i39.tinypic.com/2wnbrxy.jpg
FinalRow = Range("A65536").End(xlUp).Row
do While trim(cells(FinalRow,1)) = ""
FinalRow = FinalRow - 1
Loop
FinalColumn = Range("IV1").End(xlToLeft).Column
Do While trim(Cells(1,FinalColumn)) <> ""
FinalColumn = FinalColum - 1
Loop
Assuming all data is contiguous you could use something like this as a formula:
=INDIRECT("A" & COUNTA(A:A))
Otherwise you can get the last row in VBA like this:
ActiveSheet.UsedRange.Rows.Count