I basically have a date entered in cell N3 and a column (column I) that contains date sorted by date. I am trying to write a code that asks the user to input a date at first. Then the program would look for that date in the column I and delete ALL the rows after the row in which the match was found.
My code is somehow working, but here is my problem. Only a part of the rows under the wanted row are being deleted and not all of them. I was thinking that could be an issue with the for-loop or the counter?
Please let me know if you could help :)
Sub DeleteAllRowsPaymentTooFar()
Worksheets("Master").Activate
Dim date_max As Date
date_max = InputBox("What is the maximum date to filter? ")
Range("N3").Value = date_max
Range("N3").NumberFormat = ("ddddd, mmmmmmmmm d, yyyy")
Dim i As Long
i = Range("A4").End(xlDown).Row
Dim result As Integer
Dim str1 As String, str2 As String
Dim counter As Long
str1 = Cells(3, 14).Text
counter = 4
Do While counter <= i
str2 = Cells(counter, 9).Text
result = StrComp(str1, str2, vbTextCompare)
If result <> 0 Then
MsgBox ("The date is not yet reached.")
Else
For K = counter To i
Worksheets("Master").Rows(K).EntireRow.Delete
Next
End If
counter = counter + 1
Loop
End Sub
I expect to remove all the rows under the row where the match was found.
This may help you:
For i=LastPoint To FirstPoint Step-1
Worksheets("Master").Rows(i).EntireRow.Delete
Next i
Related
I am trying to split a column that contains a combination of Date and Time into two columns, where date and time are separated.
Column C contains a combination of date and time, for example "2022-01-01 09:30:00".
This should be split into Date in Column D and Time in Column E, in the format "dd.mm.yyyy" and "hh:mm":
Column D with 01.01.2022
Column E with "09:30"
I need to compare with a different sheet, where they are in this format.
Although I managed to split Date and Time into two columns the Time format is wrong.
I found suggestions to use Int() to get the date, and then subtract to get the time, however my date seems to be string. I tried to format my column to a Date datatype by using the Cdate function, however this resulted in an error.
As I don't necessarily need the value to have this datatype, I thought I could work with the Left() and Right() function. This first gave a problem but by including a string in between, I am getting closer to what I want.
Dim iAircol As Integer
Dim lastrow As Integer
Dim i As Integer
Dim str1 As String
Dim str2 As String
Dim spacepos as Int
iAircol= Worksheets(ws).Cells.Find(What:="Airdate", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
lastrow = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastrow
spacepos = InStr(Cells(i, iAircol), " ")
str1 = Left(Cells(i, iAircol).Value, spacepos)
Cells(i, iAircol + 1) = str1
str2 = Left(Right(Cells(i, iAircol).Value, Len(Cells(i, iAircol)) - spacepos), 6)
Cells(i, iAircol + 2) = str2
Next i
Time value still is in "hh:mm:ss":
I give the cell the first 5 characters of the total time, so no idea why it ends up with all 8 characters again, and this should be a string now, but Debug.Print gives me the Type "Date" for the date, and a Double for the Time.
Use DateValue and TimeValue, they are exactly for this:
Cells(i, iAircol + 1) = DateValue(Cells(i, iAircol))
Cells(i, iAircol + 2) = TimeValue(Cells(i, iAircol))
Then apply the Format you prefer to the two date and time columns, as these will hold true DateTime values, not text.
Please, use the next function to split the string as you need:
Function splitDateTime(strTime As String) As Variant
Dim d As Date, t As Date, arrD
arrD = Split(Split(strTime, " ")(0), "-")
d = DateSerial(CLng(arrD(0)), CLng(arrD(1)), CLng(arrD(2)))
t = CDbl(CDate(Format(Split(strTime, " ")(1), "hh:mm")))
splitDateTime = Array(d, t)
End Function
It can be tested like this:
Sub testSplitDateTime()
Dim arr, ac As Range
Set ac = ActiveCell 'in the active cell should be the string to be split/converted...
arr = splitDateTime(ac.value)
ac.Offset(0, 2).EntireColumn.NumberFormat = "HH:mm"
Range(ac.Offset(0, 1), ac.Offset(0, 2)).value = arr
End Sub
I currently have a "For Next" loop that iterates through various years and I want to modify it to loop through dates, specifically the end of each month. My generic code for the year loop is below. Clearly looping through years is relatively easy since you have a start year, which is an integer, and the iteration is 1. Now I want to modify the loop to iterate though various end of month dates. For example, 1/31/2003, 2/28/2003, ......, 12/31/2007. Also, note that for each iteration I create a new worksheet with the name of the current iteration as the name of the worksheet. Again, this is relatively easy for a year but using a date with a "/" complicates things. Does anyone have any ideas for creating a loop using end of month dates as well as creating sheets using dates? I do have an array of the dates so the code could refer to the array within a sheet. And the name of the sheet could be in any format. For example, "mm-dd-yyyy".
Sub YearLoop()
Dim FirstYr As Integer
Dim LastYr As Integer
Dim Sheetname As String
Dim Counter1 As Single
FirstYr = Sheets("Model").Range("ax15").Value
LastYr = Sheets("Model").Range("ax16").Value
Counter1 = 0
For J = FirstYr To LastYr
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = J
Sheetname = J
'do stuff
Counter1 = Counter1+1
Next
End Sub
The DateSerial function produces the end-of-month date of the previous month when you give any month a day of zero.
dim m as integer
for m = 2 to 13
debug.print dateserial(2016, m, 0)
next m
The characters that can't be used in sheet names are ASCII \/[]*:?, but you can use Unicode characters like ⁄∕/
d = #1/31/2003#
While d <= #12/31/2007#
Sheets.Add(, ActiveSheet).Name = Replace(d, "/", ChrW(8260))
d = d + 32
d = d - Day(d)
Wend
Update
Or you can use Jeeped's answer like this:
For m = FirstYr * 12 + 2 To LastYr * 12 + 13
Sheets.Add(, ActiveSheet).Name = Replace(DateSerial(0, m, 0), "/", ChrW(8260))
Next
Public Sub ReadAndDisplay()
' Get Range
Dim rg As Range
Set rg = ThisWorkbook.Worksheets("Returns Calc").Range("C118:C319")
' Create dynamic array
Dim Arr() As Variant
' Read values into array from sheet1
Arr = rg
For Each mark In Arr
Dim CurrentDate1 As Date, DimCurrentDate2 As String
CurrentDate1 = mark
CurrentDate2 = Replace(CurrentDate1, "/", ".")
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = CurrentDate2 & " Rtns"
'do Stuff
Next mark
End Sub
I have a question about excel (hopefully on the right forum)
I have a data of 100 numbers in excel and I want to randomly choose 30 numbers with the fact that the same number is not chosen again (so by removing the number that was already selected)
And I come across by not knowing on how to do that?
I tried with RANDBETWEEN(1;100) and copying it 30 times but it is/can repeat the same number.
Could you please offer me assistance on how to do that?
Thank you.
..............
Is there a way to do this with worksheat formulas instead of using VBA -that some other forun questions suggest?
.......
Here is some Excel VBA Code which should do the trick
Sub RandomUniquiNumber()
Dim NumberArray As Variant
ReDim NumberArray(100)
Dim NumberArrayPosition As Long
For NumberArrayPosition = 1 To 100
NumberArray(NumberArrayPosition) = NumberArrayPosition
Next NumberArrayPosition
Dim Result As Variant
ReDim Result(30)
Dim ResultPositionNumber As Long
Dim ResultString As String
Dim RandomNumber As String
Dim InStrResult As Long
ResultString = ""
For ResultPositionNumber = 1 To 30
RandomNumber = Application.WorksheetFunction.RandBetween(1, 100)
InStrResult = InStr(1, ResultString, RandomNumber)
If InStrResult = 0 Then
ResultString = ResultString & " " & RandomNumber
Else
Do While InStrResult > 1
RandomNumber = Application.WorksheetFunction.RandBetween(1, 100)
InStrResult = InStr(1, ResultString, RandomNumber)
Loop
ResultString = ResultString & " " & RandomNumber
End If
'Result in an Array
Result(ResultPositionNumber) = RandomNumber
Next ResultPositionNumber
'If you want the result as an Array Use Result(ResultPositionNumber)
'If you want the result as an Array Use ResultString
End Sub
Try this variation.
In column A put in the 100 numbers in order.
For each number in column A, put =RAND() in column B.
Then sort the array using column B.
Pick off the top 30 numbers in column A.
I have a large table, sometimes with hundreds of rows.
This table is generated by another application that exports to excel.
One column has the heading "Adjusted Price".
I want all the rows in this column to contain a formula (presently they're all 0's).
I want to automate this process because the table gets regenerated all the time.
This column may not always be in the same place. So I need a macro that can find this column ("Adjusted Price") and then fill all the cells in that column with a formula (with the exception of the first row of course).
Can this be done?
Thanks in advance :)
Your homework is to figure out how to plug it in!
Option Explicit
Sub setAdjustedPrice()
Dim column As Integer
Dim adjustedPriceColumn As String
Dim found As Boolean
Dim rowCount As Long
column = 1
rowCount = 1
Do While Range(FncAlphaCon(column) & rowCount).Value <> "" And found = False
If (Range(FncAlphaCon(column) & rowCount).Value = "Adjusted Price") Then
found = True
adjustedPriceColumn = FncAlphaCon(column)
Else
column = column + 1
End If
Loop
If found = True Then
Do While rowCount < ActiveSheet.UsedRange.Rows.count
rowCount = rowCount + 1
Range(adjustedPriceColumn & rowCount) = "YOUR FORMULA"
Loop
Else
MsgBox ("'Adjusted Price' column not found, cannot continue.")
End If
End Sub
Private Function FncAlphaCon(aNumber As Integer) As String
' Fixed version 27/10/2011
Dim letterArray As String
Dim iterations As Integer
letterArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
If aNumber <= 26 Then
FncAlphaCon = (Mid$(letterArray, aNumber, 1))
Else
If aNumber Mod 26 = 0 Then
iterations = Int(aNumber / 26)
FncAlphaCon = (Mid$(letterArray, iterations - 1, 1)) & (Mid$(letterArray, 26, 1))
Else
'we deliberately round down using 'Int' as anything with decimal places is not a full iteration.
iterations = Int(aNumber / 26)
FncAlphaCon = (Mid$(letterArray, iterations, 1)) & (Mid$(letterArray, (aNumber - (26 * iterations)), 1))
End If
End If
End Function
In a program that I'm trying to write now I take two columns of numbers and perform calculations on them. I don't know where these two columns are located until the user tells me (they input the column value in a cell in the workbook that my code is located in).
For example, if the user inputted "A" and "B" as the columns where all the information is in I can perform calculations based on those values. Likewise if they wanted to analyze another worksheet (or workbook) and the columns are in "F" and "G" they could input those. The problem is that I'm asking the user to input those two columns as well as four others (the last four are the result columns). I did this in hopes that I would be able to make this flexible, but now inflexibility is acceptable.
My question is, if I'm given a value of where some information will be (let's say "F") how can I figure out what the column will be after or before that inputted value. So if I'm only given "F" I'll be able to create a variable to hold the "G" column.
Below are examples of how the variables worked before I needed to do this new problem:
Dim first_Column As String
Dim second_Column As String
Dim third_Column As String
first_Column = Range("B2").Text
second_Column = Range("B3").Text
third_Column = Range("B4").Text
Here the cells B2 - B4 are where the user inputs the values. Generally I want to be able to not have the B3 and B4 anymore. I feel like the Offset(0,1) might be able to help somehow but so far I've been unable to implement it correctly.
Thank you,
Jesse Smothermon
Here are two functions that will help you dealing with columns > "Z". They convert the textual form of a column to a column index (as a Long value) and vice versa:
Function ColTextToInt(ByVal col As String) As Long
Dim c1 As String, c2 As String
col = UCase(col) 'Make sure we are dealing with "A", not with "a"
If Len(col) = 1 Then 'if "A" to "Z" is given, there is just one letter to decode
ColTextToInt = Asc(col) - Asc("A") + 1
ElseIf Len(col) = 2 Then
c1 = Left(col, 1) ' two letter columns: split to left and right letter
c2 = Right(col, 1)
' calculate the column indexes from both letters
ColTextToInt = (Asc(c1) - Asc("A") + 1) * 26 + (Asc(c2) - Asc("A") + 1)
Else
ColTextToInt = 0
End If
End Function
Function ColIntToText(ByVal col As Long) As String
Dim i1 As Long, i2 As Long
i1 = (col - 1) \ 26 ' col - 1 =i1*26+i2 : this calculates i1 and i2 from col
i2 = (col - 1) Mod 26
ColIntToText = Chr(Asc("A") + i2) ' if i1 is 0, this is the column from "A" to "Z"
If i1 > 0 Then 'in this case, i1 represents the first letter of the two-letter columns
ColIntToText = Chr(Asc("A") + i1 - 1) & ColIntToText ' add the first letter to the result
End If
End Function
Now your problem can be solved easily, for example
newColumn = ColIntToText(ColTextToInt(oldColumn)+1)
EDITED accordingly to the remark of mwolfe02:
Of course, if you are not interested in the column names, but just want to get a range object of a specific cell in a given row right beneath a column given by the user, this code is "overkill". In this case, a simple
Dim r as Range
Dim row as long, oldColumn as String
' ... init row and oldColumn here ...
Set r = mysheet.Range(oldColumn & row).Offset(0,1)
' now use r to manipulate the cell right to the original cell
will do it.
You were on the right track with Offset. Here is a test function that shows a couple different approaches to take with it:
Sub test()
Dim first_Column As String
Dim second_Column As String
Dim third_Column As String
Dim r As Range
first_Column = Range("B2").Text
second_Column = Range("B2").Offset(1, 0).Text
third_Column = Range("B2").Offset(2, 0).Text
Debug.Print first_Column, second_Column, third_Column
Set r = Range("B2")
first_Column = r.Text
Set r = r.Offset(1, 0)
second_Column = r.Text
Set r = r.Offset(1, 0)
third_Column = r.Text
Debug.Print first_Column, second_Column, third_Column
End Sub
UPDATE: After re-reading your question I realize you were trying to do offsets based on a user-entered column letter. #rskar's answer will shift the column letter, but it will be a lot easier to work with the column number in code. For example:
Sub test()
Dim first_Col As Integer, second_Col As Integer
first_Col = Cells(, Range("B2").Text).Column
second_Col = first_Col + 1
Cells.Columns(first_Col).Font.Bold = True
Cells.Columns(second_Col).Font.Italic = True
End Sub
There are a few syntactical problems with #rskar's answer. However, it was helpful in producing a function that grabs a column "letter", based on an input column "letter" and a desired offset to the right:
Public Function GetNextCol(TheCol As String, OffsetRight As Integer) As String
Dim TempCol1 As String
Dim TempCol2 As String
TempCol1 = Range(TheCol & "1").Address
TempCol2 = Range(TempCol1).Offset(0, OffsetRight).Address(0, 0, xlA1)
GetNextCol = Left(TempCol2, Len(TempCol2) - 1)
End Function
In light of the comments of others (and they all raised valid points), here is a much better solution to the problem, using Offset and Address:
Dim first_Column As String
Dim second_Column As String
Dim p As Integer
first_Column = Range("B2").Text
second_Column = _
Range(first_Column + ":" + first_Column).Offset(0, 1).Address(0, 0, xlA1)
p = InStr(second_Column, ":")
second_Column = Left(second_Column, p - 1)
The above should work for any valid column name, "Z" and "AA" etc. included.
Make use of the Asc() and Chr() functions in VBA, like so:
Dim first_Column As String
Dim second_Column As String
first_Column = Range("B2").Text
second_Column = Chr(Asc(first_Column) + 1)
The Asc(s) function returns the ASCII code (in integer, usually between 0 and 255) of the first character of a string "s".
The Chr(c) function returns a string containing the character which corresponds to the given code "c".
Upper case letters (A thru Z) are ASCII codes 65 thru 90. Just google ASCII for more detail.
NOTE: The above code will be fine so long as the first_Column is between "A" and "Y"; for columns "AA" etc., it will take a little more work, but Asc() and Chr() will still be the ticket to coding for that.