I am very new to VBA Scripts. I have been googling trying to figure out how to combine these together and assign a button to run this.
My first script I am adding a sequence number and row to a table. The table is two columns B:C. My numbering is looking at the row below the line I am inserting to keep the numbers in sequential order. (I found this on Youtube works great)
Private Sub CommandButton2_Click()
Sheets("Sheet1").Range("B4").Select
ActiveCell.EntireRow.Insert Shift:=xlDown
Sheets("Sheet1").Range("B4:C4").Select
Selection.Borders.Weight = xlThin
Sheets("Sheet1").Range("B4").Select
ActiveCell.Value = "=B5+1"
End Sub
Second one is applying a timestamp to C4 which is giving a timestamp to the sequential number.
Private Sub timeStamp()
Dim ts As Date
With Range("C4")
.Value = Now
.NumberFormat = "h:mm:ss AM/PM"
End With
End Sub
I cannot figure out how to make these two run together. Individually they work.
Thank you in advance for any help.
How about you just paste the code of the second in the first (and a few other adjustments):
Private Sub CommandButton2_Click()
Dim ts As Date
With ThisWorkbook.Sheets("Sheet1")
.Range("B4").EntireRow.Insert Shift:=xlDown
.Range("B4:C4").Borders.Weight = xlThin
.Range("B4").Value= "=B5+1"
With .Range("C4")
.Value = Now
.NumberFormat = "h:mm:ss AM/PM"
End With
End With
End Sub
Related
I have some columns that have cells that are "Number stored as text", which is causing problems with other code that is trying to copy one range to another
rng1.Copy Destination:=rng2
The destination range (rng2) is blank. Not had this issue with any other data, just when these "Number stored as text" are there, so I need to, via VBA, be able to convert them to number.
Looking around there seems to be two methods for this, but neither are working for me...
TextToColumnns:
rng1.Select
Selection.TextToColumns Destination:=Range(rng1), DataType:=xlDelimited
Value = value:
Range(rng1).Select
With Selection
.NumberFormat = "General"
.Value = .Value
End With
Other ways I have discovered are 100+ lines of code long which surely cannot be right??
What is the best way to achieve this?
What is wrong with something like this:
Public Sub TestMe()
With Selection
.NumberFormat = "0.00"
.Value = .Value
End With
End Sub
You can convert them with this:
Function text_to_number(txt As String) As Double
text_to_number = txt * 1.0
End Function
So, first of all, you need to convert them, and after this, copy to another place.
Something like this:
Sub copy_and_paste()
Dim numbers(30) As Double
'Copping from range A1:A30
For i = 1 To 30
numbers(i) = text_to_number(Cells(i, 1).Value)
Next i
'Pasting tn B1:B30
For i = i To 30
Cells(i, 2).Value = numbers(i)
Next i
End Sub
I've tried for days to find an answer to this but honestly, it might be a lack of knowledge in the subject that's causing me to not search for the right terms.
I have a spreadsheet with a series of dates between S7:GE7 and rows from 8:96 that have data that need to be locked the next day.
I know my way of selecting the columns is all wrong, and there's probably more wrong too but I can't think of a way to make it right.
Private Sub Workbook_Open()
Dim i As Range, cell As Range
Set i = Range("S7:GE7")
For Each cell In i
If (cell.Value < DateValue(Now())) Then
Range(i + "8:96").Locked = True
End If
Next cell
End Sub
What I'm hoping to achieve with this is a loop that looks through the dates in S7:GE7, and if the date is older than today it locks cells 8:96 in that column.
Any help that can be provided would be much appreciated. Thanks
Try this:
Private Sub Workbook_Open()
Dim i As Range, cell As Range
Set i = Range("S7:GE7")
For Each cell In i
If (cell.Value < DateValue(Now())) Then
cell.Offset(1, 0).Resize(79, 1).Locked = True
End If
Next cell
End Sub
I have an excel sheet where I am doing a VlookUP using VBA. The problem is that I extract information, and the amount is always different. I want to find a way to add to the code that will add information until there is no more information to add.
Here is the code that works but only for the cells I put in:
Sub vLook()
With ThisWorkbook.Worksheets("EODComponents").Range("f5:F200")
.Formula = "=VLOOKUP(C5,($H$5:$i$34),2,FALSE)"
.Value = .Value
End With
End Sub
You can set a lastRow:
Sub vLook()
Dim lastRow as Long
With ThisWorkbook.Worksheets("EODComponents")
lastRow = .Cells(.Rows.Count,6).End(xlUp).Row
With .Range("f5:F" & lastRow)
.Formula = "=VLOOKUP(C5,($H$5:$i$34),2,FALSE)"
.Value = .Value
End With
End With
End Sub
Maybe you can try to use a Do while-loop?
If you put the while statement right, this will continue until the statement becomes false, in your case; there is no more information to add.
You can use the Len()-function to check the length of the text/value inside a cell, when this is zero you can assume the cell is empty.
More information about this item can be found here.
Example:
Public Sub Something()
Dim i As Integer
i = 1
Do While (Len(Cells(i, 1).Text) > 0)
i = i + 1
Loop
MsgBox "The next row of column 'A' is empty: A" & i
End Sub
Hope this helps.
I have a userform that has a drop down box in which a person can select a record to have deleted off a list.
The code below is deleting the ENTIRE ROW. I do not want that. I just want the cells between A:E cleared on my spreadsheet.
I am not sure how else to describe this so I apologize in advance. Here is the code:
Private Sub CheckBox1_Click()
End Sub
Private Sub CommandButton1_Click()
Dim lRw As Long
ActiveWorkbook.Sheets("RAWDATA").Visible = xlSheetVisible
'get the row number. add 2 because ListIndex starts at one
lRw = Me.ComboBox1.ListIndex + 2
ActiveWorkbook.Sheets("RAWDATA").Select
Cells(lRw, 1).EntireRow.ClearContents
ActiveWorkbook.Sheets("RAWDATA").Visible = xlSheetHidden
End Sub
Private Sub CommandButton2_Click()
ComboBox1.Value = ""
ComboBox1.Clear
ComboBox1.Clear
Unload Me
End Sub
Private Sub UserForm_Initialize()
'assumes data starts in A1 and has a header row
Me.ComboBox1.List = ActiveWorkbook.Sheets("RAWDATA").Cells(1, 2).CurrentRegion.Offset(1, 2).Value
End Sub
side note: You don't need to select the cells to manipulate the contents in vba.
Check out this link to explain that concept in more detail:
how-to-avoid-using-select-in-excel-vba-macros
This is the problem code. You are clearing the entire row, using ".EntireRow.ClearContents"
ActiveWorkbook.Sheets("RAWDATA").Select
Cells(lRw, 1).EntireRow.ClearContents
ActiveWorkbook.Sheets("RAWDATA").Visible = xlSheetHidden
Here are three solutions. Both should give you some insight into how the .Cells(row,col) idea works while using a loop. You are using a variable to control the row number, and the same concept can be applied to the column. Even though it's just 5 columns. It might be 50 for another project. So you can loop through them using a "For Loop" This is my preferred method.
If you want to get loopy, try something like this. Use a Variable for the Column
For lCol = 1 To 5
Sheets("RAWDATA").Cells(lRw, lCol).ClearContents
Next lCol
You can do one cell at a time Directly coding the column number:
Sheets("RAWDATA").Cells(lRw, 1).ClearContents
Sheets("RAWDATA").Cells(lRw, 2).ClearContents
Sheets("RAWDATA").Cells(lRw, 3).ClearContents
Sheets("RAWDATA").Cells(lRw, 4).ClearContents
Sheets("RAWDATA").Cells(lRw, 5).ClearContents
You can do one cell at a time Directly coding the column LETTER:
Sheets("RAWDATA").Cells(lRw, "A").ClearContents
Sheets("RAWDATA").Cells(lRw, "B").ClearContents
Sheets("RAWDATA").Cells(lRw, "C").ClearContents
Sheets("RAWDATA").Cells(lRw, "D").ClearContents
Sheets("RAWDATA").Cells(lRw, "E").ClearContents
edit: added some explanation and link
The cells(lRw, 1).EntireRow.ClearContents is your issue. The EntireRow function selects the row which is pointed to by cells(lRw, 1). The .ClearContents function clears what's selected. You should replace it with something like:
Range("A" & <the row number> & ":J" & <the row number>).clearcontents
Your variable lRw is supposed to hold the value of the row in which the selected project is located, correct? If so, then:
Range("A" & lRw & ":J" & lRw ).clearcontents
should work. You can change the column letters to whatever you'd like to clear.
I think PJ Rosenburg's solutions are bit impractical, but I agree with the fact that you should shy away from using the .select function. You can do everything you need to do without using it. You'll write much better code once you understand this concept. In fact, here's a rewrite of your commandButton1_click that should do the exact same thing, but with less code and is easier to read.
Private Sub CommandButton1_Click()
Dim lRw As Long
lRw = Me.ComboBox1.ListIndex + 2
with ActiveWorkbook.Sheets("RAWDATA")
.Visible = xlSheetVisible
.Range("A" & lRw & ":J" & lRw ).clearcontents
.Visible = xlSheetHidden
end with
end sub
Notice a couple of things:
No .select
Moving the assignment statement
The addition of the With/End With statements
Anyway, I hope this helps and better explains what I was trying to say earlier.
I have a vba code that determines the date based on 3 cells in excel, it then reformats the date.
Sub Test()
Dim i As Long
i = Sheet1.Cells(Rows.Count, 2).End(xlUp).Row
Range("K3").Formula = "=DATE(A3,G3,H3)"
Range("K3").AutoFill Destination:=Range("K3:K28")
Set fmt = Range("K3:K28")
fmt.NumberFormat = "ddmmmyyyy"
End Sub
The line that gives a value to "i" (which is 28) determines the last populated cell in that column.
I would like to use the value of "i" as K28 in the range.
I am fairly new to VBA and have not been able to figure out how to accomplish this task.
You can slightly simplify your code. There is no need to use AutoFill:
Sub Test()
Dim i As Long
With Sheet1
i = .Cells(.Rows.Count, 2).End(xlUp).Row
With .Range("K3:K" & i)
.Formula = "=DATE(A3,G3,H3)"
.NumberFormat = "ddmmmyyyy"
End With
End With
End Sub