I created a simple form in Excel for inserting data to the spreadsheet. I have a table in my sheet with headlines in row 2 and blank in row 3. When I enter the data form into the first empty row, it started from row 4 (fist row under the table). I want to insert data into existing table. How can I do this?
This is Add button code in my form:
Private Sub Add_Click()
TrackingDate = UserForm1.TrackingDate.Value
INS = UserForm1.INS.Value
COY = UserForm1.COY.Value
Amount = UserForm1.Amount.Value
InvoiceDate = UserForm1.InvoiceDate.Value
InvoiceNumber = UserForm1.InvoiceNumber.Value
PolicyNumber = UserForm1.PolicyNumber.Value
Reminder = UserForm1.Reminder.Value
Cheque = UserForm1.Cheque.Value
Status = UserForm1.Status.Value
Range("A" & Rows.Count).End(xlUp).Offset(1).Value = TrackingDate
Range("B" & Rows.Count).End(xlUp).Offset(1).Value = INS
Range("C" & Rows.Count).End(xlUp).Offset(1).Value = COY
Range("D" & Rows.Count).End(xlUp).Offset(1).Value = Amount
Range("E" & Rows.Count).End(xlUp).Offset(1).Value = InvoiceDate
Range("F" & Rows.Count).End(xlUp).Offset(1).Value = InvoiceNumber
Range("G" & Rows.Count).End(xlUp).Offset(1).Value = PolicyNumber
Range("H" & Rows.Count).End(xlUp).Offset(1).Value = Reminder
Range("I" & Rows.Count).End(xlUp).Offset(1).Value = Cheque
Range("J" & Rows.Count).End(xlUp).Offset(1).Value = Status
End Sub
your code can be written like this:
Private Sub Add_Click()
Dim i&: i = [A:J].Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1 'find the last empty row range("A:J")
With UserForm1
Range("A" & i).Value = .TrackingDate.Value
Range("B" & i).Value = .INS.Value
Range("C" & i).Value = .COY.Value
Range("D" & i).Value = .Amount.Value
Range("E" & i).Value = .InvoiceDate.Value
Range("F" & i).Value = .InvoiceNumber.Value
Range("G" & i).Value = .PolicyNumber.Value
Range("H" & i).Value = .Reminder.Value
Range("I" & i).Value = .Cheque.Value
Range("J" & i).Value = .Status.Value
End With
End Sub
When you use Range("A" & Rows.Count).End(xlUp) approaching an Excel "table", the first cell that registers will be the last cell of the "table" even if that cell is blank. I haven't tested it but suspect if your "table" is all values then it won't auto-extend itself if something is entered below a blank row.
You could try adding an innocuous formula like =Row() in the last column of your table and hiding that column - then the table should auto-extend.
Related
I need to categorize my data into different buckets. my worksheet has column V & Column Y (actually a name match & address match respectively) has values that are either "ok" or "check". Column O has IDs, of which some are only numeric and some are alpha numeric.i need to fill my column A based on these 3 columns.
category 1 - Column A to be filled with "Verify name & Address" - logic for this is - If Column A is blank, Column V value = "check", Column Y value = "check" and column O = all alphanumeric IDs (except that starts with CWT) and numeric IDs = 2 & 9612
Category 2 - Column A to be filled with "Verify Address" - logic for this is - If Column A is blank, Column V value = "ok", Column Y value = "check" and column O = all alphanumeric IDs (except that starts with CWT) and numeric IDs = 2 & 9612.
Sub Rules()
'
'Autofill based on Rules
Worksheets("ORD_CS").Activate
Dim sht As Worksheet
Dim LR As Long
Dim i As Long
Set sht = ActiveWorkbook.Worksheets("ORD_CS")
LR = sht.UsedRange.Rows.Count
With sht
For i = 8 To LR
If .Range("A" & i).Value = "" And Range("V" & i).Value = "check" And Range("Y" & i).Value = "check" And Range("O" & i).Value = "2" And Range("O" & i).Value = "9612" Then
.Range("D" & i).Value = "Verify Name & Address"
End If
Next i
End With
End Sub
I have not completed my code. Can someone help? Thanks
The below should work, I changed your O column to be an OR
Edit: for function
Public Function IsAlpha(strValue As String) As Boolean
Dim intPos As Integer
For intPos = 1 To Len(strValue)
Select Case Asc(Mid(strValue, intPos, 1))
Case 65 To 90, 97 To 122
IsAlpha = True
Case Else
IsAlpha = False
Exit For
End Select
Next
End Function
With sht
For i = 8 To LR
If .Range("A" & i).Value = "" And Range("V" & i).Value = "check" And Range("Y" & i).Value = "check" And Range("O" & i).Value = "2" Or Range("O" & i).Value = "9612" Or IsAlpha(Range("O" & i).Value) = True Then
.Range("D" & i).Value = "Verify Name & Address"
Else
If .Range("A" & i).Value = "" AND .Range("V" & i).Value = "ok" AND .Range("O" & i).Value = "2" Or .Range("O" & i).Value = "9612" Then
Do Stuff
End If
End If
Next i
End With
I am trying to sum two columns, but i keep getting an error message of type mismatch. Where my error is?
Sub SumCols()
Dim ws As Worksheet
Set ws = Sheets("Recon")
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For i = 2 To LastRow
Range("E" & i).Value = Range("C" & i).Value + Range("D" & i).Value
Next i
End Sub
The below might be my issue, I checked for blank cells and non were found. But I can see blank cells.
Most likely one of the values you are trying to add together is not numeric, so check to see if they are numeric and non-blank before you try adding them.
For i = 2 To LastRow
If Len(Range("C" & i).Value) > 0 And Len(Range("D" & i).Value) > 0 Then
If IsNumeric(Range("C" & i).Value) And IsNumeric(Range("D" & i).Value) Then
Range("E" & i).Value = Range("C" & i).Value + Range("D" & i).Value
End If
End If
Next i
Also, you might be better off just using a formula:
Range("E" & i).Formula = "=C" & i & "+D" & i
I'm really new to VBA and I only get by with copying codes from solutions on this site but for this one I can't find a similar problem.
I want to copy only certain cells (not whole rows) when column A and B aligns as "apple" with "cat". I also want to delete that row I copied from.
the plan illustrated
My code so far:
Sub animals()
Dim rcnt As Long
rcnt = Worksheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To rcnt
If Range("B" & i).Value = "cat" And ("A" & i) = "apple" Then
Range("C" & i).Offset(1, 0).Copy
Range("D" & i).Offset(1, 0).Copy
End If
Next i
End Sub
For i = 1 To rcnt
If Range("B" & i).value = "cat" And ("A" & i) = "apple" Then
Range("C" & i).value = Range("C" & i).Offset(1, 0).value
Range("D" & i).value = Range("D" & i).Offset(1, 0).value
End If
Next i
You were copying the data, just not pasting it anywhere. This method will skip the .Copy (skipping the clipboard), and just sets the values equal.
I have an Excel sheet with data I would like to concatenate and use to create an .ini file.
There are multiple columns with data, if a cell in column D is highlighted I want it to copy the data in that row on to another sheet but at the same time I want it to concatenate the data in each column with the column header, see below:
From the picture above I would like the macro to copy the data into another sheet in the following format:
name = Machine 1
caption = Presentation
make = Company 1
model = Model 1
Is this possible?
If you want to create a ini file and not a sheet with ini look. Use this code:
LastRow = Range("A" & Rows.Count).End(xlUp).Row
for i = 3 to LastRow
If range("A" & i).Interior.ColorIndex = 2 then
MyFile = "C:\inifiles\" & Range("C" & i).Value & ".ini" 'Machine 1.ini
fnum = FreeFile()
Open MyFile For Output As #fnum
Print #fnum, "name=" & Range("C" & i).Value
Print #fnum, "caption=" & Range("D" & i).Value
Print #fnum, "make=" & Range("E" & i).Value
Print #fnum, "model=" & Range("F" & i).Value
Close #fnum
End if
Next i
It loops through all rows and uses the data to create a file with the "name" as the name of the file.
EDIT:
If you want to create the sheets with ini look:
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 3 To LastRow
If range("A" & i).Interior.ColorIndex = 2 then
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets.Add(After:= _
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
ws.Name = Sheets(1).Range("C" & i).Value
Sheets(Sheets(1).Range("C" & i).Value).Range("A1") = "name=" & Sheets(1).Range("C" & i).Value
Sheets(Sheets(1).Range("C" & i).Value).Range("A2") = "caption=" & Sheets(1).Range("D" & i).Value
Sheets(Sheets(1).Range("C" & i).Value).Range("A3") = "make=" & Sheets(1).Range("E" & i).Value
Sheets(Sheets(1).Range("C" & i).Value).Range("A4") = "model=" & Sheets(1).Range("F" & i).Value
End if
Next i
I am looking to see if there is a more efficient way to achieve the result below, so it can be extended if needed.
I'm using this to clean up large spreadsheets that have the rows C-Z blank. I imagine there should be a way to clean it up so that it doesn't have to double in size if I need to clean up a spreadsheet with data from C to AZ.
It's been a while since I used VBA, I found the code below online. (counting ROW B as the spreadsheet in question had an empty ROW A)
Sub delem()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "B").End(xlUp).Row
For r = lr To 1 Step -1
If Range("C" & r).Value = "" And Range("D" & r).Value = "" And Range("E" & r).Value = "" And Range("F" & r).Value = "" And Range("G" & r).Value = "" And Range("H" & r).Value = "" And Range("I" & r).Value = "" And Range("J" & r).Value = "" And Range("K" & r).Value = "" And Range("L" & r).Value = "" And Range("M" & r).Value = "" And Range("N" & r).Value = "" And Range("O" & r).Value = "" And Range("P" & r).Value = "" And Range("Q" & r).Value = "" And Range("R" & r).Value = "" And Range("S" & r).Value = "" And Range("T" & r).Value = "" And Range("U" & r).Value = "" And Range("V" & r).Value = "" And Range("W" & r).Value = "" And Range("X" & r).Value = "" And Range("Y" & r).Value = "" And Range("Z" & r).Value = "" Then Rows(r).Delete
Next r
End Sub
Thanks!
Just add an inner loop to go through the columns you care about. This will actually run much faster, as VBA doesn't short-circuit the If statement (all of the conditionals are evaluated). But with the loop, you can exit early if you find a value anywhere:
Sub delem()
Dim last As Long
Dim current As Long
Dim col As Long
Dim retain As Boolean
last = Cells(Rows.Count, "B").End(xlUp).Row
For current = last To 1 Step -1
retain = False
For col = 3 To 26
If Cells(current, col).Value <> vbNullString Then
retain = True
Exit For
End If
Next col
If Not retain Then Rows(current).Delete
Next current
End Sub
The Excel worksheet function COUNTA is a clean way to test if a range is empty.
Sub delem()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "B").End(xlUp).Row
For r = lr To 1 Step -1
'This function Counts the number of cells that are not empty
If WorksheetFunction.CountA(Range(Cells(r, 3), Cells(r, 26)) = 0 Then
Rows(r).Delete
End If
Next r
End Sub