How do i fix application.counta formula - excel

I am writing a user-form to input data into an excel spreadsheet and i am having issues with the counta command to register a blank line.
I am new to VBA and tried looking for alternatives online without much success, and honestly not knowing what i was looking at when reading some of the code.
Please see my code below and for some context it is for room management, tracking guests arrival and departure dates, room rates and if two rooms are utilized next to each other. For some reason the data input begins at row 25 and i can't figure out for the life of me why when there are 25 blank rows above the cell being registered:
Dim emptyRow As Long
emptyRow = Application.WorksheetFunction.CountA(Range("B:B")) + 1
Cells(emptyRow, 2).Value = txtRoomNum.Value
Cells(emptyRow, 3).Value = txtGuestName.Value
Cells(emptyRow, 4).Value = DTPicker1.Value
Cells(emptyRow, 5).Value = DTPicker2.Value
If opInter.Value = True Then
Cells(emptyRow, 7).Value = (Val(txtRev1.Text) + Val(txtRev2.Text) + Val(txtRev3.Text)) * 0.5
Cells(emptyRow, 12).Value = Val(txtChildren.Text) * 0.5
Cells(emptyRow, 13).Value = Val(txtAdults.Text) * 0.5
Else
Cells(emptyRow, 7).Value = (Val(txtRev1.Text) + Val(txtRev2.Text) + Val(txtRev3.Text))
Cells(emptyRow, 12).Value = txtChildren.Value
Cells(emptyRow, 13).Value = txtAdults.Value
End If
Cells(emptyRow, 8).Value = UCase(txtRateCode.Value)
If opRoomOnly.Value = True Then
Cells(emptyRow, 10).Value = "Yes"
Else
Cells(emptyRow, 10).Value = "No"
End If
If opInter = True Then
Cells(emptyRow, 11).Value = "Yes"
Else
Cells(emptyRow, 11).Value = "No"
End If
Unload Me
End Sub```

The function COUNTA() returns the number of cells that are not blank in a given range.
I assume you want to paste data on the first empty row in the sheet, so you need the number of the last non-empty row (+1) to paste the data in. Try this:
emptyRow = Cells(Rows.Count, "B").End(xlUp).Row + 1
Here are some more examples on how to find last row / column in a sheet.
https://www.thespreadsheetguru.com/blog/2014/7/7/5-different-ways-to-find-the-last-row-or-last-column-using-vba

Related

Excel - return a value only if a condition is met via VBA

I think this is an easy one...
I have some code that goes through a source xls file and based on the company name it will pull out sales data relevant to that company and populate it in the company's own file.
It works fine (probably not very elegant), but I want to have a condition that only returns a value in one of my columns if a condition is met.
It's the commented line in the code below - any help greatly appreciated
For i = 2 To LastRow
If SourceSheet.Cells(i, 21).Value Like "CompanyName goes here*" Then
'change the column numbers to the relevant number
Product = SourceSheet.Cells(i, 11).Value
Base Sales Value = SourceSheet.Cells(i, 27).Value
Partner = SourceSheet.Cells(i, 21).Value
EndUser = SourceSheet.Cells(i, 7).Value
License = SourceSheet.Cells(i, 13).Value
PostingMonth = SourceSheet.Cells(i, 3).Value
LicType = SourceSheet.Cells(i, 12).Value
newuplift = SourceSheet.Cells(i, 15).Value
UpliftValue = SourceSheet.Cells(i, 28).Value
erow = DestSheet.Cells(DestSheet.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
'change the column numbers to the relevant number
DestSheet.Cells(erow, 1).Value = ProdType
DestSheet.Cells(erow, 2).Value = License
DestSheet.Cells(erow, 3).Value = Partner
DestSheet.Cells(erow, 4).Value = EndUser
DestSheet.Cells(erow, 5).Value = SOValue
DestSheet.Cells(erow, 6).Value = PostingMonth
DestSheet.Cells(erow, 7).Value = newuplift
DestSheet.Cells(erow, 8).Value = LicType
DestSheet.Cells(erow, 9).Value = UpliftValue 'TRYING TO PLACE A CONDITION HERE - SEE BELOW
' If newuplift = "Renewal" then place the Upliftvalue in row 9, otherwise set to "0"
End If
Next i
You could use IIf()
DestSheet.Cells(erow, 9).Value = IIf(newuplift = "Renewal", UpliftValue, 0)

VBA - Data Entry Form

I'm doing a Data Entry Table with a From using VBA. The code works fine, but it adds the entry to the first row after the table. For example, if the table's last row is row 20, then it will add the entry in row 21 outside of the table. Is there a way to assign the data to be added in the last row of the table?
This is the code I'm using to add the data to the table:
Private Sub buttonSave_Click()
If MsgBox("Deseas incluir este trabajo?", vbYesNo, "Guardar repuesto") = vbYes Then
'write the data to the worksheet from controls
Call WriteDataToSheet
'empty textboxes
Call EmptyTextBoxes
End If
End Sub
Private Sub WriteDataToSheet()
Dim newRow As Long
With Sheet1
newRow = .Cells(.Rows.Count, 2).End(xlUp).row + 1
.Cells(newRow, 1).Value = textboxNewDate.Value
.Cells(newRow, 2).Value = textboxNameNew.Value
.Cells(newRow, 3).Value = textboxNewEquipment.Value
.Cells(newRow, 4).Value = textboxNewTech.Value
.Cells(newRow, 5).Value = textboxNewCot.Value
.Cells(newRow, 6).Value = textboxNewPrice.Value
.Cells(newRow, 7).Value = textboxNewFac.Value
.Cells(newRow, 8).Value = textboxNewStatusF.Value
.Cells(newRow, 9).Value = textboxNewServ.Value
.Cells(newRow, 10).Value = textboxNewEnt.Value
.Cells(newRow, 11).Value = textboxNewDesc.Value
.Cells(newRow, 12).Value = textboxNewStatus.Value
End With
End Sub
This is the code I'm using to extract the range from the table. This is in a separate module.
Public Function GetType() As Variant
GetType = Sheet1.ListObjects("tbTabla2").DataBodyRange.Value
End Function
Is there a way I can assign the data to be added after the last row inside of "Tabla2".
This is the table for reference:
To keep your data inside the table, I suppose you would want to use the ListObject.ListRows.Add() method to add a new row. You could do something like this:
Private Sub WriteDataToTable()
Dim newTableRow As Range
newTableRow = Sheet1.ListObjects("Table2").ListRows.Add().Range
WriteDataToRow newTableRow
End Sub
Private Sub WriteDataToRow(row As Range)
Dim newRowNumber As Long
newRowNumber row.row
With Sheet1
.Cells(newRow, 1).Value = textboxNewDate.Value
.Cells(newRow, 2).Value = textboxNameNew.Value
.Cells(newRow, 3).Value = textboxNewEquipment.Value
.Cells(newRow, 4).Value = textboxNewTech.Value
.Cells(newRow, 5).Value = textboxNewCot.Value
.Cells(newRow, 6).Value = textboxNewPrice.Value
.Cells(newRow, 7).Value = textboxNewFac.Value
.Cells(newRow, 8).Value = textboxNewStatusF.Value
.Cells(newRow, 9).Value = textboxNewServ.Value
.Cells(newRow, 10).Value = textboxNewEnt.Value
.Cells(newRow, 11).Value = textboxNewDesc.Value
.Cells(newRow, 12).Value = textboxNewStatus.Value
End With
End Sub
Now, since you are using newRow = .Cells(.Rows.Count, 2).End(xlUp).row + 1 to find a last-used-row, that implies to me that you could be looking at a case were there is room in your table and you don't want to add a new row. In that case, you'll want to do some sort of conditional test to check if adding the new row is needed. Depending on how things are managed, I think its easier to just ensure the table always ends with the last used row so that you will ALWAYS be adding a row to the table when you want to add data.

Excel VBA Userform: data overwrites when I change first column of data entry

I am using the following code to enter data from Userform to Excel sheet and works fine.
The problem is that it overwrites the same row of data. But if I change:
.Cells(RowCount, 4).Value = Me.DepSectDrop.Value to contain a 1 --> .Cells(RowCount, 1).Value = Me.DepSectDrop.Value, and likewise for the rest (2 fore SiteFacOpen, 3 for CaseStartOpen, etc), it does not overwrite.
Private Sub cmdAdd_Click()
'Copy input values to sheet.
Dim RowCount As Long
Dim ws As Worksheet
Set ws = Worksheets("TRACK")
RowCount = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
.Cells(RowCount, 4).Value = Me.DepSectDrop.Value
.Cells(RowCount, 5).Value = Me.SiteFacOpen.Value
.Cells(RowCount, 6).Value = Me.CaseStartOpen.Value
.Cells(RowCount, 7).Value = Me.TypeDrop.Value
.Cells(RowCount, 8).Value = Me.ProcessDrop.Value
.Cells(RowCount, 9).Value = Me.CompNameOpen.Value
.Cells(RowCount, 10).Value = Me.CompEIDOpen.Value
.Cells(RowCount, 11).Value = Me.RespNameOpen.Value
.Cells(RowCount, 12).Value = Me.RespEIDOpen.Value
.Cells(RowCount, 13).Value = Me.DescOpen.Value
End With
'Clear input controls.
Me.DepSectDrop.Value = ""
Me.SiteFacOpen.Value = ""
Me.CaseStartOpen.Value = ""
Me.TypeDrop.Value = ""
Me.ProcessDrop.Value = ""
Me.CompNameOpen.Value = ""
Me.CompEIDOpen.Value = ""
Me.RespNameOpen.Value = ""
Me.RespEIDOpen.Value = ""
Me.DescOpen.Value = ""
End Sub
What do I need to do to so I maintain the right columns for it all to be entered, but not be overwritten? Thank you
You need to change all lines that start
.Cells(RowCount, 5).Value ...
To
.Cells(RowCount + 1, 5).Value
The '+1' bit means you're using the next blank line.
Also, as Samuel pointed out, you should also change to
RowCount = ws.Cells (Rows.Count, 4).End (xlUp).Offset (1,0).Row
so that you're testing a column that's guaranteed to have data in it!
Sorry, I missed the offset bit ... No need to '+1' if you're offsetting by 1 ... It amounts to the same thing.

When pressing the OK Button for my EXcel VBA Form the info does not get added to the data table correctly

I am trying to format an 'Ok' button so that all the information in my form fills in the next empty row in my data table. Here is what I have so far:
Private Sub OKButton_Click()
Dim emptyRow As Long
'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
'Transfer Information
Cells(emptyRow, 1).Value = BoatModelComboBox.Value
Cells(emptyRow, 2).Value = BoatIDTextBox.Value
Cells(emptyRow, 3).Value = NameofDefectComboBox.Value
Cells(emptyRow, 4).Value = InspectionAreaComboBox.Value
Cells(emptyRow, 5).Value = DateTextBox.Value
Cells(emptyRow, 6).Value = OccurenceTextBox.Value
Cells(emptyRow, 7).Value = DefectOriginComboBox.Value
If BoatCheckOptionButton1.Value = True Then
Cells(emptyRow, 8).Value = "TRUE"
Else
Cells(emptyRow, 8).Value = "FALSE"
End If
If TireKickOptionButton1.Value = True Then
Cells(emptyRow, 9).Value = "TRUE"
Else
Cells(emptyRow, 9).Value = "FALSE"
End If
Cells(emptyRow, 10).Value = TypeofInspectionComboBox.Value
Cells(emptyRow, 11).Value = MonthComboBox.Value
thanks for the help!
Try getting the empty row like this instead
emptyRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row + 1

VBA Dynamic Range with Values Calculation

Hello I have the following problem:
As you can see in column A we have Dates, in column B there is always a "1" when a year changes from one to the next, I marked it in yellow. In column H are different values and in column I, I want to have only the FIRST value, which is greater (in this case) than 10% within one year (so in the period from one to one in column B). After that I want to have the next first value, which is >10% in the next year so next period from 1 to 1 in column B and so on.
Can anyone help me?
So far I programmed this, but it shows me all values >10% but not the first from each range to each range.
Sub ABC ()
With ThisWorkbook.Worksheets("Test")
rowCount = 2
Do While .Cells(rowCount + 1, 8).Value <> ""
If .Cells(rowCount, 2).Value = 0 And .Cells(rowCount, 8).Value >= 0.1 Then
.Cells(rowCount, 9).Value = .Cells(rowCount, 7).Value * 0.1 / 1.1
Else
.Cells(rowCount, 9).Value = ""
End If
rowCount = rowCount + 1
Loop
End With
End Sub
It seem the code is error on the cell address
Sub ABC ()
With ThisWorkbook.Worksheets("Test")
rowCount = 2
Do While .Cells(rowCount + 1, 8).Value <> ""
If .Cells(rowCount, 2).Value = 0 And .Cells(rowCount, 8).Value >= 0.1 Then
.Cells(rowCount, 9).Value = .Cells(rowCount, 8).Value * 0.1 / 1.1
Else
.Cells(rowCount, 9).Value = ""
End If
rowCount = rowCount + 1
Loop
End With
End Sub

Resources