http://imgur.com/zEm7hT7
In my image I have my form which upon entering information into the text boxes and pressing enter will place the information into the correct places below the titles, what i'm wanting to do is either have a next button that will go down one row and a back button that will go up one row and allows me to enter information into a 'database' per say or i would like to have it automatically jump down one row upon clicking 'Enter'. I have looked and I can't find anything that is quite what i'm asking and response is great, thanks.
This is what i have so far.
Private Sub CommandButton1_Click()
Dim x As Integer
x = Cells(1, 1).End(xlDown).Row + 1
Cells(x, 1) = TextBox1.Value
Cells(x, 2) = TextBox2.Value
Cells(x, 3) = TextBox3.Value
Cells(x, 4) = TextBox4.Value
Cells(x, 5) = TextBox5.Value
End Sub
Replace x=2 with finding the next empty row
If Cells(2,1) = "" Then
x = 2
Else
x = Cells(1,1).End(xlDown).Row + 1
End If
Related
I want to go to the next column each time I click on a button. I have tried this but it does not work:
Dim i As Integer
If i = 0 Then
i = 4
End If
i = i + 1
ffc = ActiveCell.Offset(x, i).Column
I used ffc because I want to add it to other button to insert the value "A" or "P" like this:
Sheet3.Cells(x, ffc).Value = "A"
To understand more, my project is for record attendance by days. My problem is just moving to the next column.
I have tried this too
ffc = ActiveCell.Offset(1, 0).Row
It works but it's working with rows, which mean when I go down in rows the column move one step and I want the column move when I only press the button
Please try the code below
Dim i As Integer
For i = 1 To Columns.Count
ActiveSheet.Cells(1, i).Select
Next
I'm just getting all the text with red font-color on a first sheet and then stored it in an array. I then want to navigate to another sheet and activate a specific cell where I want to initially put the values inside my previous array. But the part where I activate a cell on the other sheet resulted to Error 400. I'm confident enough that it's right since I already used it before. I don't know if I have some manipulation at the first part of my code that affects that.
Sub isFontRed()
Cells(2, 1).Select
Dim missingJobs(0 To 600) As String
Dim size As Integer, row As Integer, col As Integer, jobIndex As Integer
jobIndex = 0
For row = 2 To 10 '600
For col = 1 To 2
If (CStr(Cells(row, col).Font.ColorIndex) = 3) Then
missingJobs(jobIndex) = Cells(row, col)
jobIndex = jobIndex + 1
End If
Next col
Next row
Dim jobs As String
jobs = ""
For i = 0 To UBound(missingJobs) - 1
jobs = jobs + missingJobs(i) + ", "
Next i
Worksheets("New Jobs in New Folder").Activate
Cells(4, 2).Activate
End Sub
It's been a while but are you trying to select cells(4, 2)? I don't think you can 'activate' a cell like you can a worksheet. Try Cells(4, 2).Select instead.
i have written the following code:
Public Sub SortMyData()
'approach: convert line to string and concatenate to that as it's a lot less picky than Excel's formats, then replace cell value with the new string.
' Excel will then define the string type as either Percentage or Scientific depending on the magnitude.
Dim i As Integer
Dim g As Integer
Dim N_Values As Integer
Dim IntermediateString As String
N_Values = Cells(1048576, 2).End(xlUp).Row 'retrieves the final filled row in column 2 (B)
For i = 6 To N_Values 'iteration loop from 6 (first row of value) to N_Values (last filled row)
If Cells(i, 2).NumberFormat <> "0.0%" Then
IntermediateString = Cells(i, 2).Value
Cells(i, 2).NumberFormat = "0.0%"
Cells(i, 2).Value = Cells(i, 2).Value / 100
Else
MsgBox ("The Range of Cells Has Already Been Formated as Percentage")
End If
Next i
For g = 6 To N_Values 'iteration loop from 6 (first row of value) to N_Values (last filled row)
If Len(Cells(g, 3) > 3) Then
Cells(g, 3).Value = Cells(g, 3).Value / 1000
Else
MsgBox ("Data is correct so no action will be taken")
End If
Next g
End Sub
the code runs fine but it does not move onto the secont if statement, it just keeps on running the first one and displaying the MsgBox ("The Range of Cells Has Already Been Formated as Percentage") so i dont know where i have made a mistake?
i am a rookie by the way so go easy on me!
You declare the string :
Dim IntermediateString As String
Then you affect a value to it (by the way, the .Value is optional, cells(x, y) and cells(x, y).Value are the same) :
IntermediateString = Cells(i, 2).Value
But then you don't do anything with this string, so I'm pretty sure that's not what you intended to do (you could comment both of those line, your program would run the same way).
Also, seeing as your program is coded, you should have at most as much messages "The Range of Cells Has Already Been Formated as Percentage" as the number of lines in the B column, but it should eventually keep going to reach second if statement.
I suggest you open the execution window (Ctrl + G), and replace the occurences of "MsgBox" in your code by Debug.Print. You will see the message in the execution window, but it won't pause the execution of the program (much better way to debug)
I am trying to create a conditional loop macro in Excel. Column B contains a last name, Column C contains a first name, and Column D contains a first and last name. I am trying to get the macro to detect when Column D = Column C + Column B.
If D = C + B, then clear contents of D.
So, the following works for a single row:
Sub ClearContentsD ()
If Range("D1").Value = Range("C1").Value + Space(1) + Range("B1") Then Range("D1").ClearContents
End Sub
It does not work without the added Space(1), and I cannot get it to loop through the whole worksheet:
Sub ClearContentsLoop()
Application.ScreenUpdating = False
Dim i As Long
For i = 1 To Rows.Count
Next i
Do While Cells(i, 4).Value = Cells(i, 3).Value + Space(1) + Cells(i, 2).Value
Cells(i, 4).ClearContents
Loop
Application.ScreenUpdating = True
End Sub
VBA doesn't like my Do While. Any help would be greatly appreciated.
CJ
Some issues:
You must concatenate strings with &. The plus (+) is for addition;
Your For loop is not doing anything: its body is empty;
Your Do While Loop will at most run once, because i is not incremented;
It is a mystery why you would want two loops (For and Do While);
A sheet has many rows of which you only use a fraction, so don't loop through all of them (For) and use UsedRange.
Possible correction:
Sub ClearContentsLoop()
Dim i As Long
Application.ScreenUpdating = False
For i = 1 To ActiveSheet.UsedRange.Rows.Count
If Cells(i, 4).Value = Cells(i, 3).Value & " " & Cells(i, 2).Value Then
Cells(i, 4).ClearContents
End If
Next i
Application.ScreenUpdating = True
End Sub
There is a way to ignore the space in the values you are evaluating. Try this:
Application.ScreenUpdating = False
Dim i As Long
For i = 1 To Rows.Count
If InStr(1, Cells(i, 4).Value, Cells(i, 2).Value, vbTextCompare) > 0 And InStr(1, Cells(i, 4).Value, Cells(i, 3).Value, vbTextCompare) > 0 Then Cells(i, 4).ClearContents
Next i
Application.ScreenUpdating = True
Explanation:
By using the InStr function, you are testing for the presence of one text string inside of another, and if at least one match is found, then the function returns a non-zero value (the position where the match was found). In the above example, you are testing for the presence of the first name and last name at the same time, and if both are found, then the code clears out the contents of the cell.
And, as was pointed out in the comments section, you need to do this inside the loop so that all cells down the length of the worksheet are evaluated and updated as specified.
Be sure to test this on a COPY of your original data so that you don't lose the original values in case you want to roll back your changes! ;)
VBA'ers,
I'll cut to the chase. I have a userform with all the bells and whistles(Label's,textboxes,listboxes,tabstrips,etc.). Currently I have three subs.
Here's my code. I know people only asked for the userform initialize but seeing all of it might help find the problem.
Private x As Single
Private y As Single
'------------------------------------------
Private Sub CommandButton1_Click()
Unload Me
End Sub
'------------------------------------------
Private Sub ListBox1_Click()
x = 2
y = 2
name = ListBox1.Value
'Loop to match names
Do Until name = Cells(x, y)
x = x + 1
Loop
'Changes lables on click <- I realize I can handle this better with listbox.values
Label2.Caption = Sheet2.Cells(x, 2) 'Name
Label5.Caption = Sheet2.Cells(x, 3) 'Current Positions
Label7.Caption = Sheet2.Cells(x, 4) 'Previous Positions
Label9.Caption = Sheet2.Cells(x, 5) 'DOB
Label11.Caption = Sheet2.Cells(x, 6) 'POB
Label13.Caption = Sheet2.Cells(x, 7) 'Party Affiliation
'Changes tab strip accordingly
Call TabStrip1_Change
'Handles Picture
If Cells(x, 8) <> "" Then
Image1.Picture = LoadPicture(ThisWorkbook.Path & Cells(x, 8))
Else
Image1.Picture = LoadPicture(ThisWorkbook.Path & "..\pics\nophoto.jpg")
End If
End Sub
'------------------------------------------
Private Sub TabStrip1_Change()
'Handle Tab Strip
If TabStrip1.Value = 0 Then
TextBox1.Value = Cells(x, 9)
ElseIf TabStrip1.Value = 1 Then
TextBox1.Value = Cells(x, 10)
Else
TextBox1.Value = Cells(x, 11)
End If
End Sub
'------------------------------------------
Private Sub UserForm_Initialize()
'Initialize global variables
x = 2
'Initialize lists within userform.
ListBox1.RowSource = "B2:B11"
'Set tab strip to first tab.
TabStrip1.Value = 0
TextBox1.Value = Sheet2.Cells(2, 9)
'Grab photo if path is in cell
If Cells(2, 8) <> "" Then
Image1.Picture = LoadPicture(ThisWorkbook.Path & Cells(2, 8))
Else
Image1.Picture = LoadPicture(ThisWorkbook.Path & "..\pics\nophoto.jpg")
End If
End Sub
The problem is that when I run the code, via vba or a commandButton (Userform1.show) its a coins flip on whether or not the userform populates the listbox. The labels are initialized correctly, but the listbox shows no text. If I continue to run and stop the macro, it will eventually work fine.
Is this a memory issue? Am I not activating the userform properly? Or is this due to sloppy coding?
Any suggestions would be appreciated.
Since we cannot see the full Userform_Initialize(), I assume you have only populated the list into listbox.
If you want a listbox to select something when it shows, you need to call something like ListBox1.ListIndex = 0 or the index of your default value. This must be after the list is populated.
UPDATE:
Thanks, I believe when it doesn't work, it's because the activesheet is not where you have the list items. Either put in full formula address or Range Name my test workbook is "Test.xlsm":
ListBox1.RowSource = "[Test.xlsm]Sheet1!B2:B11" ' Change Workbook and Sheet name to fit yours
or
ListBox1.RowSource = "Test.xlsm!MyListItems" ' Change Workbook name, Create and Change the name of the Range that contains the list items.