I am attempting to select a range of cells. I have done this before but am having trouble with the syntax.
Sub ChgDateX()
Range("A41").Select
Do
If ActiveCell.Value = "Last Updated" Then
mydate = ActiveCell.Offset(-40, 0).Value
'Cells(ActiveCell.Offset(0, 1), ActiveCell.Offset(0, 9)).Select
ActiveCell.Offset(0, 1).Value = mydate
ActiveCell.Offset(0, 2).Value = mydate
ActiveCell.Offset(0, 3).Value = mydate
ActiveCell.Offset(0, 4).Value = mydate
ActiveCell.Offset(0, 5).Value = mydate
ActiveCell.Offset(0, 6).Value = mydate
ActiveCell.Offset(0, 7).Value = mydate
ActiveCell.Offset(0, 8).Value = mydate
ActiveCell.Offset(0, 9).Value = mydate
ActiveCell.EntireRow.Select
Selection.NumberFormat = "m/d/yyyy"
ActiveCell.Offset(1, 0).Select
Else: ActiveCell.Offset(1, 0).Select
End If
Loop Until ActiveCell.Value = "" & ActiveCell.Offset(-3, 0).Value = ""
End Sub
I am trying to get away from the individual offset = mydate type coding.
Couple of things:
This code:
Loop Until ActiveCell.Value = "" & ActiveCell.Offset(-3, 0).Value = ""
Doesn't work as expected because the correct operator you're looking for is And and not &
You don't have to "Select" anything. You can just put a reference to a cell in a variable (see my code).
Also, since you are always moving down to the next cell in the loop, you can put that outside of the IF statement.
Based on your code I think you're looking for something like this:
Option Explicit
Sub test()
Dim r As Range
Dim myDate As Date
Set r = Range("A41")
Do
If (r.Value = "Last Updated") Then
myDate = r.Offset(-40, 0).Value
With Range(r.Offset(0, 1), r.Offset(0, 9))
.Value = myDate
.NumberFormat = "m/d/yyyy"
End With
End If
Set r = r.Offset(1, 0)
Loop Until r.Value = vbNullString And r.Offset(-3, 0).Value = ""
End Sub
Try this:
ActiveCell.Range("B1:J1").Value = MyDate
How about this ... with 1 note. I wouldn't advise the looping like this. Perhaps you can filter on Column A <> "" or something, then just loop through the visible cells? Hard to say without knowing what you are doing.
Option Explicit
Sub ChgDateX()
Range("A41").Select
Do
If ActiveCell.Value = "Last Updated" Then
Range(ActiveCell.Offset(0, 1), ActiveCell.Offset(0, 9)).Value = ActiveCell.Offset(-40, 0).Value
ActiveCell.EntireRow.NumberFormat = "m/d/yyyy"
End If
ActiveCell.Offset(1).Select
Loop Until ActiveCell.Value = "" & ActiveCell.Offset(-3, 0).Value = ""
End Sub
Related
I am trying to convert a sub procedure to a function. This sub generate number based on some conditions. It's working as expected but when I try to convert it as a function, it only generate "0".
Can anyone please help? It will be great appreciated!
Here is my sub named "GenerateNumber"
Public Sub GenerateNumber()
Dim evenRange As Range
Dim oddRange As Range
Dim i As Integer
Set evenRange = Range("A2:Z2,A4:Z4,A6:Z6,A8:Z8,A10:Z10,A12:Z12,A14:Z14,A16:Z16,A18:Z18 ")
Set oddRange = Range("A1:Z1,A3:Z3,A5:Z5,A7:Z7,A9:Z9,A11:Z11,A13:Z13,A15:Z15,A17:Z17,A19:Z19")
If Not Intersect(Activecell, evenRange) Is Nothing Then
If Activecell.Interior.Color = Activecell.Offset(0, -1).Interior.Color Then
Activecell.Value = Activecell.Offset(0, -1).Value + 1
For i = 1 To Selection.Cells.Count - 1
Activecell.Offset(0, i).Value = Activecell.Value + i
Next i
Else
Activecell.Value = 1
For i = 1 To Selection.Cells.Count - 1
Activecell.Offset(0, i).Value = Activecell.Value + i
Next i
End If
'MsgBox "Active Cell In Even Range!"
Else
' MsgBox "Active Cell In Odd Range!"
If Activecell.Interior.Color = Activecell.Offset(0, 1).Interior.Color Then
Activecell.Value = Activecell.Offset(0, 1).Value + 1
For i = 1 To Selection.Cells.Count - 1
Activecell.Offset(0, -i).Value = Activecell.Value + i
Next i
Else
Activecell.Value = 1
For i = 1 To Selection.Cells.Count - 1
Activecell.Offset(0, -i).Value = Activecell.Value + i
Next i
End If
End If
End Sub
And my function code like this,
Public Function GenNumber() As Variant
Dim evenRange As Range
Dim oddRange As Range
Dim rng As Range
Dim i As Integer
Set evenRange = Range("A2:Z2,A4:Z4,A6:Z6,A8:Z8,A10:Z10,A12:Z12,A14:Z14,A16:Z16,A18:Z18 ")
Set oddRange = Range("A1:Z1,A3:Z3,A5:Z5,A7:Z7,A9:Z9,A11:Z11,A13:Z13,A15:Z15,A17:Z17,A19:Z19")
If Not Intersect(Activecell, evenRange) Is Nothing Then
If Activecell.Interior.Color = Activecell.Offset(0, -1).Interior.Color Then
Activecell.Value = Activecell.Offset(0, -1).Value + 1
For i = 1 To Selection.Cells.Count - 1
Activecell.Offset(0, i).Value = Activecell.Value + i
Next i
Else
Activecell.Value = 1
For i = 1 To Selection.Cells.Count - 1
Activecell.Offset(0, i).Value = Activecell.Value + i
Next i
End If
'MsgBox "Active Cell In Even Range!"
Else
' MsgBox "Active Cell In Odd Range!"
If Activecell.Interior.Color = Activecell.Offset(0, 1).Interior.Color Then
Activecell.Value = Activecell.Offset(0, 1).Value + 1
For i = 1 To Selection.Cells.Count - 1
Activecell.Offset(0, -i).Value = Activecell.Value + i
Next i
Else
Activecell.Value = 1
For i = 1 To Selection.Cells.Count - 1
Activecell.Offset(0, -i).Value = Activecell.Value + i
Next i
End If
End If
GenNumber = Activecell.Value
End Function
Can anyone say where is the wrong and how to solve it.
Thank you.
I am trying to make a VBA scrip that check all cells between B2 and B60 for the text "Ja" that's yes in Norwegian.
How can I make this a little bit simpler that making a "if" command for each cell?
I want it to, if the cell contains "ja"(yes) then write to colum D and the same number.
ie. B1,2,3,4,5 cotains "ja", I need it to take the previous cell value in D1.2,3,4,5 and add another digit to it +1.
If nothing is found in B(ie.false) it needs to write "NEI" in the current cell, and if "NEI" (no) is found in that cell it adds +1 to colum E
Sub Macro2()
Dim celltxt As String
Dim a As Variant
If IsEmpty(Range("B2").Value) = True Then
Cells(2, 2).Value = "NEI"
End If
celltxt = ActiveSheet.Range("B2").Text
If InStr(1, celltxt, "ja") Then
a = Cells(2, 1).Value
'write to cell
Cells(2, 4).Value = Cells(2, 4) + 1
Else
'antall Cw'er vedkommende IKKE har deltatt på
Cells(2, 5).Value = Cells(2, 5) + 1
End If
If IsEmpty(Range("B3").Value) = True Then
Cells(3, 2).Value = "NEI"
End If
celltxt = ActiveSheet.Range("B3").Text
If InStr(1, celltxt, "ja") Then
a = Cells(3, 1).Value
'write to cell
Cells(3, 4).Value = Cells(3, 4) + 1
Else
'antall Cw'er vedkommende IKKE har deltatt på
Cells(3, 5).Value = Cells(3, 5) + 1
End If
End Sub
Sub slettingALL()
Range("D2:E55").Select
Selection.ClearContents
End Sub
Sub slettingdeltakelse()
Range("B2:B60").Select
Selection.ClearContents
End Sub
The following code uses a For Each loop and an IF THEN ELSE statement to check for the value "JA" in the range B2:B60.
If it finds "JA", it looks two columns to the right from the current i location, and adds "+1" to the value above it. If it finds nothing, it writes "NEI" to the current i location, and then moves three columns to the right and adds +1 to the value above it.
Sub Macro2()
For Each i In Range(Cells(2, 2), Cells(60, 2))
If i.Value = "JA" Then
i.Offset(0, 2).Value = i.Offset(-1, 2).Value + 1
Else
i.Value = "NEI"
i.Offset(0, 3).Value = i.Offset(-1, 3).Value + 1
End If
Next i
End Sub
Please let me know if this code does not work for your purpose.
I have a table of numbers like below in a excel sheet.
Excel Table
No1 No2 No3 No4 No5
1 190 134 190 101
10 142 117 10 151
155 12 12 12 128
154 154 154 154 154
I have a UserForm with 5 TextBoxes to display 5 unique numbers from the table.
When I click on the command button, It should pull 5 UNIQUE numbers from the above table and display it in the TextBoxes respectively.
Also, If I click on the command button again(i.e. Multiple Clicks to get more result), It should populate a different UNIQUE numbers from the table.
I used simple if else concept to achieve this but it is not working correctly.
Please assist me to achieve the result. Thank you!
I use the below code:
Private Sub btnGenerate_Click()
Dim PresentRow As Byte, PresentColumn As Byte
PresentRow = ActiveCell.Row
PresentColumn = ActiveCell.Column
If PresentRow = 5 And PresentColumn = 1 Then
Sheet1.Range("F2").Activate
ElseIf PresentRow = 5 And PresentColumn = 5 Then
Sheet1.Range("A2").Activate
End If
Select Case ActiveCell.Column
Case 1
TextBox1.Text = ActiveCell.Value
If ActiveCell.Offset(0, 1).Value <> TextBox1.Text Then
TextBox2.Text = ActiveCell.Offset(0, 1).Value
Else
TextBox2.Text = ActiveCell.Offset(1, 1).Value
End If
If ActiveCell.Offset(0, 2).Value <> TextBox2.Text Then
TextBox3.Text = ActiveCell.Offset(0, 2).Value
Else
TextBox3.Text = ActiveCell.Offset(1, 2).Value
End If
If ActiveCell.Offset(0, 3).Value <> TextBox3.Text Then
TextBox4.Text = ActiveCell.Offset(0, 3).Value
Else
TextBox4.Text = ActiveCell.Offset(1, 4).Value
End If
If ActiveCell.Offset(0, 4).Value <> TextBox4.Text Then
TextBox5.Text = ActiveCell.Offset(0, 4).Value
Else
TextBox5.Text = ActiveCell.Offset(1, 5).Value
End If
Case 5
TextBox1.Text = ActiveCell.Value
If ActiveCell.Offset(0, -1).Value <> TextBox1.Text Then
TextBox2.Text = ActiveCell.Offset(0, -1).Value
Else
TextBox2.Text = ActiveCell.Offset(1, -1).Value
End If
If ActiveCell.Offset(0, -2).Value <> TextBox2.Text Then
TextBox3.Text = ActiveCell.Offset(0, -2).Value
Else
TextBox3.Text = ActiveCell.Offset(1, -2).Value
End If
If ActiveCell.Offset(0, -3).Value <> TextBox3.Text Then
TextBox4.Text = ActiveCell.Offset(0, -3).Value
Else
TextBox4.Text = ActiveCell.Offset(1, -4).Value
End If
If ActiveCell.Offset(0, -4).Value <> TextBox4.Text Then
TextBox5.Text = ActiveCell.Offset(0, -4).Value
Else
TextBox5.Text = ActiveCell.Offset(1, -5).Value
End If
End Select
Sheet1.Activate
ActiveCell.Offset(1, 0).Select
End Sub
The following requires that Microsoft Scripting Runtime be added to the project via Tools, References.
In the worksheet code sheet:
Option Explicit
Private Sub CommandButton1_Click()
Dim txt As Long, vals As Variant, ky As Variant
Dim uniq As New Scripting.Dictionary
vals = Range("A2:E5").Value2
uniq.RemoveAll
Do While uniq.Count < 5
uniq.Item(vals(Application.RandBetween(LBound(vals, 1), UBound(vals, 1)), _
Application.RandBetween(LBound(vals, 2), UBound(vals, 2)))) = vbNullString
Loop
TextBox1 = uniq.Keys(0)
TextBox2 = uniq.Keys(1)
TextBox3 = uniq.Keys(2)
TextBox4 = uniq.Keys(3)
TextBox5 = uniq.Keys(4)
End Sub
I am new to VBA and coding in general. I am currently trying to transfer date from a multitab over to a workbook and keep getting the run time error 424: object required. Please help
Dim sheetname As String
Private Sub CommandButton2_Click()
Dim LastRow As Long
Dim sheetname As String
Select Case MultiPage1.Value
Case 0`sheetname = "Hannah"
Sheets(sheetname).Select
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A1").Select`enter code here`
Selection.End(xlDown).Offset(1, 0).Select
ActiveCell.Value = Date
ActiveCell.Offset(0, 1).Value = Me.RequestorHR.Value
ActiveCell.Offset(0, 2).Value = Me.CaseHR.Value
ActiveCell.Offset(0, 3).Value = Me.TypeHR.Value
ActiveCell.Offset(0, 4).Value = Me.UrgencyHR.Value
ActiveCell.Offset(0, 5).Value = Me.ReasonsHR.Value
ActiveCell.Offset(0, 6).Value = Me.DeadlineHR.Value
Clear_Click
Case 1
sheetname = "John"
Sheets(sheetname).Select
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A1").Select
Selection.End(xlDown).Offset(1, 0).Select
ActiveCell.Value = Date
ActiveCell.Offset(0, 1).Value = Me.RequestorJM.Value
ActiveCell.Offset(0, 2).Value = Me.CaseJM.Value
ActiveCell.Offset(0, 3).Value = Me.TypeJM.Value
ActiveCell.Offset(0, 4).Value = Me.UrgencyJM.Value
ActiveCell.Offset(0, 5).Value = Me.ReasonsJM.Value
ActiveCell.Offset(0, 6).Value = Me.DeadlineJM.Value
Clear_Click
I made some suggestions below so you have less repeating code. I added an array so you do not have to keep retyping all your textbox(Assuming) values each time. I hope that helps. I had to guess so if it needs to be tweaked let me know bud! It may run an error cause i was not sure of the active cell your referring too. If you let me know, i can fix it.
Private Sub CommandButton2_Click()
Dim LastRow As Long
Dim sheetname As String
Dim strList() As Variant
Dim i As Integer
strList = Array(, Me.RequestorHR.Value, Me.CaseHR.Value, Me.TypeHR.Value, _
Me.UrgencyHR.Value, Me.ReasonsHR.Value, Me.DeadlineHR.Value)
Select Case MultiPage1.Value
Case 0
sheetname = "Hannah"
Sheets(sheetname).Select
LastRow = Sheets(sheetname).Range("A" & Rows.Count).End(xlUp).Row
Sheets(sheetname).Range("A1").End(xlDown).Offset(1, 0).Value = Date
For i = 1 To 6
With Sheets(sheetname)
.ActiveCell.Offset(0, i).Value = strList(i)
End With
Next i
Clear_Click
Case 1
sheetname = "John"
Sheets(sheetname).Select
LastRow = Sheets(sheetname).Range("A" & Rows.Count).End(xlUp).Row
Sheets(sheetname).Range("A1").End(xlDown).Offset(1, 0).Value = Date
For i = 1 To 6
With Sheets(sheetname)
.ActiveCell.Offset(0, i).Value = strList(i)
End With
Next i
Clear_Click
In Userform Code:
' StartUpPosition > Manual
Private Sub UserForm_Initialize()
With Me
.Top = Int(((Application.Height / 2) + Application.Top) - (.Height / 2))
.Left = Int(((Application.Width / 2) + Application.Left) - (.Width / 2))
End With
End sub
Code in worksheet or Module:
Sub Open_Userform
UserForm1.Show False
End sub
Have a great day!
I have this macro that concatenates values from 3 cells, the problem is that it only concatenates the 1st and 2nd values, before it only concatenated the the first value was 3, the second value was march and the third value was 2015, so the result just gives me " 3march"
this is my code:
Private Sub CommandButton1_Click()
Sheets("Info").Activate
Range("M2:Q2").Select
Selection.Copy
Range("A2").Cells(1).Select
Do While ActiveCell.Value <> ""
ActiveCell.Offset(1, 0).Select
Loop
Selection.Offset(0, 12).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Selection.Offset(0, -12).Select
Selection.Cells(1).Select
'ahora viene la parte de pegar los valores,,,
Selection.Cells(1).Value = TextBox1.Value
Selection.Cells(1, 2).Value = ComboBox1.Value
Selection.Cells(1, 3).Value = ComboBox4.Value
Selection.Cells(1, 4).Value = ComboBox5.Value
Selection.Cells(1, 5).Value = ComboBox6.Value
Selection.Cells(1, 6).Value = ComboBox9.Value
Selection.Cells(1, 7).Value = ComboBox13.Value
Selection.Cells(1, 8).Value = TextBox3.Value
Selection.Cells(1, 9).Value = TextBox4.Value
Selection.Cells(1, 10).Value = TextBox8.Value
Selection.Cells(1, 11).Value = TextBox6.Value
Selection.Cells(1, 12).Value = ComboBox12.Value
'HERE IS THE PROBLEM!!!!!!
f_env = Selection.Cells(1, 3).Value & "-" & Selection.Cells(1, 4).Value & "-" & Selection.Cells(1, 5).Value
Selection.Cells(1, 17).Value = f_env
Unload UserForm1
End Sub
Because the column (on the selection's row) is actually determined by the column that selection was in, you will get a date in column Q only if selection is in column A. That may be a minor point but I believe the destination should always be a certain column.
Sub gather_date()
Dim sel As Range
With Selection.Parent
For Each sel In Selection.Columns(1).Cells
If Application.CountA(sel.Resize(1, 3)) = 3 Then
.Cells(sel.Row, 17) = CDate(Join(Array(sel.Value2, Left(sel.Offset(0, 1).Value2, 3), sel.Offset(0, 2).Value2), "-"))
.Cells(sel.Row, 17).NumberFormat = "d-mmmm-yyyy"
End If
Next sel
End With
End Sub
Since you are usig a macro based upon select, I've added a loop that will process all of the cells in selection.
This could also be performed with the following worksheet formula in Q2.
=DATEVALUE(CONCATENATE(B2, "-", C2, "-", D2))
Format as a date and fill down as necessary.
Edit:
Added new code.
It is hard to run the code as nobody but you has the workbook, and we would have to try and reproduce the workbook ourselves. Here is a revised code as I have interpreted what you are trying to do. I cannot test it as I don't have your workbook.
Private Sub CommandButton1_Click()
Dim sh As Worksheet
Dim Crng As Range 'copy range
Dim LrWs As Long 'last row
Dim Prng As Range 'paste range
Dim Drng As Range
Dim f_env As String
Set sh = Worksheets("Info")
With sh
Set Crng = .Range("M2:Q2")
LrWs = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
Set Prng = .Cells(LrWs, 13)
Crng.Copy
Prng.PasteSpecial xlPasteAll
Application.CutCopyMode = 0
Set Drng = .Cells(LrWs, 1)
Drng.Value = TextBox1.Value
Drng.Offset(0, 1).Value = ComboBox1.Value
Drng.Offset(0, 2).Value = ComboBox4.Value
Drng.Offset(0, 3).Value = ComboBox5.Value
Drng.Offset(0, 4).Value = ComboBox6.Value
Drng.Offset(0, 5).Value = ComboBox9.Value
Drng.Offset(0, 6).Value = ComboBox13.Value
Drng.Offset(0, 7).Value = TextBox3.Value
Drng.Offset(0, 8).Value = TextBox4.Value
Drng.Offset(0, 9).Value = TextBox8.Value
Drng.Offset(0, 10).Value = TextBox6.Value
Drng.Offset(0, 11).Value = ComboBox12.Value
End With
f_env = ComboBox4.Value & "-" & ComboBox5.Value & "-" & ComboBox6.Value
Drng.Offset(0, 16) = f_env
End Sub
Link us up to a sample workbook, if you can't get this working properly.